skip to Main Content

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

2

Answers


  1. 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
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search