Hi I am new to Ansible. Can anyone please help me with my script.
I get the error when I run the below script. I am able to create the instance however unable to terminate.
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: <Response><Errors><Error><Code>MissingParameter</Code><Message>The request must contain the parameter InstanceId</Message></Error></Errors><RequestID>8e1039f2-b905-4d6b-b727-4395feb9310c</RequestID></Response>
fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "module_stderr": "Traceback (most recent call last):\n File \"/tmp/ansible_7eh91W/ansible_module_ec2.py\", line 1725, in <module>\n main()\n File \"/tmp/ansible_7eh91W/ansible_module_ec2.py\", line 1693, in main\n (changed, instance_dict_array, new_instance_ids) = terminate_instances(module, ec2, instance_ids)\n File \"/tmp/ansible_7eh91W/ansible_module_ec2.py\", line 1354, in terminate_instances\n for res in ec2.get_all_instances(instance_ids):\n File \"/usr/lib/python2.7/site-packages/boto/ec2/connection.py\", line 585, in get_all_instances\n max_results=max_results)\n File \"/usr/lib/python2.7/site-packages/boto/ec2/connection.py\", line 681, in get_all_reservations\n [('item', Reservation)], verb='POST')\n File \"/usr/lib/python2.7/site-packages/boto/connection.py\", line 1186, in get_list\n raise self.ResponseError(response.status, response.reason, body)\nboto.exception.EC2ResponseError: EC2ResponseError: 400 Bad Request\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Response><Errors><Error><Code>MissingParameter</Code><Message>The request must contain the parameter InstanceId</Message></Error></Errors><RequestID>8e1039f2-b905-4d6b-b727-4395feb9310c</RequestID></Response>\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 0}
---
- name: Create a new Demo EC2 instance
hosts: localhost
connection: local
gather_facts: False
vars:
region: ap-southeast-2
instance_type: t2.micro
ami: ami-8536d6e7 # Amazon Linux
keypair: awstestkeypair # pem file name
tasks:
- name: Create an ec2 instance
ec2:
key_name: "{{ keypair }}"
group: launch-wizard-1 # security group name
instance_type: "{{ instance_type}}"
image: "{{ ami }}"
wait: true
region: "{{ region }}"
exact_count: 1 # default
count_tag:
Name: TestPC
instance_tags:
Name: TestPC
vpc_subnet_id: subnet-cdd7f1a9
assign_public_ip: yes
register: ec2
- name: Wait for SSH to come up
wait_for:
host: "{{ item.public_dns_name }}"
port: 22
delay: 60
timeout: 320
state: started
with_items: "{{ ec2.instances }}"
- name: Terminate instances that were previously launched
ec2:
state: absent
region: "{{ region }}"
instance_ids: "{{ ec2.instance_ids }}"