Ansible:在下一个任务中循环注册的输出

Ansible:在下一个任务中循环注册的输出

Ansible版本:ansible 2.6.2 python版本2.7.5

我正在努力使用 with_items 循环遍历另一个任务中的注册 IP 地址,并从 ansible playbook 中获得调试输出,结果如下:

剧本:

- name: Check DVS portgroup from vCenter and allocate IP address from EPG description
  hosts: localhost
  connection: local
  gather_facts: no
  vars:
    nios_provider:
     host: "dns1"
     username: ""
     password: ""

  tasks:
  - name: Find unused IP address from EPG
    local_action: command sh /free.sh "{{ item }}"
    with_items: "DM-SNAP (VLAN-869) in DM AMZ, IP: 11.25.2.0/24 BD: BD-DH-VLAN-869 BD: BD_AZX_NO1, IP: 11.25.3.0/24"
    register: dvs_output

  - name: The IP
    debug: msg={{ dvs_output.results | map(attribute='stdout_lines') | list }}
#    debug: msg={{ dvs_output }}
    register: ip_dns

  - name: return next available IP address for network {{ ip_dns }}
    set_fact:
      ipaddr: "{{ lookup('nios_next_ip', '{{ item }}', num=10, provider=nios_provider) }}"
    with_items: "{{ ip_dns.msg }}"
    ignore_errors: true
    register: lookup

这是播放输出。此处播放失败,因为来自之前注册的变量的输入未格式化。

    PLAY [Check DVS portgroup from vCenter and allocate IP address from EPG description] ***********************************************************************************************************************

TASK [Find unused IP address from EPG] *********************************************************************************************************************************************************************
changed: [localhost -> localhost] => (item=DM-SNAP (VLAN-869) in DM AMZ, IP: 11.25.2.0/24 BD: BD-DH-VLAN-869 BD: BD_AZX_NO1, IP: 11.25.3.0/24)

TASK [The IP] **********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        [
            "11.25.2.0",
            "11.25.3.0"
        ]
    ]
}

TASK [return next available IP address for network {'msg': [[u'11.25.2.0', u'11.25.3.0']], 'failed': False, 'changed': False}] ***************************************************************************

ok: [localhost] => (item=11.25.2.0)
fatal: [localhost]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ lookup('nios_next_ip', '{{ item }}', num=10, provider=nios_provider) }}): 'dict' object is not callable"}
...ignoring

TASK [Debug IP] ********************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'dict object' has no attribute 'ansible_facts'\n\nThe error appears to have been in '/etc/ansible/playbooks/vmware/dev/test_allocate.yml': line 37, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: Debug IP\n    ^ here\n"}

PLAY RECAP *************************************************************************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=1

我怎样才能更好地使循环在第二个任务中工作,其中 IP 的值没有正确地输入到 {{ item }} 并从而循环?

答案1

您要做的是,在第一个任务中注册一个变量。该变量包含 IP,然后您将该变量过滤为一个列表并将其输出为调试并注册该调试输出,然后尝试迭代调试的输出。这样不好。

为什么不迭代调试中输出的过滤器?那么使用:

- name: return next available IP address for network {{ ip_dns }}
  set_fact:
    ipaddr: "{{ lookup('nios_next_ip', '{{ item }}', num=10, provider=nios_provider) }}"
  with_items: "{{ dvs_output.results | map(attribute='stdout_lines') | list }}"

另外 - 下一个问题 - 你遍历一个列表并设置一个变量的值。因此,完成该任务后,仍有一个变量ipaddr包含最后一个 IP。如果你想对所有 IP 地址执行某些操作,那么你应该使用:

- name: return next available IP address for network {{ ip_dns }}
  include_tasks: loop_over_an_ip.yml
  with_items: "{{ dvs_output.results | map(attribute='stdout_lines') | list }}"

包含loop_over_an_ip.yml每个 IP 应执行的任务列表(存储在 中item)。您可以通过 重命名它loop_var

- name: return next available IP address for network {{ ip_dns }}
  include_tasks: loop_over_an_ip.yml
  with_items: "{{ dvs_output.results | map(attribute='stdout_lines') | list }}"
  loop_control:
    loop_var: newip

相关内容