【问题标题】:Loop ansible_host stuck with first item循环 ansible_host 卡在第一项上
【发布时间】:2019-08-07 20:48:15
【问题描述】:

我正在使用模块 csv-source-of-truth (https://github.com/joelwking/csv-source-of-truth) 从 csv 文件中获取 IP 和操作系统信息。我能够将这些信息注册到一个 vsheet 并使用调试,我可以看到我可以遍历 vsheet 的内容。

但是,当我使用 ios_command 并尝试遍历 vsheet 时,它似乎卡在了 vsheet 的第一个条目上。

这是 Inventory.csv 文件的内容:

192.168.68.201,ios

192.168.68.202,ios

代码:

---
- hosts: localhost
  gather_facts: false

  tasks:
    - name: Block
      block:
          - name: Use CSV

            csv_to_facts:
              src: '{{playbook_dir}}/NEW/Inventory.csv'
              vsheets:
                - INFO:
                    - IP
                    - OS

          - debug:
              msg: '{{item.IP}}'
            loop: '{{INFO}}'

          - name: Show Version
            vars:
              ansible_host: '{{item.IP}}'
              ansible_network_os: '{{item.OS}}'
              ansible_user: cisco
              ansible_ssh_pass: cisco
              ansible_connection: network_cli
              ansible_become: yes
              ansible_become_method: enable
            ios_command:
              commands: show version
            register: output
            loop: '{{INFO}}'

          - name: Show the output of looped Show Version
            debug:
              var: output

          - name: Show just the stdout_lines
            debug:
              var: output.results.{{item}}.stdout_lines
            with_sequence: "0-{{output|length - 2}}" 

当您查看正常运行时间信息时,您会注意到输出中只有 R1 的结果。即 R1 有这样那样的正常运行时间。


PLAY [localhost] **********************************************************************************************************************************************

TASK [Use CSV] ************************************************************************************************************************************************
ok: [localhost]

TASK [debug] **************************************************************************************************************************************************
ok: [localhost] => (item={u'IP': u'192.168.68.201', u'OS': u'ios'}) => {
    "msg": "192.168.68.201"
}
ok: [localhost] => (item={u'IP': u'192.168.68.202', u'OS': u'ios'}) => {
    "msg": "192.168.68.202"
}

TASK [Show Version] *******************************************************************************************************************************************
ok: [localhost] => (item={u'IP': u'192.168.68.201', u'OS': u'ios'})
ok: [localhost] => (item={u'IP': u'192.168.68.202', u'OS': u'ios'})

TASK [Show the output of looped Show Version] *****************************************************************************************************************
ok: [localhost] => {
    "output": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "auth_pass": null,
                        "authorize": null,
                        "commands": [
                            "show version"
                        ],
                        "host": null,
                        "interval": 1,
                        "match": "all",
                        "password": null,
                        "port": null,
                        "provider": null,
                        "retries": 10,
                        "ssh_keyfile": null,
                        "timeout": null,
                        "username": null,
                        "wait_for": null
                    }
                },
                "item": {
                    "IP": "192.168.68.201",
                    "OS": "ios"
                },
                "stdout": [
                   -- Output removed for brevity
                ],
                "stdout_lines": [
                    [
                        "-- Output removed for brevity

                        "R1 uptime is 1 hour, 34 minutes",

                    ]
                ]
            },
            {
                "ansible_loop_var": "item",
                "changed": false,
                "failed": false,
                "invocation": {
                    "module_args": {
                        "auth_pass": null,
                        "authorize": null,
                        "commands": [
                            "show version"
                        ],
                        "host": null,
                        "interval": 1,
                        "match": "all",
                        "password": null,
                        "port": null,
                        "provider": null,
                        "retries": 10,
                        "ssh_keyfile": null,
                        "timeout": null,
                        "username": null,
                        "wait_for": null
                    }
                },
                "item": {
                    "IP": "192.168.68.202",
                    "OS": "ios"
                },
                "stdout": [
                   -- Output removed for brevity
                ],
                "stdout_lines": [
                    [
                       -- Output removed for brevity
                        "R1 uptime is 1 hour, 34 minutes",

                    ]
                ]
            }
        ]
    }
}

TASK [Show just the stdout_lines] *****************************************************************************************************************************
ok: [localhost] => (item=0) => {
    "ansible_loop_var": "item",
    "item": "0",
    "output.results.0.stdout_lines": [
        [
           -- Output removed for brevity
            "R1 uptime is 1 hour, 34 minutes",

        ]
    ]
}
ok: [localhost] => (item=1) => {
    "ansible_loop_var": "item",
    "item": "1",
    "output.results.1.stdout_lines": [
        [
           -- Output removed for brevity
            "R1 uptime is 1 hour, 34 minutes",
        ]
    ]
}

PLAY RECAP ****************************************************************************************************************************************************
localhost                  : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

【问题讨论】:

  • 提示:查看add_host,在您的 CSV 中从您的主机创建一个动态组,并在后续播放中定位该组。

标签: loops ansible


【解决方案1】:

尝试创建库存

      - name: Create inventory
        add_host:
          hostname: '{{ item.IP }}'
          groups: temp_group_01
          ansible_network_os: '{{ item.OS }}'
          ansible_user: cisco
          ansible_ssh_pass: cisco
          ansible_connection: network_cli
          ansible_become: yes
          ansible_become_method: enable
        loop: '{{ INFO }}'

并委托给主机

      - name: Show Version
        ios_command:
          commands: show version
        register: output
        delegate_to: '{{ item }}'
        loop: '{{ groups['temp_group_01'] }}'

说明

从下面的播放可以看出,连接不服从改变的ansible_host,一直使用循环中的第一项。

- hosts: test_01
  tasks:
    - command: hostname
      register: result
      vars:
        ansible_host: "{{ item }}"
      loop:
        - test_02
        - test_03
    - debug:
        msg: "{{ result.results|map(attribute='stdout')|list }}"

给予

TASK [command] ******************************************************************************
changed: [test_01] => (item=test_02)
changed: [test_01] => (item=test_03)

TASK [debug] ********************************************************************************
ok: [test_01] => {
    "msg": [
        "test_02", 
        "test_02"
    ]
}

这种行为很可能是由连接插件引起的,因为 vars 按预期工作。下面的戏

- hosts: test_01
  tasks:
    - command: echo "{{ ansible_host }}"
      register: result
      vars:
        ansible_host: "{{ item }}"
      loop:
        - test_02
        - test_03
    - debug:
        msg: "{{ result.results|map(attribute='stdout')|list }}"

给予

TASK [command] ******************************************************************************
changed: [test_01] => (item=test_02)
changed: [test_01] => (item=test_03)

TASK [debug] ********************************************************************************
ok: [test_01] => {
    "msg": [
        "test_02", 
        "test_03"
    ]
}

因此,无法循环 ansible_host。而是应使用 delegate_to

【讨论】:

  • 非常感谢!它起作用了,它遍历了其他行。你能向我解释一下这是如何工作的吗?
  • 不客气!我已将解释部分添加到答案中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-21
  • 1970-01-01
  • 2017-05-06
  • 1970-01-01
相关资源
最近更新 更多