AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 927125
Accepted
Mailo Světel
Mailo Světel
Asked: 2018-08-20 16:02:13 +0800 CST2018-08-20 16:02:13 +0800 CST 2018-08-20 16:02:13 +0800 CST

yaml 格式的动态库存

  • 772

背景

我通过 Terraform 管理域,它输出服务器 IP 地址。然后我有一个输出 YAML 清单的脚本(稍后输出)。

  • Ansible 2.6.2
  • 格式来源:https ://docs.ansible.com/ansible/2.6/user_guide/intro_inventory.html#hosts-and-groups

问题

当我运行ansible-inventory --inventory inventory.sh --graph它时,它总是以

@all:
  |--@stage:
  |--@ungrouped:
  |  |--18.66.1.28

如果我希望机器处于小组赛阶段。

我发现静态清单和作为脚本输出的清单有不同的行为。

静态文件

如果您将其保存为静态文件并将其用作库存,它将起作用。

all:
  hosts:
    18.66.1.28
  children:
    stage:
      hosts:
        18.66.1.28:

输出:

  @all:
  |--@stage:
  |  |--18.66.1.28
  |--@ungrouped:

脚本

但是如果你像下面这样指定shell脚本,它就行不通了

#!/bin/bash

echo "all:
  hosts:
    18.66.1.28
  children:
    stage:
      hosts:
        18.66.1.28:"

输出:

 [WARNING]:  * Failed to parse /tmp/inventory.sh with script plugin: You defined a group 'all' with bad data for the host list:  {u'hosts':
u'18.66.1.28', u'children': {u'stage': {u'hosts': {u'18.66.1.28': None}}}}

 [WARNING]:  * Failed to parse /tmp/inventory.sh with ini plugin: /tmp/inventory.sh:3: Error parsing host definition 'echo "all:': No closing
quotation

 [WARNING]: Unable to parse /tmp/inventory.sh as an inventory source

 [WARNING]: No inventory was parsed, only implicit localhost is available

YAML 输出

变体 1

all:
  hosts:
    - 18.66.1.28
  children:
    stage:
      hosts:
        - 18.66.1.28:

变体 2

all:
  hosts:
    - 18.66.1.28
  children:
    stage:
      hosts:
        18.66.1.28:

变体 3

all:
  hosts:
    - 18.66.1.28
  children:
    stage:
      hosts:
        - 18.66.1.28
ansible
  • 2 2 个回答
  • 5008 Views

2 个回答

  • Voted
  1. Best Answer
    Mailo Světel
    2018-08-23T15:45:17+08:002018-08-23T15:45:17+08:00

    简短的回答

    不要看https://docs.ansible.com/ansible/2.6/user_guide/intro_inventory.html而是看https://docs.ansible.com/ansible/2.6/dev_guide/developing_inventory.html。

    长答案

    Ansible 使用至少三种格式的清单。YAML、INI 和 JSON。INI 格式是完全独立的格式,有自己的结构。YAML 和 JSON 本身允许创建几乎相同的数据结构。当我查看充满 YAML 示例的https://docs.ansible.com/ansible/2.6/user_guide/intro_inventory.html时,使用 JSON 我只需更改符号以生成与 YAML 相同的数据结构。问题是,脚本插件使用不同的数据结构,但它允许您同时使用 YAML 和 JSON 格式。

    例子

    静态库存

    输入:

    $ cat /tmp/inventory-static 
    all:
      hosts:
        18.66.1.28
      children:
        stage:
          hosts:
            18.66.1.28:
    

    输出

    $ ansible-inventory --inventory /tmp/inventory-static --graph 
    @all:
      |--@stage:
      |  |--18.66.1.28
      |--@ungrouped:
    

    根据https://docs.ansible.com/ansible/2.6/user_guide/intro_inventory.html ,这是正确的格式和行为。

    脚本

    输入:

    $ cat /tmp/inventory.sh 
    #!/bin/bash
    
    echo "all:
      hosts:
        18.66.1.28
      children:
        stage:
          hosts:
            18.66.1.28:"
    

    输出:

     $ ansible-inventory --inventory /tmp/inventory.sh --graph    
     [WARNING]:  * Failed to parse /tmp/inventory.sh with script plugin: You defined a group 'all' with bad data for the host list:  {u'hosts':
    u'18.66.1.28', u'children': {u'stage': {u'hosts': {u'18.66.1.28': None}}}}
    
     [WARNING]:  * Failed to parse /tmp/inventory.sh with ini plugin: /tmp/inventory.sh:3: Error parsing host definition 'echo "all:': No closing
    quotation
    
     [WARNING]: Unable to parse /tmp/inventory.sh as an inventory source
    
     [WARNING]: No inventory was parsed, only implicit localhost is available
    

    这不是我所期望的。问题是脚本插件需要不同的数据结构。让我们用来自https://docs.ansible.com/ansible/2.6/dev_guide/developing_inventory.html的数据结构来试试这个。

    输入:

    $ cat /tmp/inventory.sh                                  
    #!/bin/bash
    echo "stage:
      hosts:
        - 18.66.1.28”
    

    输出

    $ ansible-inventory --inventory /tmp/inventory.sh --graph
    @all:
      |--@stage:
      |  |--18.66.1.28
      |--@ungrouped:
    

    是的,这是我预期的结果。机器在正确的组中,摄取插件没有抱怨任何事情。当然,根据“动态库存源”,脚本应该做的不仅仅是echo库存,它可能应该输出 JSON,这只是演示如何构建脚本化库存的输出。

    这花了我很多时间来弄清楚,所以我希望通过写这篇文章我能把它保存给其他人。

    • 2
  2. Vladimir Botka
    2018-08-21T01:25:17+08:002018-08-21T01:25:17+08:00

    主机名(您的变体 1)之前不应有破折号。

    all:
      hosts:
        18.66.1.28
      children:
        stage:
          hosts:
            18.66.1.28:
    

    $ ansible-inventory --inventory 库存--graph

    @all:
      |--@stage:
      |  |--18.66.1.28
      |--@ungrouped:
    

    使用破折号,您应该会看到很多警告。

    all:
      hosts:
        - 18.66.1.28
      children:
        stage:
          hosts:
            - 18.66.1.28:
    

    $ ansible-inventory --inventory 库存--graph

     [WARNING]:  * Failed to parse inventory with yaml plugin:
     Invalid "hosts" entry for "all" group, requires a dictionary, found "<class 
     'ansible.parsing.yaml.objects.AnsibleSequence'>" instead.
    
     [WARNING]:  * Failed to parse inventory with ini plugin:
     inventory:3: Expected key=value host variable assignment, 
     got: 18.66.1.28
    
     [WARNING]: Unable to parse inventory as an inventory source
    
     [WARNING]: No inventory was parsed, only implicit localhost is available
    
    ...
    

    引用 man ansible-inventory

    -i, --inventory, --inventory-file
        specify inventory host path or comma separated host list.
        --inventory-file is deprecated
    

    如果脚本首先创建一个文件,那么一切都是正确的。

    $ ./inventory.sh > /tmp/my_inventory.yml && ansible-inventory --inventory /tmp/my_inventory.yml --graph

    @all:
      |--@stage:
      |  |--18.66.1.28
      |--@ungrouped:
    
    • 0

相关问题

  • 重复的 Ansible 任务

  • 无法形成站点中的文件的链接,该链接可用于使用 ansible 在远程服务器中启用的目录站点?

  • 如何执行 ansible 的特定角色?

  • Ansible 和 rbash

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve