Hey Techies…👋
In this blog, we’re going to discuss about Mastering Ansible Playbook Features: Part-3.
During this session, we will explore various examples of creating Ansible playbooks.
****************************************************************************
Prerequisites — 3 Virtual Machines
1. server — 1 CPU — 1GB RAM (Python 2.7) — Ansible Server
2. node1–1 CPU — 1GB RAM ( python 2.6 and above) — Ansible Client 1
3. node2–1 CPU — 1GB RAM ( python 2.6 and above) — Ansible Client 2
From ansible server login as an ansible user as per previous blog.
Please go through the lab-setup blog in case you missed it — https://medium.com/cloudnloud/setting-up-lab-environment-for-ansible-skillbuilder-series-7d41638815ed.
From ansible user execute below command:
ansible all -m ping
the above ping command should return with a ping / pong green color.
****************************************************************************
Take ansible ssh connection in 3 windows:
First session for vi editor.
Second session for execution of playbook.
Third session for ansible-doc window for referrals.
This practice is designed to help you gain expertise in Ansible through hands-on experience, emphasizing practical knowledge over rote memorization. By following this approach, you’ll develop a deep understanding of Ansible and enhance your troubleshooting skills, which is invaluable for real-world applications.
Here’s a breakdown of the playbook’s basic structure:
- “name”: A description of the playbook’s purpose.
- “hosts”: The target hosts or group of hosts where these tasks will be executed.
- “tasks”: A list of tasks to be performed on the specified hosts.
****************************************************************************
1.Example for installing multiple packages with a single task/play
The “ansible-doc yum” command is used to access the documentation for Ansible’s “yum” module. This command provides detailed information about how to use the “yum” module in Ansible playbooks, including its parameters, options, and usage examples. It’s a valuable resource for Ansible users looking to work with package management using YUM on Linux systems.
[ansible@server dev]$ ansible-doc yum module
To check if the Apache HTTP Server (httpd) is installed on “node1,” you can log in to that server and run the following command in your terminal or SSH session:
[root@node1 ~]# yum list httpd wget unzip
If yes, uninstall it and see the changes by executing the below file.
[root@node1 ~]# yum remove wget unzip httpd -y
Do the same on Node2 as well.
The below Ansible playbook is to be intended to install packages (httpd, wget, and unzip) on all the target nodes.
vim package.yaml
---
- name: I am going to install few packages
hosts: all
tasks:
- name: installing packages
yum:
name:
- httpd
- wget
- unzip
state: latest
To verify, check the below command in node1 and node2
[root@node1 ansibleseries]# yum list httpd wget unzip
[root@node2 ansibleseries]# yum list httpd wget unzip
****************************************************************************
2. Example playbook for 2 plays or 2 tasks
Create a service.yaml file in ansible server and run the playbook:
This Ansible playbook is designed to perform two tasks:
- Install the latest version of the “httpd” package using the “yum” module.
- Restart the web service, which in this case is “httpd,” using the “service” module.
vim service.yaml
---
- name: install httpd
hosts: all
tasks:
- name: install httpd
yum:
name: httpd
state: latest
- name: restart web service
service:
name: httpd
state: started
To perform the tasks you’ve described, you can create an Ansible playbook, such as “service.yaml,” to check the status of the Apache HTTP Server (httpd) on “node1” and “node2,” stop it if it’s running, and then check its status again.
[root@node1 ansibleseries]# systemctl status httpd
[root@node1 ansibleseries]# systemctl stop httpd
Then execute the below command to run the playbook:
[ansible@server dev]$ ansible-playbook service.yaml
****************************************************************************
3. Example Playbook for 4 plays or 4 tasks
Type 1: Simple playbook
This Ansible playbook is designed to perform 4 tasks:
- Install the latest version of the “httpd” package using the “yum” module.
- Restart the web service, which in this case is “httpd,” using the “service” module.
- Installing the “vsftpd” package using the “yum” module.
- Restarting the “vsftpd” service using the “service” module.
vim httpd.yaml
---
- name: install httpd
hosts: all
tasks:
- name: install httpd
yum:
name: httpd
state: latest
- name: restart web service
service:
name: httpd
state: restarted
- name: install vsftpd
yum:
name: vsftpd
state: latest
- name: restart web service
service:
name: vsftpd
state: restarted
Type 2: get_url method
Utilizing Ansible’s get_url Module for Efficient File Downloads
[ansible@server dev]$ ansible-doc get_url
Create a file called url.yaml and paste the below script into the file:
This Ansible Playbook is designed to perform below tasks:
- “httpd install”: This task uses the “yum” module to ensure that the “httpd” package is installed and is in the latest state.
- “httpd service start”: This task uses the “service” module to start the “httpd” service.
- “enable the service”: This task uses the “service” module to enable the “httpd” service to start automatically on boot.
- “download the httpd.conf”: This task uses the “get_url” module to download the Ansible PDF from the specified URL and save it as “ansible.pdf” in the “/var/www/html” directory with the specified file mode.
url.yaml
---
- name: httpd install
hosts: node1,node2
tasks:
- name: httpd install
yum:
name: httpd
state: latest
- name: httpd service start
service:
name: httpd
state: started
- name: enable the service
service:
name: httpd
enabled: yes
- name: download the httpd.conf
get_url:
url: https://riptutorial.com/Download/ansible.pdf
dest: /var/www/html/ansible.pdf
mode: 0644
Run the playbook by executing below command :
[ansible@server dev]$ ansible-playbook url.yaml
Then login to target nodes and check the file has been created in destination:
Now take the IP address of your target machine and check the url is working or now as shown below:
****************************************************************************
4.Example playbook create a module called group and user (adding group and user).
Check the examples of group in ansible-doc section.
[ansible@server dev]$ ansible-doc group
Check the examples of userin ansible-doc section.
[ansible@server dev]$ ansible-doc user
Create group.yaml
This Ansible Playbook is designed to perform below tasks:
- “Create group”: This task uses the “group” module to create a group named “admin” with the state set to “present,” which means it will be created if it doesn’t exist.
- “Create user”: This task uses the “user” module to create a user named “athira” with additional information provided in the “comment” field. The user is added to the “admin” group.
group.yaml
---
- name: Creating users and groups
hosts: all
tasks:
- name: Create group
group:
name: admin
state: present
- name: Create user
user:
name: athira
comment: Athira is learning Ansible
group: admin
Now run the playbook:
[ansible@server dev]$ ansible-playbook group.yaml
Creating a group named “admin” and a user named “Athira” in the /etc/group and /etc/passwd files on the target nodes. This is a common administrative task in Unix-like operating systems for managing user accounts and groups. These files store user and group information.
- The “/etc/passwd” file contains user account information, including usernames, user IDs (UIDs), group IDs (GIDs), home directories, and default shells.
- The “/etc/group” file stores information about user groups, including group names and GIDs.
This information is typically added manually or through automated scripts as part of user and group management on a Unix-based system. If you have any specific questions or tasks related to user and group management, feel free to ask for further assistance.
There is a minor change in the script below: groups have been put in place of groups at the last line.
This task uses the “user” module to create a user named “athira.” The “comment” field provides additional information about the user, and the “groups” parameter adds the user to the “admin” group.
group.yaml
---
- name: Creating users and groups
hosts: all
tasks:
- name: Create group
group:
name: admin
state: present
- name: Create user
user:
name: athira
comment: Athira is learning Ansible
groups: admin
Now run the playbook again:
[ansible@server dev]$ ansible-playbook group.yaml
Go to target server and check the difference in /etc/group output
The user “athira” is added to the “admin” group. This ensures that the user “athira” is a member of the “admin” group, allowing them to have the group’s privileges and access rights.
💹Next day — Mastering Ansible Handlers: Streamlining Playbook Automation: Part-1.
⭐⭐⭐ Enjoy your learning….!!! ⭐⭐⭐