Create and Run Ansible Playbook File - Best Method ?

Ansible is a popular server configuration management tool that lets users manage and monitor remote systems from a single control node. With Ansible, you can install software packages, deploy services, and make configurations on multiple hosts from a single node instead of logging into each of the nodes. 

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Server Monitoring Tasks.

In this context, we shall look into what playbooks are, how to create them, and use them to deploy services.

Previously, we described how to install and set up Ansible on Ubuntu 20.04


Prerequisite to Create Ansible Playbook File ?

To complete this Configuration task, you need to have the following ready.

  • Ansible control node IP: 192.168.2.101
  • Managed host IP: 192.168.2.102


What is a playbook file?

Ansible, just like Terraform, falls under the Infrastructure as a Code. What does this mean? Infrastructure as a Code (IaC) is described as a mechanism of provisioning and managing hosts using machine-readable configuration files as opposed to physically logging in and making the configurations. In Ansible, a playbook is one such configuration file.

A playbook is a file in YAML that contains one or more plays.

What is a play? A play is an ordered task that automates a task or process on the managed host such as deploying an application such as a web server or making configurations. A playbook can have one or multiple plays, each performing different tasks.

Plays make use of modules which are special functions to specify the changes required on the remote host. Each module is special and defines a particular task.

A playbook file is saved with a .yml or .yaml file extension.


How to Create a playbook file ?

Here, we will create a playbook file called greetings.yml in the Ansible directory path /etc/ansible as below:

$ sudo vim /etc/ansible/greetings.yml

Add the following configuration. This is a simple playbook that prints a message to stdout on the remote server. Take careful note of the indentation of the modules.

The Ansible Playbook file begins with three hyphens ( — ) to indicate that it is a YAML file. The 'hosts' parameter specifies the remote host or group of hosts defined in the inventory file, which by default is located in /etc/ansible/hosts. Here, staging is the host group for which the remote host of IP 192.168.2.102 is defined.

The remote host is defined under the host group called staging with the following entries:

[staging]
192.168.2.102 ansible_ssh_pass=xxxxxxxx ansible_ssh_user=john

The ansible_ssh_pass specifies the SSH password of the remote user while ansible_ssh_use specifies the user name on the remote host.

Next, we have the name of the play "Print a simple message" followed by the debug module that prints out the message defined by the msg module.


How to run the playbook file ?

To execute the playbook, simply use the ansible-playbook command in the syntax provided below:

$ ansible-playbook /path/to/playbook-file

In our case, this is going to be:

$ ansible-playbook /etc/ansible/greetings.yml

During play execution, Ansible first prints out the name of the host group or remote host on which the play will be executed -in our case the staging group. Ansible then retrieves information about the play, referred to as Facts, and finally performs the action specified in the playbook. Here, the simple message is printed.

Let's take yet another example of a playbook file called install_apache_and_git.yml. Here, we have two plays. The first play installs the Apache webserver while the second play installs git on the remote system.

The become: true parameter executes the command as an elevated user or sudo user on the remote user as is expected.

When the playbook is executed, all the plays are listed in order of execution from the first to the last. The playbook first installs the Apache web server before installing git. The –ask-become-pass directive prompts for the sudo user in order to carry out the tasks defined in the plays.


How to Create a Directory ?

Creating a new directory uses the same configuration as when creating an empty file. The only difference is that under the state parameter, you enter directory as the value:

---
- hosts: all
  tasks:
  - name: Creating a new directory
    file:
      path: "/your path"
      state: directory


How to Remove Files ?

Ansible playbooks can also remove existing files. To do this, set the state parameter to absent:

---
- hosts: all
  tasks:
  - name: Removing a file
    file:
      path: "/your path"
      state: absent

If the file is already removed, this command does nothing.


How to Set File Permissions ?

You can also set the permission for new files and folders. For this, you need to use the mode parameter.

There are two ways to do this:

1. Using octal mode format: You can use octal numbers, like 0644 or 0777. Don’t forget the leading 0, as leaving it out can lead to unexpected results.

2. Using symbolic mode format: You can use values like u=rwx, g=rx, or o=rx, where u stands for owner, g stands for group, and o stands for others. The permissions are defined as r for read, w for write, and x for execute.

For example, you can use the octal value 0755, while defining the owner:

---
- hosts: all
  tasks:
  - name: Create a new file with permissions
    file:
      path: "/your path"
      state: touch
      mode: 0755
      owner: test

You can also use the symbolic equivalent to 0755:

---
- hosts: all
  tasks:
  - name: Create a new file with permissions
    file:
      path: "/your path"
      state: touch
      mode: u=rwx,g=rx,o=rx
      owner: test

[Need help with fixing Linux System errors? We can help you. ]

This article covers how you can create a simple playbook file and execute it. Basically, Ansible is an Infrastructure as Code tool that allows you to use a single central location (Ansible control node) to monitor and control a large number of remote servers (hosts).

Therefore, we can use Ansible to set up a number of tasks that the remote hosts can perform, including creating new files and directories.


Components of Ansible playbook file:

  • hosts: Defines on which remote hosts from your Ansible inventory file you want to execute the task. All means that every host receives the command, but you can also enter the name of a host category or one individual host.
  • tasks: Announces that the remote host needs to perform a task.
  • name: Lets you define a name for the task. The name is for your reference only and has no influence on the task itself.
  • file: Engages Ansible's file module to create a new file.
  • path: Defines the path for the new file on the remote host’s hard drive.
  • state: Similar to the touch command in the Ubuntu terminal, entering touch creates an empty file in the location you chose.

Related Posts