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:
2
Answers
First of all, indent everything on the same level:
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: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.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.If the inventory file is in another location, specify the entire path instead of just the filename.