skip to Main Content

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


  1. Your playbook and your hosts group name does not match.

    In aws.yml

    - hosts: webserver
    

    And in /etc/ansible/hosts

    [web]
    

    You should either change your aws.yml playbook to read

    - hosts: web
    

    Or your hosts file /etc/ansible/hosts to read

    [webserver]
    
    Login or Signup to reply.
  2. Too many -s. Remove the ones in front of tasks and vars.

    Also, use delegate_to: localhost on the ec2 task:

    ---
    - 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
        delegate_to: localhost
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search