我在所有api_servers中都运行了ansible任务,我将其限制为只能在我添加了run_once的一个IP(api_server之一)上运行:true,但没有帮助。
好心提醒。编辑:
以下工作吗?我有10个app_servers实例正在运行,我希望任务仅在一台app_server上运行
run_once: true
when:
- inventory_hostname == groups['app_servers'][0]
我的库存文件在哪里
[app_servers]
prod_app_[1:4]
我会这样写我的剧本:
---
# Run on all api_servers
- hosts: api_servers
tasks:
- name: do something on all api_servers
#...
# Run only on one api_server e.q. api_server_01
- hosts: api_server_01
tasks:
- name: Gather data from api_server_01
#...
另一个选项是使用when:
或运行带有该--limit
选项的剧本
---
- hosts: all
tasks:
- name: do something only when on api_server_01
#...
when: inventory_hostname == "api_server_01"
编辑:在这里您将在一个示例中看到所有选项:
---
- hosts: all
tasks:
- debug: msg="run_once"
run_once: true
- debug: msg=all
- debug: msg="run on the first of the group"
when: inventory_hostname == groups['app_servers'][0]
# Option with separated hosts, this one will be faster if gather_facts is not tuned.
- hosts: app_servers[0]
tasks:
- debug: msg="run on the first of the group"
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句