ANSIBLE TUTORIALS #2: GROUPS

Chigozie Oziri
6 min readMay 18, 2023

--

Cover photo

Sequel to my previous article, ANSIBLE TUTORIALS #1, we are going to learn how to execute the following tasks on 1 CentOS and 3 Ubuntu machines in this tutorial.

Tasks

  1. In your inventory section create THREE groups, and name them: webserver, centos, and parent. Place the Ubuntu machines in the webserver group, the centos machine in the centos group, and all the machines in the parent group.
  2. Using the appropriate group, install and start Apache on the CentOS machine.
  3. Install and start NGINX on the Ubuntu group.
  4. Write another task to update the Centos and Ubuntu servers using the parent group you created, and install PHP. Run this as a pre-task.

Just joining us? Please follow my previous blog on Building Virtual Machines for Home Lab to learn how to spin up VMs, so that you can follow this tutorial. If you have done that, 👋 welcome back!

# 1: Creating webservers, centos, and parent groups

Ansible inventory grouping makes it easier to target a specific set of VMs in our Ansible playbook.

From your control machine, run,

vagrant@ubuntu-control:~$ mkdir ansible_tasks2 
vagrant@ubuntu-control:~$ cd ansible_tasks2
vagrant@ubuntu-control:~/ansible_tasks2$ vi hosts

and copy the code block below and paste it into your text editor:

[webservers]
ubuntu-node-1
ubuntu-node-2
ubuntu-node-3

[centos]
centos-node-1

[parent]
ubuntu-node-1
ubuntu-node-2
ubuntu-node-3
centos-node-1

Writing our inventory file this way, we would have to create a host variable file for each host. These files must be named according to the hostnames declared in our inventory file, and located in a directory conventionally named, “host_vars”.

vagrant@ubuntu-control:~/ansible_tasks2$ mkdir host_vars
vagrant@ubuntu-control:~/ansible_tasks2$ cd host_vars
vagrant@ubuntu-control:~/ansible_tasks2/host_vars$ touch ubuntu-node-1.yml ubuntu-node-2.yml ubuntu-node-3.yml centos-node-1.yml

We have created all our host variable files at once using the touch command, however, they are all empty. So we edit each of them and enter the variables we need to declare. We are going to be using a method that makes it easy for us to edit these files. On the command line, run

vagrant@ubuntu-control:~/ansible_tasks2/host_vars$ sudo vim ./* 

When you enter this command, the Vim editor opens in such a way that you can each file in the host_var directory one after the other without having to exit the text editor. So check the name of the file currently open. If it is any of our Ubuntu host variable files, enter the lines below after pressing “i” to enter the insert mode.

ansible_host: 192.168.53.22
apache: apache2
php: libapache2-mod-php
sudo_group: sudo

Press the button to enter Vim’s command mode, type “:w” and press Enter to save your file, and type in “:next” and press Enter to go to the next file (“:previous” will take you to a previous file). Enter the same content for all the Ubuntu host variable files except that you have to change the IP address for each. When you get to the centos-node-1.yml file, enter the following content:

ansible_host: 192.168.53.61
apache: httpd
php: php
sudo_group: wheel

Once you are done editing all four host variable files, Esc Vim’s INSERT mode to enter the command mode and type “:q” and press Enter to exit the editor.

# 2: Using the appropriate group, install and start Apache on the CentOS machine

So because of our inventory groups, our playbook will look a little different from the playbook in our previous tutorial.

---
- name: Install Apache on CentOS servers
hosts: centos

tasks:

- name: Installing Apache
tags: install_apache_centos
package:
name: "{{ apache }}"
state: latest

We have written our play in a sort of universal format as this would work even if we target both Ubuntu and CentOS servers or CentOS alone. However, since our target group is CentOS, we may simply write our code as:

---
- name: Install Apache on CentOS servers
hosts: centos

tasks:

- name: Installing Apache
tags: install_apache_centos
yum:
name: httpd
state: latest

Hope you can spot the difference. Next, we include a task to get Apache active and running.

---
- name: Install and Start Apache on CentOS servers
hosts: centos

tasks:

- name: Installing Apache
tags: install_apache_centos
yum:
name: httpd
state: latest

- name: Starting Apache
tags: start_apache
service:
name: httpd
state: started
enabled: yes

When we visit the IP address of our CentOS VM on our web browser, we should see the default HTTPD web page as shown below:

snapshot of httpd default web page

# 3: Install and start NGINX on the Ubuntu group

Since we would be targeting only the Ubuntu servers in this particular task, we would use the apt module to install NGINX.

- name: Install and Start NGINX on Webservers
hosts: webservers

tasks:

- name: Installing NGINX
tags: install_nginx
apt:
name: nginx
state: latest
update_cache: yes

- name: Start NGINX
tags: start_nginx
service:
name: nginx
state: started

So bringing our playbook together we have:

---
- name: Install and Start Apache on CentOS servers
hosts: centos

tasks:

- name: Installing Apache
tags: install_apache_centos
yum:
name: httpd
state: latest

- name: Starting Apache
tags: start_apache
service:
name: httpd
state: started
enabled: yes


- name: Install and Start NGINX on Webservers
hosts: webservers

tasks:

- name: Installing NGINX
tags: install_nginx
apt:
name: nginx
state: latest
update_cache: yes

- name: Start NGINX
tags: start_nginx
service:
name: nginx
state: started

Copy and paste any of your Ubuntu servers’ IP addresses on your web browser, and you would see the default NGINX welcome web page.

# 4: Write another task to update the Centos and Ubuntu servers using the parent group you created, and install PHP. Run this as a pre-task

Finally, we get the opportunity to write a task that targets both Ubuntu and CentOS groups. Therefore, we must write our play because we are dealing with servers with different Operating Systems, hence we would be using the package module, the host variable for our PHP package.

- name: Update hosts and Install PHP
hosts: parent

pre_tasks:

- name: Updating servers
tags: always
package:
update_cache: yes
state: latest

- name: Installing PHP
tags: install_php
package:
name: "{{ php }}"
state: latest

Running this play as a pre-task means Ansible will run the tasks in this play first before that of other plays. So, bringing it all together, we have:

---
- name: Update and Install PHP
hosts: parent

pre_tasks:

- name: Updating servers
tags: always
package:
update_cache: yes
state: latest

- name: Installing PHP
tags: install_php
package:
name: "{{ php }}"
state: latest


- name: Install and Start Apache on CentOS servers
hosts: centos

tasks:

- name: Installing Apache
tags: install_apache_centos
yum:
name: httpd
state: latest

- name: Starting Apache
tags: start_apache
service:
name: httpd
state: started
enabled: yes


- name: Install and Start NGINX on Webservers
hosts: webservers

tasks:

- name: Installing NGINX
tags: install_nginx
apt:
name: nginx
state: latest
update_cache: yes

- name: Start NGINX
tags: start_nginx
service:
name: nginx
state: started

Conclusion

We come to the end of our second Ansible tutorial. In our next tutorial, we would be implementing Ansible roles to execute tasks for installing Terraform, Jenkins, Docker, etc. Stay tuned!

Get the full resources for this tutorial from our GitHub repository. For any questions or observations, please chat with me on WhatsApp.

--

--

Chigozie Oziri
Chigozie Oziri

No responses yet