Cifrax777
Cifrax777

Reputation: 41

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

Upvotes: 2

Views: 4818

Answers (2)

F1ko
F1ko

Reputation: 4274

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

Upvotes: 4

rpm192
rpm192

Reputation: 2464

The YAML in your playbook is not correct. Restructure it like below, notice the - that I removed. It is also a best-practise to always define the state parameter.

---

- hosts: redhat
  remote_user: root
  tasks:
    - name: Install apache
      yum:
        name: httpd
        state: present

You also have to specify the inventory file from which Ansible should get the hosts. If the playbook is within the same folder as your hosts file, you can use the command below to run the playbook.

$ ansible-playbook playbook.yml -i hosts

If the inventory file is in another location, specify the entire path instead of just the filename.

Upvotes: 1

Related Questions