I've got a playbook that looks like this:
---
- name: Launch Instances
hosts: localhost
gather_facts: no
vars:
aws_instance_type: m3.medium
aws_image: ami-cb214ae3
aws_region: us-east-1
machines:
- name: db
script: basic
- name: db2
script: basic
- name: cache
script: complicated
tasks:
- name: Launch instance
ec2:
key_name: development
instance_type: "{{ aws_instance_type }}"
image: "{{ aws_image }}"
wait: true
region: "{{ aws_region }}"
group: ansible-default
instance_tags:
cluster: "{{ tag }}"
controller: ansible
role: "{{
item.name }}"
register: ec2
with_items: machines
- name: Add new instance to host group
add_host: hostname={{ item[1].public_ip }} groups=launched
with_subelements:
- ec2.results
- instances
- name: Wait for SSH to come up
wait_for: host={{ item[1].public_dns_name }} port=22 delay=60 timeout=300 state=started
with_subelements:
- ec2.results
- instances
- name: Run init scripts
hosts: launched
tasks:
- name: Copy scripts
copy:
src: ../../build_scripts/scripts/
dest: /opt/comp/scripts/
force: yes
mode: 0755
- name: Execute script
shell: /opt/comp/scripts/init/{{ script }}.sh
The first play brings up two machines, and waits for them to be available. The second play loops over them, copying scripts and executing a specific one depending on the machine.
The problem, obviously, is that the {{ script }} variable doesn't work. I need a way to associate the host I bring up with the script variable associated with it. I could use tags, but this has to ultimate support providers that don't use tags. I'd like to write a value into the local facts of the host after bringing up a machine, but the "copy" tag doesn't support arbitrary hosts the way "wait_for" does. It would be great if "add_host" allowed you to specify variables to associate with them, or "set_fact" allowed you to set a fact for an arbitrary host, but they don't seem to.
What's the right way to do this?
-- Chris