10

I know how to create an AWS instance using Ansible. Now what I want to achieve is to configure that instance as web server by installing nginx using the same playbook which created the instance.

The goal of the playbook will be:

  1. Create an AWS instance.
  2. Configure the instance as Web server by setting up the Nginx server.

Is it possible with ansible?

0

3 Answers 3

8

Read http://www.ansible.com/blog/ansible-ec2-tags It details how to spin up an ec2 instance (or multiple) and then run tasks against it (I.e install nginx).

I'f you want to jump straight to the example playbook https://github.com/chrismeyersfsu/playbook-ec2_properties/blob/master/new_group.yml

  • Bring up ec2 instance
  • Wait for ssh
  • add ec2 instance to Ansible dynamically created host group w/ associated ec2 pem file (so you can ssh to it)
  • Call an example play with a ping task to show everything works

Note: you would replace the ping task with your set of tasks to install nginx

@Bidyut How to reference ec2 ip address

look at Line 27 Note the use of register: ec2Then at Line 46 the ec2 ip address is "extracted" {{ ec2.results[item.0]['instances'][0]['public_ip'] }}. Note that the example calls register within a loop. If you are just creating one ec2 instance then the ec2 ip address reference would look like {{ ec2.results['instances'][0]['public_ip'] }}

8
  • Can you explain how to get/obtain particular fields from JSON result returned from the AWS in Ansible playbook. I tried to google it, but i didn't find any.
    – Bidyut
    Commented Dec 22, 2015 at 4:21
  • What is 'item' in that line?
    – Bidyut
    Commented Dec 22, 2015 at 13:24
  • In 'public_ip' its value is None
    – Bidyut
    Commented Dec 22, 2015 at 13:29
  • Description of item. I am looping over ec2_instances which is an array that parallels ec2 item.0 is an index (i.e. 0, 1, 2).
    – chrism
    Commented Dec 22, 2015 at 13:30
  • @Bidyut use - debug: msg="{{ ec2 }}" to see what the structure of the ec2 variable looks like. You should find public_ip nested in the JSON structure.
    – chrism
    Commented Dec 22, 2015 at 13:33
4

Here is a working example that might help you.

---
- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - name: Create the EC2 Instance
      ec2:
        region: us-east-1
        group: sg-xxxxx # Replace your Security Group here
        keypair: test-key # Replace Key here
        instance_type: t2.mirco
        image: ami-xxxxx # Replace AMI here
        vpc_subnet_id: subnet-xxxxx # Replace Subnet here
        assign_public_ip: yes
        wait: yes
        wait_timeout: 600
        instance_tags:
           Name: "My-EC2-Instance"
      register: ec2

    - name: Create SSH Group to login dynamically to EC2 Instance
      add_host: 
        hostname: "{{ item.public_ip }}"
        ansible_ssh_private_key_file: path/to/test-pair.pem
        groupname: ec2_server
      with_items: ec2.instances

    - name: Wait for SSH to come up
      wait_for: 
        host: "{{ item.public_ip }}" 
        port: 22 
        state: started
      with_items: ec2.instances

 - hosts: ec2_server
   become: yes
   # Use ec2_user if you are using CentOS/Amazon server
   remote_user: ubuntu # for Ubuntu server
   gather_facts: yes
   roles:
     - webserver
1

Yes, you can use a single playbook to launch an instance and install nginx. Use the ansible module add_host to add the ip of the just launched instance. Then write a play for the new host.

  1. Launch an EC2 instance using ec2 module and register the instance
  2. Use add_host module to add the new instance to the host inventory
  3. Write a new play with host as the just registered host and call apt to install nginx

Try it and if you need code snippet, let me know.

2
  • I am currently exploring Ansible. I face problem while retrieving any particular field from the JSON output which AWS returns to the Ansible. I need some explaination how to obtain a particular field from the JSON result and how to access the result set.
    – Bidyut
    Commented Dec 22, 2015 at 4:23
  • Can I get the code snippet. I want to go through your code snippet.
    – Bidyut
    Commented Dec 22, 2015 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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