Ansible基于组多次运行任务

时间:2015-02-13 01:15:24

标签: ansible ansible-playbook

如何在每个群组上运行任务,而不仅仅为群组运行一次​​?

我除了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的行为并不像我期望的那样。以下输出consumer两次。

-
# 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 个答案:

答案 0 :(得分:1)

最简单的方法是使用别名代替主机名:

---
- name: Register Groups
  hosts: localhost
  connection: local
  tasks:
    - name: Add the rest-api alias for my app
      add_host:
        name: my-app-rest-api
        ansible_ssh_host: 127.0.0.1
        groups: rest-api

    - name: Add the msg-consumer alias for my app
      add_host:
        name: my-app-msg-consumer
        ansible_ssh_host: 127.0.0.1
        groups: msg-consumer


- name: Test Run Types
  hosts:
    - msg-consumer
    - rest-api
  tasks:
    - name: What's your run type
      debug: msg="Run Type of {{ ansible_ssh_host }} is {{ run_type }}"

现在您可以再次使用group_vars:

group_vars / REST的API:

---
run_type: web

group_vars / MSG-消费者:

---
run_type: consumer

,输出结果为:

PLAY [Register Groups] ******************************************************** 

TASK: [Add the rest-api alias for my app] ************************************* 
ok: [localhost]

TASK: [Add the msg-consumer alias for my app] ********************************* 
ok: [localhost]

PLAY [Test Run Types] ********************************************************* 

TASK: [What's your run type] ************************************************** 
ok: [my-app-msg-consumer] => {
    "msg": "Run Type of 127.0.0.1 is consumer"
}
ok: [my-app-rest-api] => {
    "msg": "Run Type of 127.0.0.1 is web"
}

答案 1 :(得分:0)

现在这是我能想到的最好的:

---
- name: Register Groups
  hosts: localhost
  connection: local
  tasks:
    - name: Add new host group
      add_host:
        name: 127.0.0.1
        ansible_connection: local
        groups: new-server
        run_types:
          - rest-api
          - msg-consumer
    - name: Add another new host group
      add_host:
        name: 127.0.0.2
        ansible_connection: local
        groups: new-server
        run_types:
          - nothing

- name: Test Run Types Server 1
  hosts:
    - new-server
  tasks:
    - name: What's your run type
      debug: var=item
      with_items: run_types

注意:主机必须不同才能工作,否则它将覆盖并使用add_host使用的最后一个变量值。

答案 2 :(得分:0)

请参阅Ansible run task once per database-name下的答案。

基本上,没有run_once_per_group,我所知道的最接近的方法是循环群组的真run_once。为了使问题更加混乱,没有group_vars字典变量。

---
- hosts: all
  tasks:
  - name: "do this once per group"
    delegate_to: localhost
    debug:
      msg: "do something on {{hostvars[groups[item.key].0]['somevar']}} for group named {{item}}"
    run_once: yes
    with_dict: groups
    when: item.key not in ['all', 'ungrouped']
相关问题