是否可以根据手动定义的主机变量自动创建主机组?
这是我的清单:
[webserver]
web1 ansible_host=192.168.20.1 production=False
web2 ansible_host=192.168.20.2 production=False
web3 ansible_host=192.168.20.3 production=True
web4 ansible_host=192.168.20.4 production=True
...我希望有一个动态主机组,例如,webserver_production,并使用它作为目标。
还有其他方法来存档吗?谢谢。
发布于 2022-11-10 18:00:06
使用已构建的库存插件。看见
shell> ansible-doc -t inventory constructedshell> tree inventory/
inventory/
├── 01-webserver
└── 02-webserver_production.yml
0 directories, 2 filesshell> cat inventory/01-webserver
[webserver]
web1 ansible_host=192.168.20.1 production=False
web2 ansible_host=192.168.20.2 production=False
web3 ansible_host=192.168.20.3 production=True
web4 ansible_host=192.168.20.4 production=Trueshell> cat inventory/02-webserver_production.yml
plugin: constructed
groups:
webserver_production: productionshell> ansible-inventory -i inventory --list --yaml
all:
children:
ungrouped: {}
webserver:
hosts:
web1:
ansible_host: 192.168.20.1
production: false
web2:
ansible_host: 192.168.20.2
production: false
web3:
ansible_host: 192.168.20.3
production: true
web4:
ansible_host: 192.168.20.4
production: true
webserver_production:
hosts:
web3: {}
web4: {}shell> cat pb.yml
- hosts: webserver
gather_facts: false
tasks:
- debug:
var: ansible_play_hosts_all
run_once: true
- hosts: webserver_production
gather_facts: false
tasks:
- debug:
var: ansible_play_hosts_all
run_once: true给予(删节)
shell> ansible-playbook -i inventory pb.yml
PLAY [webserver] *****************************************************************************
TASK [debug] *********************************************************************************
ok: [web1] =>
ansible_play_hosts_all:
- web1
- web2
- web3
- web4
PLAY [webserver_production] ******************************************************************
TASK [debug] *********************************************************************************
ok: [web3] =>
ansible_play_hosts_all:
- web3
- web4备注:
https://stackoverflow.com/questions/74392322
复制相似问题