ansible-collections / amazon.aws

Ansible Collection for Amazon AWS
GNU General Public License v3.0
304 stars 334 forks source link

Not able to create an instance using amazon.aws.ec2_instance #669

Closed priyadarshu closed 2 years ago

priyadarshu commented 2 years ago

Summary

EC2 instance is getting created using amazon.aws.ec2 but its been DEPRECATED

Hence the latest version is amazon.aws.ec2_instance

When I use the latest version i.e amazon.aws.ec2_instance, I'm getting the below error

Playbook Code:
---
- name: creating and launching new EC2 instances using ansible
  hosts: localhost
  connection: local
  gather_facts: false
  tags: creating_ec2_instance

  vars:
   instance_type: t2.nano
   image: ami-04505e74c0741db8d
   wait: yes
   count: 1
   region: us-east-1
   security_group:  priya_ansible_test_security_grp
   instance_name_new: priya_Auto_Test1
   keypairname: ansible_test_instance

  tasks:
    - name: Task#1 - Create and launch new EC2 instance
      amazon.aws.ec2_instance:
              key_name: "{{keypairname}}"
              instance_type: "{{instance_type}}"
              image: "{{image}}"
              wait: "{{wait}}"
              group: "{{security_group}}"
              region: "{{region}}"
              count: "{{count}}"
              instance_tags:
                 Name: "{{instance_name_new}}"
Screenshot 2022-02-11 at 11 07 07 AM

Issue Type

Bug Report

Component Name

amazon.aws.ec2_instance

Ansible Version

$ ansible --version

ansible [core 2.12.1]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/ubuntu/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0]
  jinja version = 2.10.1
  libyaml = True

Collection Versions

$ ansible-galaxy collection list

# /usr/lib/python3/dist-packages/ansible_collections
Collection                    Version
----------------------------- -------
amazon.aws                    2.1.0  
ansible.netcommon             2.5.0  
ansible.posix                 1.3.0  
ansible.utils                 2.4.3  
ansible.windows               1.9.0  
arista.eos                    3.1.0  
awx.awx                       19.4.0 
azure.azcollection            1.10.0 
check_point.mgmt              2.2.0  
chocolatey.chocolatey         1.1.0  
cisco.aci                     2.1.0  
cisco.asa                     2.1.0  
cisco.intersight              1.0.18 
cisco.ios                     2.6.0  
cisco.iosxr                   2.6.0  
cisco.ise                     1.2.1  
cisco.meraki                  2.6.0  
cisco.mso                     1.3.0  
cisco.nso                     1.0.3  
cisco.nxos                    2.8.2  
cisco.ucs                     1.6.0  
cloud.common                  2.1.0  
cloudscale_ch.cloud           2.2.0  
community.aws                 2.1.0  
community.azure               1.1.0  
community.ciscosmb            1.0.4  
community.crypto              2.1.0  
community.digitalocean        1.14.0 
community.dns                 2.0.4  
community.docker              2.1.1  
community.fortios             1.0.0  
community.general             4.3.0  
community.google              1.0.0  
community.grafana             1.3.0  
community.hashi_vault         2.2.0  
community.hrobot              1.2.2  
community.kubernetes          2.0.1  
community.kubevirt            1.0.0  
community.libvirt             1.0.2  
community.mongodb             1.3.2  
community.mysql               2.3.2  
community.network             3.0.0  
community.okd                 2.1.0  
community.postgresql          1.6.0  
community.proxysql            1.3.1  
community.rabbitmq            1.1.0  
community.routeros            2.0.0  
community.skydive             1.0.0  
community.sops                1.2.0  
community.vmware              1.17.0 
community.windows             1.9.0  
community.zabbix              1.5.1  
containers.podman             1.9.0  
cyberark.conjur               1.1.0  
cyberark.pas                  1.0.13 
dellemc.enterprise_sonic      1.1.0  
dellemc.openmanage            4.4.0  
dellemc.os10                  1.1.1  
dellemc.os6                   1.0.7  
dellemc.os9                   1.0.4  
f5networks.f5_modules         1.13.0 
fortinet.fortimanager         2.1.4  
fortinet.fortios              2.1.3  
frr.frr                       1.0.3  
gluster.gluster               1.0.2  
google.cloud                  1.0.2  
hetzner.hcloud                1.6.0  
hpe.nimble                    1.1.4  
ibm.qradar                    1.0.3  
infinidat.infinibox           1.3.3  
infoblox.nios_modules         1.2.1  
inspur.sm                     1.3.0  
junipernetworks.junos         2.8.0  
kubernetes.core               2.2.2  
mellanox.onyx                 1.0.0  
netapp.aws                    21.7.0 
netapp.azure                  21.10.0
netapp.cloudmanager           21.12.1
netapp.elementsw              21.7.0 
netapp.ontap                  21.14.1
netapp.storagegrid            21.9.0 
netapp.um_info                21.8.0 
netapp_eseries.santricity     1.2.13 
netbox.netbox                 3.5.0  
ngine_io.cloudstack           2.2.2  
ngine_io.exoscale             1.0.0  
ngine_io.vultr                1.1.0  
openstack.cloud               1.5.3  
openvswitch.openvswitch       2.1.0  
ovirt.ovirt                   1.6.6  
purestorage.flasharray        1.12.0 
purestorage.flashblade        1.9.0  
sensu.sensu_go                1.12.1 
servicenow.servicenow         1.0.6  
splunk.es                     1.0.2  
t_systems_mms.icinga_director 1.27.0 
theforeman.foreman            2.2.0  
vyos.vyos                     2.6.0  
wti.remote                    1.0.3  

AWS SDK versions

$ pip show boto boto3 botocore

Name: boto
Version: 2.49.0
Summary: Amazon Web Services Library
Home-page: https://github.com/boto/boto/
Author: Mitch Garnaat
Author-email: mitch@garnaat.com
License: MIT
Location: /home/ubuntu/.local/lib/python3.8/site-packages
Requires: 
Required-by: 
---
Name: boto3
Version: 1.20.46
Summary: The AWS SDK for Python
Home-page: https://github.com/boto/boto3
Author: Amazon Web Services
Author-email: None
License: Apache License 2.0
Location: /home/ubuntu/.local/lib/python3.8/site-packages
Requires: jmespath, s3transfer, botocore
Required-by: 

Configuration

$ ansible-config dump --only-changed

Playbook

---
- name: creating and launching new EC2 instances using ansible
  hosts: localhost
  connection: local
  gather_facts: false
  tags: creating_ec2_instance

  vars:
   instance_type: t2.nano
   image: ami-04505e74c0741db8d
   wait: yes
   count: 1
   region: us-east-1
   security_group:  priya_ansible_test_security_grp
   instance_name_new: priya_Auto_Test1
   keypairname: ansible_test_instance

  tasks:
    - name: Task#1 - Create and launch new EC2 instance
      amazon.aws.ec2_instance:
              key_name: "{{keypairname}}"
              instance_type: "{{instance_type}}"
              image: "{{image}}"
              wait: "{{wait}}"
              group: "{{security_group}}"
              region: "{{region}}"
              count: "{{count}}"
              count_tag:
                 Name: TestInstance
              instance_tags:
                 Name: "{{instance_name_new}}"

OS / Environment

Ubuntu

Steps to Reproduce

---
- name: creating and launching new EC2 instances using ansible
  hosts: localhost
  connection: local
  gather_facts: false
  tags: creating_ec2_instance

  vars:
   instance_type: t2.nano
   image: ami-04505e74c0741db8d
   wait: yes
   count: 1
   region: us-east-1
   security_group:  priya_ansible_test_security_grp
   instance_name_new: priya_Auto_Test1
   keypairname: ansible_test_instance

  tasks:
    - name: Task#1 - Create and launch new EC2 instance
      amazon.aws.ec2_instance:
              key_name: "{{keypairname}}"
              instance_type: "{{instance_type}}"
              image: "{{image}}"
              wait: "{{wait}}"
              group: "{{security_group}}"
              region: "{{region}}"
              count: "{{count}}"
              count_tag:
                 Name: TestInstance
              instance_tags:
                 Name: "{{instance_name_new}}"

Expected Results

I expect it to create an AWS EC2 instance but it didn't create it

Actual Results

ubuntu@ip-172-31-80-18:~/ansible_playbooks/create_lists$ ansible-playbook create_ec2_instance.yml -vvvv
ansible-playbook [core 2.12.1]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/ubuntu/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/ubuntu/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible-playbook
  python version = 3.8.10 (default, Nov 26 2021, 20:14:08) [GCC 9.3.0]
  jinja version = 2.10.1
  libyaml = True
Using /etc/ansible/ansible.cfg as config file
setting up inventory plugins
host_list declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
auto declined parsing /etc/ansible/hosts as it did not pass its verify_file() method
Parsed /etc/ansible/hosts inventory source with ini plugin
Loading collection amazon.aws from /usr/lib/python3/dist-packages/ansible_collections/amazon/aws
Loading callback plugin default of type stdout, v2.0 from /usr/lib/python3/dist-packages/ansible/plugins/callback/default.py
Skipping callback 'default', as we already have a stdout callback.
Skipping callback 'minimal', as we already have a stdout callback.
Skipping callback 'oneline', as we already have a stdout callback.

PLAYBOOK: create_ec2_instance.yml **********************************************************************
Positional arguments: create_ec2_instance.yml
verbosity: 4
connection: smart
timeout: 10
become_method: sudo
tags: ('all',)
inventory: ('/etc/ansible/hosts',)
forks: 5
1 plays in create_ec2_instance.yml

PLAY [creating and launching new EC2 instances using ansible] ******************************************
META: ran handlers

TASK [Task#1 - Create and launch new EC2 instance] *****************************************************
task path: /home/ubuntu/ansible_playbooks/create_lists/create_ec2_instance.yml:19
<local> ESTABLISH LOCAL CONNECTION FOR USER: ubuntu
<local> EXEC /bin/sh -c 'echo ~ubuntu && sleep 0'
<local> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/ubuntu/.ansible/tmp `"&& mkdir "` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1644558575.9813478-3640-220831920100205 `" && echo ansible-tmp-1644558575.9813478-3640-220831920100205="` echo /home/ubuntu/.ansible/tmp/ansible-tmp-1644558575.9813478-3640-220831920100205 `" ) && sleep 0'
<local> Attempting python interpreter discovery
<local> EXEC /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'python3.10'"'"'; command -v '"'"'python3.9'"'"'; command -v '"'"'python3.8'"'"'; command -v '"'"'python3.7'"'"'; command -v '"'"'python3.6'"'"'; command -v '"'"'python3.5'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'/usr/libexec/platform-python'"'"'; command -v '"'"'python2.7'"'"'; command -v '"'"'python2.6'"'"'; command -v '"'"'/usr/bin/python'"'"'; command -v '"'"'python'"'"'; echo ENDFOUND && sleep 0'
<local> EXEC /bin/sh -c '/usr/bin/python3.8 && sleep 0'
Using module file /usr/lib/python3/dist-packages/ansible_collections/amazon/aws/plugins/modules/ec2_instance.py
<local> PUT /home/ubuntu/.ansible/tmp/ansible-local-36344dv_56md/tmpozgsb2y1 TO /home/ubuntu/.ansible/tmp/ansible-tmp-1644558575.9813478-3640-220831920100205/AnsiballZ_ec2_instance.py
<local> EXEC /bin/sh -c 'chmod u+x /home/ubuntu/.ansible/tmp/ansible-tmp-1644558575.9813478-3640-220831920100205/ /home/ubuntu/.ansible/tmp/ansible-tmp-1644558575.9813478-3640-220831920100205/AnsiballZ_ec2_instance.py && sleep 0'
<local> EXEC /bin/sh -c '/usr/bin/python3 /home/ubuntu/.ansible/tmp/ansible-tmp-1644558575.9813478-3640-220831920100205/AnsiballZ_ec2_instance.py && sleep 0'
<local> EXEC /bin/sh -c 'rm -f -r /home/ubuntu/.ansible/tmp/ansible-tmp-1644558575.9813478-3640-220831920100205/ > /dev/null 2>&1 && sleep 0'
fatal: [local]: FAILED! => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3"
    },
    "changed": false,
    "invocation": {
        "module_args": {
            "availability_zone": null,
            "aws_access_key": null,
            "aws_ca_bundle": null,
            "aws_config": null,
            "aws_secret_key": null,
            "count": 1,
            "count_tag": {
                "Name": "TestInstance"
            },
            "cpu_credit_specification": null,
            "cpu_options": null,
            "debug_botocore_endpoint_logs": false,
            "detailed_monitoring": null,
            "ebs_optimized": null,
            "ec2_url": null,
            "filters": null,
            "group": "priya_ansible_test_security_grp",
            "image": "ami-04505e74c0741db8d",
            "image_id": null,
            "instance_ids": [],
            "instance_initiated_shutdown_behavior": null,
            "instance_role": null,
            "instance_tags": {
                "Name": "priya_Auto_Test1"
            },
            "instance_type": "t2.nano",
            "key_name": "ansible_test_instance",
            "launch_template": null,
            "metadata_options": null,
            "name": null,
            "network": null,
            "placement_group": null,
            "profile": null,
            "purge_tags": false,
            "region": "us-east-1",
            "security_group": null,
            "security_groups": [],
            "security_token": null,
            "state": "present",
            "tags": null,
            "tenancy": null,
            "termination_protection": null,
            "tower_callback": null,
            "user_data": null,
            "validate_certs": true,
            "volumes": null,
            "vpc_subnet_id": null,
            "wait": true,
            "wait_timeout": 600
        }
    },
    "msg": "argument 'image' is of type <class 'str'> and we were unable to convert to dict: dictionary requested, could not parse JSON or key=value"
}

PLAY RECAP *********************************************************************************************
local                      : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

Code of Conduct

ansibullbot commented 2 years ago

Files identified in the description: None

If these files are inaccurate, please update the component name section of the description or use the !component bot command.

click here for bot help

alinabuzachis commented 2 years ago

Hi @priyadarshu, thank you for tasing this. According to the ec2_instance module documentation, you should specify either image (which is a dictionary, see here https://github.com/ansible-collections/amazon.aws/blob/main/plugins/modules/ec2_instance.py#L108) or image_id (which is a string). I would probably suggest replacing image with image_id in your task, and that should do the job. Let us know if this solves your issue.

priyadarshu commented 2 years ago

Hey @alinabuzachis , thanks for letting me know, it's working now. I had changed the module name to the newest version but not the key value pairs.

Thanks again, close this issue.