【问题标题】:Skip current host and print the rest in a config file ansible跳过当前主机并在配置文件 ansible 中打印其余主机
【发布时间】:2020-08-12 20:27:59
【问题描述】:

伙计们,我正在尝试在运行我的任务以编辑 XML 时实现以下目标

要求: 跳过创建配置文件的当前主机并将库存组中的其余主机打印到配置文件

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

谁能帮助实现这个目标?

【问题讨论】:

    标签: linux ansible devops ansible-inventory


    【解决方案1】:

    问:“跳过当前主机...并打印清单组中的其余主机。”

    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
    

    【讨论】:

      【解决方案2】:

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

      使用模板作为: {{ tg_hosts |差异([ansible_host])|列表 |加入(':port,') + ':port' }}

      【讨论】:

        猜你喜欢
        • 2022-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-04
        • 2018-06-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多