$ 猫库存
[dbservers]
DB1 ansible_host=10.252.22.163
[webservers]
WS1 ansible_host=10.252.22.163
[common]
CS1 ansible_host=10.252.22.163
$ 猫 pb.yml
- name: db and web
hosts:
- dbservers
- webservers
tasks:
- name: Run task
debug:
msg: 'Running on all dbservers and webserver'
$ 猫 common.yml
- name: common task should run on common host only(that too only once)
hosts:
- common
tasks:
- name: Run common task
debug:
msg: 'Running common task'
$ cat install.yml
- import_playbook: "pb.yml"
- import_playbook: "common.yml"
每次运行以下任何命令时,是否可以在 CS1 主机上运行普通游戏(仅一次)?
> ansible-playbook -i inventory install.yml --limit 'DB1'
> ansible-playbook -i inventory install.yml --limit 'WS1'
这是一种可能的解决方案:
库存
[dbservers]
DB1 ansible_host=localhost ansible_connection=local host_key_checking=false
CS1 ansible_host=localhost ansible_connection=local host_key_checking=false
[webservers]
WS1 ansible_host=localhost ansible_connection=local host_key_checking=false
CS1 ansible_host=localhost ansible_connection=local host_key_checking=false
[common]
CS1 ansible_host=localhost ansible_connection=local host_key_checking=false
安装.yml
- name: Install <todo more info>
hosts: "{{ target_host }}"
tasks:
- import_tasks: "pb.yml"
when: inventory_hostname not in groups['common']
- name: common tasks
import_tasks: "common.yml"
when: inventory_hostname in groups['common']
common.yml
- name: Run common task COMMON
debug:
msg: 'Running common task'
pb.yml
- name: Run task PB
debug:
msg: 'Running on all dbservers and webserver'
运行命令
$ ansible-playbook -i inventory install.yml -e target_host=dbservers
$ ansible-playbook -i inventory install.yml -e target_host=webservers