1

I've got an Ansible playbook that creates an AWS EC2 instance through a CloudFormation template. After it's created I would like to configure it also through Ansible.

Here is what I have now:

---
- 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 new EC2 instance. How do I do that?

Thanks!

2

You should be able to add the instance to a group with add_host and create an in memory inventory.

   - name: Add instance  to host group
     add_host: hostname={{ item.DnsName }} groups=cloud_formation
     with_items: stack.stack_outputs

   - name: Wait for SSH to come up
     wait_for: host={{ item.DnsName }} port=22 delay=60 timeout=320 state=started
     with_items: stack.stack_outputs

   - name: Run your play
     hosts: cloud_formation
     ----- your play here -------

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.