How to Create Ansible Playbook to Install Apache Server

To Create Ansible Playbook to Install Apache Server

Apache is a web server that is used on the Internet. It is easy to set up and configure on Linux distributions including Ubuntu and Debian, as it comes in the package repositories and includes a default configuration that works out of the box. This video will cover the installation of an apache server using Ansible.

Installation process:

Create a playbook to isntall apache server on your host machine

[root@localhost ansible]# vim httpd.yml 

After creating playbook now let’s run this using the following command

[root@localhost ansible]# ansible-playbook httpd.yml 
PLAY [httpd is there] *********************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************
ok: [192.168.6.111]
TASK [ensure httpd is installed] **********************************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop 
to supply multiple items and specifying `pkg: "{{item}}"`, please use `pkg: ['nano', 'httpd']` and remove the loop. This 
feature will be removed from ansible-base in version 2.11. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
changed: [192.168.6.111] => (item=['nano', 'httpd'])

PLAY RECAP ********************************************************************************************************************
192.168.6.111              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
Now I am going to uninstall the httpd from our host machine using the following playbook
[root@localhost ansible]# vim httpd.yml

And now run this playbook to uninstall the apache from the host machine.

[root@localhost ansible]# ansible-playbook httpd.yml 
PLAY [httpd is there] *********************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************
ok: [192.168.6.111]
TASK [ensure httpd is installed] **********************************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop 
to supply multiple items and specifying `pkg: "{{item}}"`, please use `pkg: ['nano', 'httpd']` and remove the loop. This 
feature will be removed from ansible-base in version 2.11. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
changed: [192.168.6.111] => (item=['nano', 'httpd'])

PLAY RECAP ********************************************************************************************************************
192.168.6.111              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Now in case if you want to install multiple application packages on your host machine using ansible

Now run this playbook to install apache and nano to install on your host machine

[root@localhost ansible]# ansible-playbook httpd.yml%%

PLAY [httpd is there] *********************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************
ok: [192.168.6.111]
TASK [ensure httpd is installed] **********************************************************************************************
[DEPRECATION WARNING]: Invoking "yum" only once while using a loop via squash_actions is deprecated. Instead of using a loop 
to supply multiple items and specifying `pkg: "{{item}}"`, please use `pkg: ['nano', 'httpd']` and remove the loop. This 
feature will be removed from ansible-base in version 2.11. Deprecation warnings can be disabled by setting 
deprecation_warnings=False in ansible.cfg.
changed: [192.168.6.111] => (item=['nano', 'httpd'])

PLAY RECAP ********************************************************************************************************************
192.168.6.111              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

With this method installation of apache on ansible host machine comes to an end.

FAQ
Q
How do I see all the variables specific to my host?
A
Using this command

ansible -m debug -a "var=hostvars['hostname']" localhost
Q
How do I see a list of all of the ansible_ variables?
A
To see a list of all of the facts that are available about a machine, you can run the setup module as an ad-hoc action:
ansible -m setup hostname
Q
How to unistall any application in host machine using ansible
A
you can use status=absent in ansible playbook to uninstall apny application.
Q
How to install multiple application in host machine using ansible?
A
To install multiple application you have to use loop insid the playbook.
Q
What is inventory file in ansible?
A
The Ansible inventory file defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate.