Reputation: 21
I am trying to add a proxy to Zabbix server using the ansible-zabbix module
I have vars that collect IP addresses/hostnames of servers/proxies/agents
I need to go to each zabbix server and add all the proxies that I have.
I tried several different "with_XXX" loops, none of them seems to work. Environment: 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 }}"
Expected: to create a proxy on the server (current setup is 1xServer, 1xProxy, 2xAgent as test env)
Actual result: mostly I get the error:
"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"}"
Upvotes: 0
Views: 312
Reputation: 21
It worked
I substituted with_XXX
with a nested loop
, but I also changed my variables structure a bit
The final version looks like this:
---
- 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 }}"
Upvotes: 1
Reputation: 927
I think you need with_nested
and not with_list
https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html#with-nested-with-cartesian
Upvotes: 0