ansible: create temporary inventory with multiple groups with add_host or group_by

余生颓废 提交于 2021-01-07 07:52:25

问题


Is there any way to create an in memory inventory during provisioning tasks with add_host or group_by modules such:

[SET]
1.1.1.1
[SET:vars]

ip_address={{ inventory_hostname }}

[SET1]
1.1.1.2
[SET:vars]
ip_address={{ inventory_hostname }} 

回答1:


Yes. You can do something like this (if you provide more information in your question, we can provide more specificity ourselves):

- add_host:
    hostname: 1.1.1.1
    groups: SET
- add_host:
    hostname: 1.1.1.2
    groups: SET1

This will dynamically add 1.1.1.1 to the inventory as part of the SET group and 1.1.1.2 to the inventory as part of the SET1 group. there are a couple of good example of doing this during provision steps for rackspace

tasks:

  - name: Provision a set of instances
    local_action:
        module: rax
        name: "{{ rax_name }}"
        flavor: "{{ rax_flavor }}"
        image: "{{ rax_image }}"
        count: "{{ rax_count }}"
        group: "{{ group }}"
        wait: yes
    register: rax
  - name: Add the instances we created (by public IP) to the group 'raxhosts'
    local_action:
        module: add_host
        hostname: "{{ item.name }}"
        ansible_host: "{{ item.rax_accessipv4 }}"
        ansible_ssh_pass: "{{ item.rax_adminpass }}"
        groups: raxhosts
    with_items: "{{ rax.success }}"
    when: rax.action == 'create'


来源:https://stackoverflow.com/questions/41202452/ansible-create-temporary-inventory-with-multiple-groups-with-add-host-or-group

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!