In this blog, we explained how to write the ansible first playbook in which to print the hostname, host IP, and ansible version of localhost, and execute the playbook. We have created a structure of the ansible first playbook in which we added the task folder and two files, which are hosts and sample.yml. The ansible first playbook structure is easy to understand. After a few blogs, we will create the proper structure of the ansible-playbook and learn how to use a variable, conditions, loops, etc. Now, this is a simple example of an ansible first playbook.
We tried to explain every parameter in the ansible-playbook, which is written in YAML. Also, provided the ansible installation and ansible basic command links, including the git hub link to download the ansible-playbook repository. Understanding of YAML file structure is required to write the ansible-playbook.
Topics cover in Ansible First Playbook:
– What is an Ansible Playbook?
– What is a YAML file in an Ansible?
– How to write an ansible first playbook and What are the hosts and
sample.yml files?
– Explain the parameters in the hosts and sample.yml files.
– How to run an ansible first playbook?
Note:
Check the following link to install ansible:
– Install ansible
Check the following link for the Ansible basic command:
– Ansible basic command
Download the ansible-playbook from git.# git clone https://github.com/smarttechfunda/ansible-playbook.git
Go to folder first-sample-playbook under the ansible-playbook folder and run the following command.# cd ansible-playbook/first-sample-playbook
# ansible-playbook -i hosts sample.yml
OR
Follow the below manual method and understand how to write and run the first ansible-playbook.
What is an Ansible Playbook?
The Playbook contains a set of tasks or instructions to execute on the instance. The Playbook writes in the YAML file, which is human-readable.
In simple language, ansible code writes in the YAML files, and these YAML files are called a playbook.
For example, in any programming language, we write a script, and in the script we use code or a set of instructions like conditions, loops, cases, etc in a function to execute on any instance, like that in an Ansible Playbook, we also write conditions, loops, cases, etc. The Ansible Playbook code is human-readable and easy to write. We will check in the upcoming topic of how to write the simple Playbook in which we will use the variable, conditions, loops, cases, and advanced playbooks.
What is a YAML file in Ansible Playbook?
The YAML file is a key-value pair representation.
Ansible uses the YAML files. The YAML file format is human-readable.
Easy to write and understand. For more details click the YAML file.
How to write an ansible first playbook and What are the hosts and sample.yml files?
Simple Ansible Playbook Structure:
Run the following command to create the task folder:
# mkdir task
Run the following command to create the hosts and sample.yml (you can change the name of the sample.yml file to main.yml file. In this example we are using the sample.yml name.) files in the task folder. Also, you can create a “hosts” file outside the task folder or inside the task folder.
# cd task
# touch hosts sample.yml
Copy and paste the code to the hosts, sample.yml files.
OR
You can write the code of hosts and sample.yml file. It will help you to understand the structure of the host and sample.yml file.
What are the “task” folder and “hosts, sample.yml (main.yml)” files?
task:
The task is the folder, in which hosts and sample.yml files are created.
The task name denotes the task or steps to perform.
hosts:
The “hosts” file is in an inventory of the hosts group. These are two “hosts” files. For the individual playbook hosts file and another is on path /etc/ansible/hosts file which is a global file for all playbooks. In this example, we are using the individual playbook hosts file. The “hosts” file contains the IP, hostname, and different groups of hosts.
Create the number of group hosts for different purposes. For example, install tomcat, Postgres, and elasticsearch on several instances. Now check the below format. In this format group of 3 IPs for tomcat, a group of 2 IPs for the Postgres database, and a group of 2 IPs for the elasticsearch. According to a set of instructions in the sample.yml file tomcat, Postgres, and elasticsearch will install on their instances. [tomcat]
192.168.1.10
192.168.1.12
192.168.1.39
[postgres]
192.168.1.20
192.168.1.32
[elasticsearch]
192.168.1.43
192.168.1.21
Check the following code of the “hosts” file in the task folder. We used the localhost instead of IP or hostname and host1 as the group name of the hosts. You can use the IP or hostname instead of localhost. If you use the hostname, then it should be resolvable by DNS, OR if you use IP, then it should be reachable and configured passwordless ssh on hostname OR IP in both cases. For passwordless ssh check the automate-the-installation-of-ansible-on-centos-8 Step 6.
[host1]
localhost
main.yml or sample.yml:
The main.yml or sample.yml is a playbook, in which we can write the code or set of instructions to execute on any instance.
Check the following sample.yml file code in the task folder:
---
# This is sample.yml file and execute the
# commands ansible --version, hostname, and hostname -I on the instance.
# The host1 is group of hosts (group of IPs, hostnames) which is specified in the hosts file in the task folder
- hosts: host1
tasks:
- name: Example-1 Print Message
debug:
# Print the following message.
msg: "This is print message."
- name: Example-2 Ansible Version
# The following command gets ansible version and store in the variable called "ansibleversion."
command: "ansible --version"
register: ansibleversion
- debug:
# Print the variable "ansibleversion" value. This value is an ansible version.
msg: "Ansible Version: {{ ansibleversion.stdout }}"
- name: Example-3 Hostname
# The following command gets the hostname of the instance and store in the variable called "currenthostname."
command: "hostname"
register: currenthostname
- debug:
# Print the variable "currenthostname" value. This value is the hostname of the instance.
msg: "The hostname: {{ currenthostname.stdout }}"
- name: Example-4 Host IP
# The following command gets IP of the instance and store in the variable called "hostIP."
command: "hostname -I"
register: hostIP
- debug:
# Print the variable "hostIP" value. This value is the host IP of the instance.
msg: "The host IP: {{ hostIP.stdout }}"
Explain every parameter in the main.yml or sample.yml file.
The file starts with the hyphen sign “-“. In front of the hyphen sign “-” there are keywords like hosts, name, debug, and after keywords, we have written values of it.
OR
You can start with three hyphen signs “-” like —- and use the hash “#” to comment on the code. In front of hash “#” you can write the purpose of the main.yml or sample.yml file. If you want to comment on the code then use the hash “#” in front of the comment then ansible will ignore the comment statement and execute the rest code.
Explain the parameters in the hosts and sample.yml files:
hosts:
In the main.yml or sample.yml file, the “hosts” is a keyword and its value is IP or hostname or group of IPs. The “hosts” file is an inventory file of a group of hosts.
tasks:
The tasks which execute the modules like debug, commands, etc.
name:
Lable of the task.
debug:
The debug is a module to print messages during the execution for debugging the tasks.
command:
The command is a module that executes the commands like Linux commands, etc.
register:
The register is a module to store the variable value in the playbook. Use the debug module to print variable values stored in the variable.
Note:
Run the following command to check the Ansible modules.# ansible-doc -l
How to run an ansible first playbook?
Run the following command to run the Ansible Playbook:# cd task
# ansible-playbook -i hosts sample.yml
Command explanation:
Go to the task folder and run the ansible-playbook command to execute the playbook.
ansible-playbook:
The ansible-playbook is an ansible command to run the playbook.
-i:
The “-i” option is for the inventory file. Specify the inventory file path which is you are using an individual playbook hosts file or /etc/ansible/hosts file.
hosts:
The “hosts” file is an inventory file of the hosts group.
sample.yml:
The sample.yml file is an ansible-playbook in which an ansible set of instructions or code is written.
Check the following output of the command:
No comments! Be the first commenter?