Marco van Dam
Marco van Dam

Reputation: 97

Ansible AWS EC2 tags

I have an amazon console with multiple instances running. All instances have tags

for example: - tag Name: jenkins - tag Name: Nginx - tag Name: Artifactory

I want to run an Ansible playbook against the hosts that are tagged as Nginx.

I use dynamic inventory but how do I limit where the playbook is run?

My playbook looks like this:

  - name: Provision an EC2 node
    hosts: local
    connection: local
    gather_facts: False
    vars:
      instance_type: t2.micro
      security_group: somegroup
      #image: ami-a73264ce
      image: ami-9abea4fb
      region: us-west-2
      keypair: ansible_ec2
    tasks:
      - name: Step 1 Create a new AWS EC2 Ubuntu Instance
        local_action: ec2 instance_tags="Name=nginx" group={{ security_group }} instance_type={{ instance_type}} image={{ image }} wait=true region={{ region }} keypair={{ keypair }}
        register: ec2
      - name: Step 2  Add new instance to local host group
        local_action: lineinfile dest=hosts regexp="{{ item.public_dns_name }}" insertafter="[launched]" line="{{ item.public_dns_name }} ansible_ssh_private_key_file=~/.ssh/{{ keypair }}.pem"
        with_items: ec2.instances
      - name: Step 3 Wait for SSH to come up delay 180 sec timeout 600 sec
        local_action: wait_for host={{ item.public_dns_name }} port=22 delay=180 timeout=600 state=started
        with_items: ec2.instances

   - name: Step 5 Install nginx steps
     hosts: launched 
     sudo: yes 
     remote_user: ubuntu 
     gather_facts: True
     roles:
       - motd
       - javaubuntu
       - apt-get
       - nginx

Upvotes: 2

Views: 5652

Answers (2)

MillerGeek
MillerGeek

Reputation: 3137

All tags become groups in dynamic inventory, so you can specify the tag in the "hosts" parameter

- name: Provision an EC2 node
  hosts: local
  connection: local
  gather_facts: False
  vars:
      instance_type: t2.micro
      security_group: somegroup
      #image: ami-a73264ce
      image: ami-9abea4fb
      region: us-west-2
      keypair: ansible_ec2
  tasks:
    - name: Step 1 Create a new AWS EC2 Ubuntu Instance
      local_action: ec2 instance_tags="Name=nginx" group={{ security_group }} instance_type={{ instance_type}} image={{ image }} wait=true region={{ region }} keypair={{ keypair }}
      register: ec2
    - name: Step 2  Add new instance to local host group
      local_action: lineinfile dest=hosts regexp="{{ item.public_dns_name }}" insertafter="[launched]" line="{{ item.public_dns_name }} ansible_ssh_private_key_file=~/.ssh/{{ keypair }}.pem"
      with_items: ec2.instances
    - name: Step 3 Wait for SSH to come up delay 180 sec timeout 600 sec
      local_action: wait_for host={{ item.public_dns_name }} port=22 delay=180 timeout=600 state=started
      with_items: ec2.instances

- name: Step 5 Install nginx steps
  hosts: tag_Name_Nginx 
  sudo: yes 
  remote_user: ubuntu 
  gather_facts: True
  roles:
    - motd
    - javaubuntu
    - apt-get
    - nginx

Upvotes: 2

Raul Hugo
Raul Hugo

Reputation: 1136

Try with:

roles/create-instance/defaults/main.yml

quantity_instance: 1
key_pem: "ansible_ec2"
instance_type: "t2.micro"
image_base: "ami-9abea4fb"
sec_group_id: "somegroup"
tag_Name: "Nginx"
tag_Service: "reverseproxy"
aws_region: "us-west-2"
aws_subnet: "somesubnet"
root_size: "20"  

---
- hosts: 127.0.0.1
  connection: local
  gather_facts: False
  tasks:
    - name: Adding Vars
      include_vars: roles/create-instance/defaults/main.yml

    - name: run instance
      ec2:
         key_name: "{{ key_pem }}"
         instance_type: "{{ instance_type }}"
         image: "{{ image_base }}"
         wait: yes
         group_id: "{{ sec_group_id }}"
         wait_timeout: 500
         count: "{{ quantity_instance }}"
         instance_tags:
           Name: "{{ tag_Name }}"
           Service: "{{ tag_Service }}"
         vpc_subnet_id: "{{ aws_subnet }}"
         region: "{{ aws_region }}"
         volumes:
           - device_name: /dev/xvda
             volume_size: "{{ root_size }}"
             delete_on_termination: true
         assign_public_ip: yes
      register: ec2

    - name: Add new instance to host group
      add_host: hostname={{ item.public_ip }} groupname=launched
      with_items: ec2.instances 

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

- hosts: launched
  vars:
    ansible_ssh_private_key_file: ~/.ssh/ansible_ec2.pem
  gather_facts: true
  user: ubuntu
  become: yes
  become_method: sudo
  become_user: root
  roles:
    - motd 
    - javaubuntu
    - apt-get
    - nginx

To avoid add as a variable ansible_ssh_private_key_file: ~/.ssh/ansible_ec2.pem, use .ssh/config file and add the following:

IdentityFile ~/.ssh/ansible_ec2.pem 

Remember the config file need chmod 600.

If dont want create the instances again.

launch other playbook like this:

- hosts: tag_Name_Nginx
  vars:
    ansible_ssh_private_key_file: ~/.ssh/ansible_ec2.pem
  gather_facts: true
  user: ubuntu
  become: yes
  become_method: sudo
  become_user: root
  roles:
    - motd 
    - javaubuntu
    - apt-get
    - nginx

And notes how we call the specific tag_Name_Nginx.

Upvotes: 2

Related Questions