When I list the subnet cidr of every host in an environment I get repeated subent cidrs in the output as multiple hosts can be in same subnet.
What I’m trying to achieve is run a shell command in ansible for each unique subnet cidr from the above output.
Part of the playbook code for understanding purpose
- name: Generate management cidr
shell: echo {{ net_addr | ipaddr('net') }}
register: management_cidr
- debug:
var: management_cidr.stdout
- name: Apply iptables rule for Subnets[management_cidr]
shell: iptables -A INPUT -p tcp -s {{ management_cidr.stdout }} -m comment --comment "Subnet Access" -m state --state NEW,ESTABLISHED -j ACCEPT
If I use the above playbook code it will apply duplicate iptables rules
And the output of playbook
TASK: [debug ]
****************************************************************
ok: [wephgyn01.wep.ani.ag.com] => {
"var": {
"management_cidr.stdout": "136.26.13.129/26"
}
}
ok: [wephgyn02.wep.ani.ag.com] => {
"var": {
"management_cidr.stdout": "136.26.13.129/26"
}
}
ok: [wephgyn03.wep.ani.ag.com] => {
"var": {
"management_cidr.stdout": "136.26.13.129/26"
. . . [136.26.13.129/26 is repeated] .
ok: [wepr05c001.wep.ani.ag.com] => {
"var": {
"management_cidr.stdout": "136.26.13.193/26"
}
}
ok: [wepr04c018.wep.ani.ag.com] => {
"var": {
"management_cidr.stdout": "136.26.13.129/26"
}
}
ok: [wepr05c002.wep.ani.ag.com] => {
"var": {
"management_cidr.stdout": "136.26.13.193/26"
}
}
. . . [136.26.13.193/26 is repeated] .
ok: [wepr03c005.wep.ani.ag.com] => {
"var": {
"management_cidr.stdout": "136.26.13.65/26"
}
}
ok: [wepr03c008.wep.ani.ag.com] => {
"var": {
"management_cidr.stdout": "136.26.13.65/26"
}
}
ok: [wepr03c004.wep.ani.ag.com] => {
"var": {
"management_cidr.stdout": "136.26.13.65/26"
}
}
. . . [136.26.13.65/26 is repeated]
So what I thought was to store register variable output into a list and make the list unique in Ansible and then use the list with the command.
Tried multiple sources but couldn’t find anything useful. Any ideas?