使用--limit指定主机时,能否确保CS1上的Common Play仅运行一次?
Ansible Playbook 执行问题分析与解答
相关文件内容
inventory 文件
[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'
install.yml 文件
- import_playbook: "pb.yml" - import_playbook: "common.yml"
问题描述
执行以下任意一条命令时,是否可以确保common play仅在CS1主机上运行一次?
ansible-playbook -i inventory install.yml --limit 'DB1' ansible-playbook -i inventory install.yml --limit 'WS1'
解答
不能确保,反而会出现common play完全不会在CS1上运行的情况,原因如下:
--limit参数是全局生效的,会过滤整个playbook里所有play的目标主机。当你指定--limit 'DB1'或--limit 'WS1'时,CS1不在这个限定名单里,对应的commonplay会因为没有符合条件的主机被直接跳过。
如果要实现common play不管命令行怎么加--limit,都只在CS1上运行一次,给common.yml里的play添加limit: common参数即可:
修改后的common.yml:
- name: common task should run on common host only(that too only once) hosts: common limit: common tasks: - name: Run common task debug: msg: 'Running common task'
这样这个play就会强制只在common组的主机上执行,不受命令行--limit的限制,自然就能保证在CS1上仅运行一次。
内容的提问来源于stack exchange,提问作者Arjun
相关产品推荐
相关产品推荐

