问题
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