Search This Blog

Saturday 27 February 2021

Ansible playbook for apache installation using yum module

Step 1) Create a playbook

 [osboxes@master ansible-playbooks]$ cat>apache-install.yml
---
- hosts: all
  become: true
  become_user: root
  tasks:
       - name: Install httpd package
         yum:
           name: httpd
           state: present

       - name: Create a index.html
         copy:
           content: "Test page"
           dest: /var/www/html/index.html

       - name: Start httpd service
         service:
           name: httpd
           state: started
           enabled: true

- name: Test the webserver URL
  hosts: all
  become: no
  tasks:
    - name: Connect to http://<hostname>
      uri:
        url: http://localhost
        return_content: yes
        status_code: 200


Step 2) Verify the syntax

[osboxes@master ansible-playbooks]$ ansible-playbook apache-install.yml -i inventory.txt --syntax-check

playbook: apache-install.yml

Step 3) Execute the playbook with --step option to run to verify on the remote host after each step. 

[osboxes@master ansible-playbooks]$ ansible-playbook apache-install.yml -i inventory.txt -K --step
BECOME password:

PLAY [Apahce webserver installation] ************************************************************************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: **************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [linuxhost]
Perform task: TASK: Install httpd package (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Install httpd package (N)o/(y)es/(c)ontinue: ********************************************************************************************

TASK [Install httpd package] ********************************************************************************************************************************
changed: [linuxhost]
Perform task: TASK: Create a index.html (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Create a index.html (N)o/(y)es/(c)ontinue: **********************************************************************************************

TASK [Create a index.html] **********************************************************************************************************************************
changed: [linuxhost]
Perform task: TASK: Start httpd service (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Start httpd service (N)o/(y)es/(c)ontinue: **********************************************************************************************

TASK [Start httpd service] **********************************************************************************************************************************
changed: [linuxhost]

PLAY [Test the webserver URL] *******************************************************************************************************************************
Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Gathering Facts (N)o/(y)es/(c)ontinue: **************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************
ok: [linuxhost]
Perform task: TASK: Connect to http://<hostname> (N)o/(y)es/(c)ontinue: y

Perform task: TASK: Connect to http://<hostname> (N)o/(y)es/(c)ontinue: *************************************************************************************

TASK [Connect to http://<hostname>] *************************************************************************************************************************
ok: [linuxhost]

PLAY RECAP **************************************************************************************************************************************************
linuxhost                  : ok=6    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

No comments: