error trying to change the root volume_size for EC2 instance using ansible

amazon-ec2, amazon-web-services, ansible

Solution

I got interested in answering this because you put in a (nearly) fully working example. I copied it locally, made small changes to work in my AWS account, and iterated to figure out the solution.

I suspected a YAML+Ansible problem. I tried a bunch of things and looked around. Michael DeHaan (creator of Ansible) said the complex argument/module style is required as seen in the ec2 examples. Here's how the module looks now- no changes elsewhere.

  tasks:
    - name: make one instance
      local_action:
           module: ec2
           image: "{{ image }}"
           instance_type: "{{ instance_type }}"
           keypair: "{{ keypair }}"
           region: "{{ region }}"
           group: "{{ group }}"
           volumes: "{{volumes}}"
           instance_tags: '{"Name":"{{instance_name}}"}'
           wait: true
      register: ec2_host

After converting it worked- or at least got to the next error, which was because the EC2 instance needs to be in a VPC (error below). I expect you can solve that- if not, leave a comment and I'll get it fully working.

TASK: [make one instance] ***************************************************** 
<127.0.0.1> REMOTE_MODULE ec2 region=us-east-1 keypair=mykey instance_type=t2.micro image=ami-1420b57c group=default
failed: [127.0.0.1 -> 127.0.0.1] => {"failed": true}
msg: Instance creation failed => VPCResourceNotSpecified: The specified instance type can only be used in a VPC. A subnet ID or network interface ID is required to carry out the request.

Problem

I am trying to create an EC2 instance with an ansible playbook and set the root volume size with the playbook. Works great without the volumes variable included, of course. But, I want to set a different default size for the root volume. My playbook looks like this: ``` # Use the ec2 module to create a new host and then add # it to a special "ec2hosts" group. - hosts: localhost connection: local gather_facts: False vars: instance_type: "t2.micro" image: "ami-1420b57c" region: "us-east-1" volumes: - device_name: /dev/xvda volume_size: 10 tasks: - name: make one instance ec2: image="{{ image }}" instance_type="{{ instance_type }}" keypair="{{ keypair }}" region="{{ region }}" group="{{ group }}" volumes="{{ volumes }}" instance_tags='{"Name":"{{instance_name}}"}' wait=true register: ec2_host - debug: var=ec2_host - debug: var=item with_items: ec2_host.instance_ids - add_host: hostname={{ item.public_ip }} groupname=ec2hosts with_items: ec2_host.instances ``` And then when I run the playbook command, i get the following error. ``` ansible-playbook ec2-simple.yml -e "instance_name=testnode keypair=mykeypair group=testgroup" PLAY [localhost] ************************************************************** TASK: [make one instance] ***************************************************** failed: [localhost] => {"failed": true} msg: Device name must be set for volume FATAL: all hosts have already failed -- aborting ``` I've tried alternatives. With and without quotes etc., Nothing works. Sometimes, I get a different error. I'm using Ansible 1.7.2 on Mac.

Original source