如何在每个组上运行任务,而不仅仅是为组执行一次?
我除了 Run this on each host
任务将为每个group_var值运行一次 . 相反,它似乎只是选择一个并运行它 .
我计划稍后在多个服务器上打破这些,但是现在它应该可以在一个自动缩放上运行,然后随着需求的增加然后轻松将其分解为多个自动缩放组 .
playbook.yml:
---
# Run with: ansible-playbook -i localhost, playbook.yml
- name: Register Groups
hosts: localhost
connection: local
tasks:
- name: Add the groups
add_host:
name: localhost
ansible_connection: local
groups: rest-api, msg-consumer
- name: Run this on each host
hosts:
- rest-api
- msg-consumer
tasks:
- name: Say type
debug: var=item
with_items: run_type
group_vars / REST的API:
---
run_type: web
group_vars / MSG-消费者:
---
run_type: consumer
输出Ansible 1.8.2:
$ ansible-playbook -i localhost, playbook.yml
PLAY [Register Groups] ********************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [Add the groups] ********************************************************
ok: [localhost]
PLAY [Run this on each host] **************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [Say type] **************************************************************
ok: [localhost] => (item=consumer) => {
"item": "consumer"
}
PLAY RECAP ********************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0
注意:它可能是其他东西 . 我以为我也可以把我的剧本弄得乱七八糟,但是如下所示分散任务:
---
- name: Register Groups
hosts: localhost
connection: local
tasks:
- name: Add the groups
add_host:
name: localhost
ansible_connection: local
groups: rest-api, msg-consumer
- name: Run this on each host
hosts:
- msg-consumer
tasks:
- name: Say type
debug: var=item
with_items: run_type
- name: Run this on each host
hosts:
- rest-api
tasks:
- name: Say type
debug: var=item
with_items: run_type
但第二部剧本的输出是:
$ ansible-playbook -i localhost, playbook2.yml
PLAY [Register Groups] ********************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [Add the groups] ********************************************************
ok: [localhost]
PLAY [Run this on each host] **************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [Say type] **************************************************************
ok: [localhost] => (item=consumer) => {
"item": "consumer"
}
PLAY [Run this on each host] **************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [Say type] **************************************************************
ok: [localhost] => (item=consumer) => {
"item": "consumer"
}
PLAY RECAP ********************************************************************
localhost : ok=6 changed=0 unreachable=0 failed=0
编辑:然而另一次尝试访问数据,看起来group_vars的行为并不像我期望的那样 . 以下输出消费者两次 .
-
# Run with: ansible-playbook -i localhost, playbook.yml
- name: Register Groups
hosts: localhost
connection: local
tasks:
- name: Add the groups
add_host:
name: localhost
ansible_connection: local
groups: rest-api, msg-consumer
- name: Run this on each host
hosts:
- msg-consumer
- rest-api
tasks:
- name: What's your run type
debug: var=hostvars[groups[item][0]]['run_type']
with_items: group_names
3 回答
最简单的方法是使用别名作为主机名而不是真正的主机:
现在你可以再次使用你的group_vars:
group_vars / REST的API:
group_vars / MSG-消费者:
输出将是:
现在这是我能想到的最好的:
注意:主机必须不同才能使其正常工作,否则它将覆盖并使用add_host使用的最后一个变量值 .
请参阅Ansible run task once per database-name下的答案 .
基本上,没有
run_once_per_group
,而我所知道的最接近的方法是循环群组的真正的run_once
. 为了使问题更加混乱,没有group_vars
字典变量 .