create a dynamic group from hosts or ips
If you get back a set of hostnames or ip addresses from another action and you want to use that as a group to operate on for a new play you can use the http://ansible.cc/docs/modules.html#add-host action.
It creates a temporary, in-memory group of hosts that only persists until the end of the playbook.
An example:
Get back the list of guests from a libvirt host, put them in a group named myvms_new and halt all of the guests directly, not using the virt interface. (we do this b/c sometimes some guests do not respond to the shutdown command from the virt host)
- name: find instances
hosts: $vhost
user: root
tasks:
- name: get list of guests
action: virt command=list_vms
register: vmlist
- name: add them to myvms_new group
local_action: add_host hostname=$item groupname=myvms_new
with_items: ${vmlist.list_vms}
- name: halt instances
hosts: myvms_new
user: root
tasks:
- name: echo-y
action: command /sbin/halt -p
ignore_errors: true
You can also use add_host with the ec2 module to have a list of ips to act on from just-created instances.
Written by Seth Vidal
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ansible
Authors
Misha Behersky
78.97K
tartansandal
73.31K
Related Tags
#ansible
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#