跳过当前主机并在配置文件中打印其余部分

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

伙计们,我正在努力实现以下目标,而 运行 我的任务是编辑 XML

要求: 跳过创建配置文件的当前主机,并将清单组中的主机的其余部分打印到配置文件

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

任何人都可以帮助实现这个目标吗?

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

A: 创建所有 IP 的列表并使用过滤器 difference 删除当前 IP。例如库存

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

和剧本

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 }}"

给出(删节)

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

限制播放test_01给出删节

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

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

定义了一个变量 tg_hosts: : "{{ groups['tgzone']|map('extract',hostvars,'ansible_host')|list }}"

使用模板作为: {{ tg_hosts |差异([ansible_host])|清单 |加入(':端口,')+':端口'}}