使用Ansible动态添加子组

时间:2019-08-27 08:33:43

标签: ansible

我想动态地复制以下静态清单:

novaclient ansible_connection=local

[masters]
192.168.13.236

[nodes]
192.168.13.201
192.168.13.237

[cluster:children]
masters
nodes

通过阅读ansible文档,add_host模块似乎是正确的选择,因为它旨在将主机(或一组)添加到ansible-playbook内存清单中。这是我的工作手册:

- hosts: all

  connection: local

  vars:
    ips_per_group:
      - ["masters", "192.168.13.236"]
      - ["nodes"  , "192.168.13.201"]
      - ["nodes"  , "192.168.13.237"]

  tasks:

    - name: add host dynamically
      add_host:
        name: "{{ item[1] }}"
        groups: "{{ item[0] }}" 
      loop: "{{ ips_per_group }}"

    - name: add masters and nodes groups to cluster group
      add_host:
        name: "{{ item[0] }}"
        groups: "cluster"
      loop: "{{ ips_per_group }}"

    - name: test
      debug:
        var: groups

运行该剧本会触发以下警告:

 [WARNING]: Found both group and host with same name: masters
 [WARNING]: Found both group and host with same name: nodes

据我了解,这些警告来自masters组中nodeschildren尚未被声明为cluster的事实。尝试以下语法:

- name: add masters and nodes groups to cluster group
  add_host:
    name: "{{ item }}"
    groups: "cluster:children"
  loop:
    - masters

由于groups名称中的冒号,触发了以下警告:

 [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

您有没有任何警告的想法吗?

谢谢

2 个答案:

答案 0 :(得分:1)

当我尝试您的示例并重新阅读文档时,我感到非常惊讶。

但是在阅读了module's source之后,我并没有真正看到可以实现将组添加为子级的东西(或者至少是干净的,因此来自the inventory data management class的警告)

但是您仍然可以通过立即将主机添加到所有需要的组中来达到预期的结果,这将在最后给出相同的结果(使用包含子项声明的静态清单检查UPDATE YOUR_TABLE TOUT SET TOUT.CHECKIN = ( SELECT CASE WHEN SUM(T.CHECKIN) = 0 THEN 1 ELSE TOUT.CHECKIN END FROM YOUR_TABLE T WHERE T.ITEMID IN ( 1, 2, 3 ) ) WHERE TOUT.ITEMID IN ( 1, 2, 3 ); var,这与我在下面提供的一个)

groups

请注意,如果您仍然希望将其保留在单独的任务中并利用已经创建的组作为子组,则可以这样做:

---
- hosts: localhost
  gather_facts: false

  vars:
    ips_per_group:
      - ["masters", "192.168.13.236"]
      - ["nodes"  , "192.168.13.201"]
      - ["nodes"  , "192.168.13.237"]

  tasks:
    - name: Add host dynamically
      add_host:
        name: "{{ item[1] }}"
        groups:
          - "{{ item[0] }}"
          - cluster
      loop: "{{ ips_per_group }}"

    - name: See result
      debug:
        var: groups

无论选择哪种解决方案,这都是最终的组(前提是您一开始的库存为空)

  tasks:

    - name: Add host dynamically
      add_host:
        name: "{{ item[1] }}"
        groups:
          - "{{ item[0] }}"
      loop: "{{ ips_per_group }}"

    - name: Add masters and nodes to cluster group
      add_host:
        name: "{{ item }}"
        groups:
          - cluster
      loop: "{{ ['masters', 'nodes'] |
        map('extract', groups) |
        list | flatten | unique }}"

答案 1 :(得分:1)

可以使用group_by – Create Ansible groups based on facts。修改后的数据结构使示例更简单,下面的代码展示了它的工作原理。

- name: 1.Create my_groups
  hosts: localhost
  gather_facts: false
  vars:
    ips_per_group:
      192.168.13.236:
        my_groups: ['masters']
        my_children_group: 'cluster'
      192.168.13.201:
        my_groups: ['nodes']
        my_children_group: 'cluster'
      192.168.13.237:
        my_groups: ['nodes']
        my_children_group: 'cluster'
  tasks:
    - name: add host dynamically
      add_host:
        name: "{{ item.key }}"
        groups: "{{ item.value.my_groups }}" 
        group_children: "{{ item.value.my_children_group }}"
      loop: "{{ ips_per_group|dict2items }}"
    - debug:
        var: groups

- name: 2.Create children for group masters
  hosts: masters
  gather_facts: false
  tasks:
    - name: add hosts to children groups
      group_by:
        key: "{{ group_children }}"
    - debug:
        var: groups

- name: 3.Create children for group nodes
  hosts: nodes
  gather_facts: false
  tasks:
    - name: add hosts to children groups
      group_by:
        key: "{{ group_children }}"
    - debug:
        var: groups

- name: 4.Test group cluster
  hosts: cluster
  gather_facts: false
  tasks:
    - debug:
        var: group_children
    - debug:
        var: groups

- name: 5.Print groups
  hosts: localhost
  tasks:
    - debug:
        var: groups

播放“ 5.Print组”给予

"groups": {
    "all": [
        "192.168.13.201", 
        "192.168.13.236", 
        "192.168.13.237"
    ], 
    "cluster": [
        "192.168.13.236", 
        "192.168.13.201", 
        "192.168.13.237"
    ], 
    "masters": [
        "192.168.13.236"
    ], 
    "nodes": [
        "192.168.13.201", 
        "192.168.13.237"
    ], 
    "ungrouped": []
}