鉴于以下inventory
文件,ansible -m raw -a 'mkdir test'
等adhoc命令如何在不同的机器上运行?
例如,如何运行命令:
app1
和app2
app1
且不在app2
app1
或app2
这是库存文件:
[app1]
ip1.address
ip2.address
ip3.address
[app2]
ip1.address
ip2.address
ip4.address
答案 0 :(得分:2)
如您的问题所述,在不同的主机组上运行Ansible ad-hoc。 -l参数称为limit,用于在清单中的一组有限节点上执行任务。
ansible all -i' / path / to / inventory' -l' app1,app2' -m shell -a" mkdir $ HOME /测试"
ansible all -i' / path / to / inventory' -l' app1' -m shell -a" mkdir $ HOME /测试"
ansible all -i' / path / to / inventory' -l' app1:app2' -m shell -a" mkdir $ HOME /测试
PS:如果已经在ansible.cfg中配置了库存,则无需明确指定库存路径。
答案 1 :(得分:1)
您可以在此处找到所有相关信息:https://docs.ansible.com/ansible/latest/user_guide/intro_patterns.html
Description Pattern(s) Targets All hosts all (or *) One host host1 Multiple hosts host1:host2 (or host1,host2) One group webservers Multiple groups webservers:dbservers all hosts in webservers plus all hosts in dbservers Excluding groups webservers:!atlanta all hosts in webservers except those in atlanta Intersection of groups webservers:&staging any hosts in webservers that are also in staging
答案 2 :(得分:0)
这是一个古老的问题,但您也可以这样做:
ansible all -i '/path/to/inventory' -l '!app2' -m shell -a "mkdir $HOME/test"
将在清单中所有不在app2中的主机上运行。或者,如果您的清单中包含的资源超过了所示的两个组,并且您希望限制为app1中的主机,而不是app2中的主机:
ansible app1 -i '/path/to/inventory' -l '!app2' -m shell -a "mkdir $HOME/test"