Hi,
I am having hard time figuring out idiomatic way of doing AWS infrastructure deployment and application e.g. EC2 instance provisioning in one go in same playbook. The problem being accessing EC2 host in the next play after AWS infrastructure creation. The example playbook is as follows:
---
- name: Deploy cloud infrastructure
hosts: localhost
connection: local
gather_facts: False
vars:
aws_region: "us-east-1"
ssh_key_name: "MyAwsKey"
stack_name: "test-stack"
tasks:
- name: Create a sample stack
cloudformation:
stack_name: "{{ stack_name }}"
state: "present"
region: "us-east-1"
disable_rollback: true
template: "files/simple.template"
template_parameters:
KeyName: "{{ ssh_key_name }}"
register: cf
- name: Output EC2 IP
debug: var=cf.stack_outputs.PublicIP
# Try to install something to EC2
- name: Provision application
hosts: "{{ hostvars.localhost.cf.stack_outputs.PublicIP }}"
remote_user: "ubuntu"
sudo: yes
tasks:
- name: Just a test command
command: uptime
register: out
- debug: var=out.stdout
And output:
ok: [localhost] => {
"var": {
"cf.stack_outputs.PublicIP": "XXX.XXX.XXX.XXX"
}
}
PLAY [Provision application] **************************************************
skipping: no hosts matched
My naive assumption was that consecutive play "Provision application" would reference EC2 instance with hosts directive via registered variable e.g. hostvars.localhost.cf.stack_outputs.PublicIP which apparently does not work like I imagined.
So, the question is - what is the right way to approach this problem? I bet I am missing something. I would prefer just one playbook that does both cloud infrastructure deployment and server/application provisioning.
Thanks!
Juris