Marcelo Hualoto
Marcelo Hualoto

Reputation: 23

Why Ansible don't like AmazonLinux2 ami's?

i just was trying some stuff with ansible but i really appreciate if anyone can reproduce this or at least can explain this to me. I'm trying to deploy instances with ansible on AWS. I'm using ansible (2.9.6) from a virtual machine deployed with Vagrant on W10 host. I wrote this playbook:

---

- name: Configuring the EC2 instance
  hosts: localhost
  connection: local
  vars:
    count: '{{ count }}'
    volumes:
      - device_name: /dev/sda1
        volume_size: '{{ volume_size }}'

  tasks:
    - name: Launch Instances
      ec2:
        instance_type: '{{ instance_type }}'
        image: '{{ ami }}'
        region: '{{ region }}'
        key_name: '{{ pem }}'
        count: '{{ count }}'
        group_id: '{{ sec_grp }}'
        wait: true
        volumes: '{{ volumes }}'
      register: ec2

    - name: Associating after allocating eip
      ec2_eip:
        in_vpc: yes
        reuse_existing_ip_allowed: yes
        state: present
        region: '{{ region }}'
        device_id: '{{ ec2.instance_ids[0] }}'
      register: elastic_ip

    - name: Adding tags to the instance
      local_action:
        module: ec2_tag
        region: '{{ region }}'
        resource: '{{ item.id }}'
        state: present
      with_items: '{{ ec2.instances }}'
      args:
        tags: 
          Name: '{{ tag_name }}'
          Env: '{{ tag_env }}'
          Type: AppService
      register: tag

I execute the next command with --extra-vars: ansible-playbook ec2.yml --extra-vars instance_type=t2.micro -e ami=ami-04d5cc9b88f9d1d39 -e region=eu-west-1 -e pem=keyname -e count=1 -e sec_grp=sg-xx -e volume_size=10 -e tag_name=prueba -e tag_env=dev -vvv

ami-04d5cc9b88f9d1d39 = Amazon Linux 2 AMI (HVM), SSD Volume Type for eu-west-1

When the playbook finish to run succesfully and on my aws-console seeing the instance booting and changed to running state (initializing), suddenly change to terminating and finally to stopped state. On my terminal the playbook runs fine and i get:

PLAY RECAP **************************
localhost                  : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

I spent whole day trying to fix this, changing '''state: running''' and adding new tasks to make instance explicitly runnning. Well, finally the problem was the AMI. I changed it to an ubuntu ami: ami-035966e8adab4aaad on same region and works fine, it's still runing. I used this ami before (amazon linux 2) with cloudformation and terraform and never happened something like this, always boot fine. Well, if anybody have any idea why this is happening or there is something that i'm missing, i really like to know it. Take care!

Upvotes: 1

Views: 716

Answers (1)

saurabh14292
saurabh14292

Reputation: 1401

I was able to reproduce this issue. I tried everything you have mentioned. Copied yml file you have provided, executed with exact same values (except key-pair and security group). EC2 goes into running state and then stops. To isolate the issue, I did the same thing in another region (AP-South-1), with same Amazon Linux 2 ami, but the same error was reproduced.

This is because, Amazon Linux 2 AMI requires EBS to be mounted at /dev/xvda and not at /dev/sda1. /dev/sda1 is used when AMI is ubuntu.

Since the AMI and mount path for root EBS is not compatible, EC2 instance goes into stopped state after initializing.

Refer this SO issue : AWS EC2 Instance stopping right after start using boto3

Update the "volume" part of yml and it should work fine.

Upvotes: 1

Related Questions