Ansible:遍历多个单独的列表

Ansible:遍历多个单独的列表,ansible,ansible-facts,Ansible,Ansible Facts,我正在尝试使用ansible Zabbix模块向Zabbix服务器添加代理 我有收集服务器/代理/代理的IP地址/主机名的VAR 我需要转到每个zabbix服务器并添加我拥有的所有代理 我尝试了几种不同的“with_XXX”循环,它们似乎都不起作用。 环境:CentOS7、Ansible2.8、Python2.7、Zabbix4.2 --- - name: Install and configure Zabbix Proxy + Agent hosts: zabbix-proxy bec

我正在尝试使用ansible Zabbix模块向Zabbix服务器添加代理

我有收集服务器/代理/代理的IP地址/主机名的VAR

我需要转到每个zabbix服务器并添加我拥有的所有代理

我尝试了几种不同的“with_XXX”循环,它们似乎都不起作用。 环境:CentOS7、Ansible2.8、Python2.7、Zabbix4.2

---
- name: Install and configure Zabbix Proxy + Agent
  hosts: zabbix-proxy
  become: true
  gather_facts: true
  vars:
   proxy_env:
     http_proxy: proxy_used_in_different_tasks
     https_proxy: proxy_used_in_different_tasks
   servervars:
     ip: "{{ groups['zabbix-server'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | join(',') }}" # server nodes IPs as a comma delimited string, for configs
     ip_list: "{{ groups['zabbix-server'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list }}" # server nodes IPs as a list, for ansible
     hostname: "{{ groups['zabbix-server'] | map('extract', hostvars, 'ansible_hostname') | list }}" #server nodes hostnames as a list
   proxyvars:
     ip: "{{ groups['zabbix-proxy'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | join(',') }}" # proxy nodes IPs as a comma delimited string, for configs
     ip_list: "{{ groups['zabbix-proxy'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list }}" # proxy nodes IPs as a list, for ansible
     hostname: "{{ groups['zabbix-proxy'] | map('extract', hostvars, 'ansible_hostname') | list }}" #proxy nodes hostnames as a list
   agentvars:
     ip_list: "{{ groups['zabbix-agent-only'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list }}" # agent nodes IPs as a list, for ansible
     hostname: "{{ groups['zabbix-agent-only'] | map('extract', hostvars, 'ansible_hostname') | list }}" #agent nodes hostnames as a list

  tasks:
### below works fine - shows all facts I need
    - debug:
        msg:
        - TEST 1
        - "{{ servervars }}"
        - TEST 2
        - "{{ proxyvars }}"
        - TEST 3
        - "{{ agentvars }}"

    - name: Create proxies on the server
      local_action:
        module: zabbix_proxy
        server_url: "http://{{ item.0.hostname }}.net.local"
        login_user: Admin
        login_password: zabbix
        proxy_name: "{{ item.1.hostname }}"
        description: Proxy
        status: active
        state: present
        interface:
            type: 0
            main: 1
            useip: 0
            ip: "{{ item.1.ip_list }}"
            dns: "{{ item.1.hostname }}"
            port: 10050
      with_list:
        - "{{ servervars }}"
        - "{{ proxyvars }}"
预期:在服务器上创建代理(当前设置为1xServer、1xProxy、2xAgent作为测试环境)

实际结果:我得到的错误主要是:

"FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: dict object has no element 0\n\nThe error appears to have been in '/root/proxy.yml': line 41, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: Create proxies in the server\n      ^ here\n\nexception type: <class 'ansible.errors.AnsibleUndefinedVariable'>\nexception: dict object has no element 0"}"
“失败!=>{“msg”:”该任务包含一个带有未定义变量的选项。错误是:dict对象没有元素0\n\n错误似乎出现在“/root/proxy.yml”:第41行第7列,但可能\n出现在文件的其他位置,具体取决于语法问题。\n\n出现问题的行是:\n\n\n-name:在服务器中创建代理\n^此处\n\n异常类型:\n异常:dict对象没有元素0“}”

我想您需要嵌套的
而不是
与_list

一起工作

我用嵌套的
循环
替换了
,但我也稍微改变了变量结构

最终版本如下所示:

---
- name: Install and configure Zabbix Proxy + Agent
  hosts: zabbix-proxy
  become: true
  vars:
   proxy_env:
     http_proxy: proxy_used_in_different_tasks
     https_proxy: proxy_used_in_different_tasks
   allvars:
     servervars:
       ip: "{{ groups['zabbix-server'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | join(',') }}" # server nodes IPs as a comma delimited string, for configs
       ip_list: "{{ groups['zabbix-server'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list }}" # server nodes IPs as a list, for ansible
       hostname: "{{ groups['zabbix-server'] | map('extract', hostvars, 'ansible_hostname') | list }}" #server nodes hostnames as a list
     proxyvars:
       ip: "{{ groups['zabbix-proxy'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | join(',') }}" # proxy nodes IPs as a comma delimited string, for configs
       ip_list: "{{ groups['zabbix-proxy'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list }}" # proxy nodes IPs as a list, for ansible
       hostname: "{{ groups['zabbix-proxy'] | map('extract', hostvars, 'ansible_hostname') | list }}" #proxy nodes hostnames as a list
     agentvars:
       ip_list: "{{ groups['zabbix-agent-only'] | map('extract', hostvars, ['ansible_default_ipv4', 'address']) | list }}" # agent nodes IPs as a list, for ansible
       hostname: "{{ groups['zabbix-agent-only'] | map('extract', hostvars, 'ansible_hostname') | list }}" #agent nodes hostnames as a list

  tasks:
    - name: Create proxies in the server
      local_action:
        module: zabbix_proxy
        server_url: "http://{{ item.0 }}.net.local/zabbix/"
        login_user: Admin
        login_password: zabbix
        proxy_name: "{{ item.1 }}"
        description: Proxy
        status: active
        state: present
        interface:
            type: 0
            main: 1
            useip: 0
            ip: "{{ item.2 }}"
            dns: "{{ item.1 }}"
            port: 10050
      loop: "{{ allvars.servervars.hostname|product(allvars.proxyvars.hostname, allvars.proxyvars.ip_list)|list }}"

with_nested
由于某种原因不起作用,但在对变量进行了一点修改后(由于某种原因有所帮助),我将固定剧本作为一个单独的回复发布在下面