I created a playbook aws.yml and want to run it over my local host
---
- hosts: webserver
- vars:
region: ap-south-1
instance_type: t2.micro
ami: ami-005956c5f0f757d37 # Amazon linux LTS
keypair: ansible # pem file name
- tasks:
- ec2:
key_name: "{{ ansible }}"
group: ansible # security group name
instance_type: "{{ t2.micro }}"
image: "{{ ami-005956c5f0f757d37 }}"
wait: true
wait_timeout: 500
region: "{{ ap-south-1 }}"
count: 1 # default
count_tag:
Name: Prod-Instance
instance_tags:
Name: Prod-Instance
vpc_subnet_id: subnet-00efd068
assign_public_ip: yes
Content of the host file /etc/ansible/hosts
[web]
localhost
When I try to run my aws.yml, it gives the below error
[root@localhost ~]# ansible-playbook aws.yml
PLAY [web] ********************************************************************************************************************************************************************************************************
ERROR! the field 'hosts' is required but was not set
[root@localhost ~]#
2
Answers
Your playbook and your hosts group name does not match.
In aws.yml
And in /etc/ansible/hosts
You should either change your aws.yml playbook to read
Or your hosts file /etc/ansible/hosts to read
Too many
-
s. Remove the ones in front oftasks
andvars
.Also, use
delegate_to: localhost
on theec2
task: