Search code examples
linuxansiblecentosredhat

ANSIBLE “ERROR! the field 'hosts' is required but was not set”


I have a CENTOS 7 VM with ansible installed, and I am trying to install the HTTPD service with ansible on a RED HAT 8.

File content:

"hosts"

[ubuntuserver]
192.168.1.51
[redhat]
192.168.56.102

"playbook.yaml"

[root @ centos7 ansible] # cat playbook.yaml
---
- hosts: redhat
- remote_user: root
   tasks:
      - name: install apache
        yum: name = httpd
[root @ centos7 ansible] #

Error I get:

error


Solution

  • First of all, indent everything on the same level:

    - hosts: redhat
      remote_user: root
       tasks:
          - name: install apache
            yum: name = httpd
    

    Note that there is only one - being used.

    Secondly, the hosts file you created is called an inventory. By executing ansible-playbook playbook.yaml it is going to use the default inventory file located at /etc/ansible/hosts which may not even exist on your machine.

    So all you have to do is to tell ansible to load in your inventory file by using the -i option:

    ansible-playbook -i hosts plabook.yaml