Configuring AWS EC2 instance after it's created through Ansible

37 views
Skip to first unread message

Michael Ludvig

unread,
Aug 25, 2016, 10:59:50 PM8/25/16
to Ansible Project
Hi there

I've got a play that creates an AWS EC2 instance through a CloudFormation template. After it's created I would like to configure it - I can use the AWS tools to do that but I prefer to use Ansible to be consistent with our other deployments.

What I have now is:

---
- name: Create Amazon Linux Instance
  hosts
: localhost
  connection
: local
  gather_facts
: no
  vars_files
:
 
- config.yml

  tasks
:
 
- name: Create CloudFormation Stack
    cloudformation
:
      stack_name
: "{{ stack_name }}"
      state
: present
     
template: basic-ec2-stack.json
      template_parameters
:
       
KeyName: "{{ key_name }}"
       
VpcId: "{{ vpc_id }}"
       
SubnetId: "{{ subnet_id }}"
       
...
   
register: stack

 
# The new instance name is in stack.stack_outputs.DnsName ...
 
- debug: var=stack.stack_outputs.DnsName


Now what? How can I run the rest of the playbook against the newly created host?
For example I would like to create user 'blah' but not on the localhost (against which the cloudformation module is running) but obviously on the EC2 instence. How do I do that?

Thanks!

Michael


Matt Davis

unread,
Aug 29, 2016, 9:03:35 PM8/29/16
to Ansible Project
You can use the returned data from cloudformation to call add_host, then start a new play that targets that host, or set up an EC2 dynamic inventory script that will pick up the host(s) you're creating and conditionally call meta: inventory_refresh when you know an instance got created (then do the same thing: start another play that touches the right host(s)). I personally prefer the add_host approach as it's more definitive that you're touching exactly the right host, but potato, potahto...

-Matt
Reply all
Reply to author
Forward
0 new messages