Akhil S
Akhil S

Reputation: 29

Skip current host and print the rest in a config file ansible

Folks, I'm trying to achieve the below while running my task to edit an XML

Requirement: Skip current host in which config file is created and print the rest of host from the inventory group to the config file

<ManagerNode ip = "**node IP**" port = "**node port**"/> <haNodes>IP2:port,IP3:port</haNodes> <!-- Comma Seperated Node IPs with port, ,Except the Same Node -->

Can anyone help with achieving this goal?

Upvotes: 1

Views: 175

Answers (2)

Akhil S
Akhil S

Reputation: 29

defined a variable tg_hosts: : "{{ groups['tgzone']|map('extract',hostvars,'ansible_host')|list }}"

used template as: {{ tg_hosts | difference([ansible_host])| list | join(':port,') + ':port' }}

Upvotes: 0

Vladimir Botka
Vladimir Botka

Reputation: 68189

Q: "Skip current host ... and print the rest of hosts from the inventory group."

A: Create a list of all IPs and use filter difference to remove the current IP. For example the inventory

shell> cat hosts
[ha]
test_01 IP=10.1.0.11
test_02 IP=10.1.0.12
test_03 IP=10.1.0.13

[ha:vars]
port=4567

and the playbook

shell> cat playbook.yml
- hosts: ha
  tasks:
    - set_fact:
        all_IP: "{{ groups.ha|map('extract', hostvars, 'IP')|list }}"
      run_once: true
    - debug:
        msg: "{{ all_IP|difference([IP])|
                 product([port])|
                 map('join', ':')|
                 list }}"

give (abridged)

shell> ansible-playbook -i hosts playbook.yml 

ok: [test_01] => 
  msg:
  - 10.1.0.12:4567
  - 10.1.0.13:4567
ok: [test_02] => 
  msg:
  - 10.1.0.11:4567
  - 10.1.0.13:4567
ok: [test_03] => 
  msg:
  - 10.1.0.11:4567
  - 10.1.0.12:4567

Limit the play to test_01 gives abridged

shell> ansible-playbook -i hosts -l test_01 playbook.yml 

ok: [test_01] => 
  msg:
  - 10.1.0.12:4567
  - 10.1.0.13:4567

Upvotes: 1

Related Questions