ansible - 如果不存在,如何创建组并动态添加主机
问题描述
我有一个特殊要求,当条件满足时将主机名添加到组中(如果不存在,我还需要创建组),该组将在另一场比赛中传递,
# check if other side os is running on this host
- set_fact:
other_pair_host: "{{ inventory_hostname | regex_replace('ha', 'hb') }}"
when: (inventory_hostname | regex_search('^.+ha.+$') )
- set_fact:
other_pair_host: "{{ inventory_hostname | regex_replace('hb', 'ha') }}"
when: (inventory_hostname | regex_search('^.+hb.+$') )
- name: check other side host is running
set_fact:
other_side_running: "{{ other_pair_host in groups['reachable_other_pairs'] }}"
when: check_other_side_running == true
# if 'check_other_side_running' is set to false means we are on B side installation and we have already validated
- set_fact:
other_side_running: true
when: check_other_side_running == false
现在我需要创建该组的主机并且该组说'failed_hosts'。像这样的东西:
- set_fact:
host: inventory_hostname
when: check_other_side_running == false
- group_by:
key: "reachable_other_pairs"
不确定如何正确执行此操作。
添加后,我将在另一个游戏中使用这个组,如下所示
- name: Install on failed hosts
hosts: failed_hosts
connection: local
gather_facts: no
roles:
- Juniper.junos
有人可以就此提出建议吗?