循环ansible_host卡在第一项

时间:2019-08-07 20:48:15

标签: loops ansible

我正在使用模块csv-source-of-truth(https://github.com/joelwking/csv-source-of-truth)从csv文件获取IP和OS信息。我能够将这些信息注册到vsheet中,并使用debug,可以看到可以遍历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  

1 个答案:

答案 0 :(得分:0)

尝试创建库存

      - 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