首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ansible:遍历多个单独的列表

Ansible:遍历多个单独的列表
EN

Stack Overflow用户
提问于 2019-09-16 19:08:49
回答 2查看 218关注 0票数 0

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

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

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

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

代码语言:javascript
运行
复制
---
- 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作为测试环境)

实际结果:大多数情况下我得到的是错误:

代码语言:javascript
运行
复制
"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"}"
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-18 16:57:08

啊,真灵

我用嵌套的loop替换了with_XXX,但我还稍微改变了变量结构

最终版本如下所示:

代码语言:javascript
运行
复制
---
- 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 }}"
票数 1
EN

Stack Overflow用户

发布于 2019-09-18 11:06:02

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57955540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档