Ansible Playbook Example for beginners

Ansible is a powerful open-source automation tool that allows you to configure and manage servers. Playbooks are a key component of Ansible, allowing you to define tasks and configurations in a declarative way.

In this tutorial, we will create a basic Ansible playbook example to demonstrate its usage. Our playbook will create a text file and write a custom message to it on remote servers.

Prerequisites

  • Ansible installed on your control machine
  • Access to remote servers with SSH keys configured

Creating the Ansible Playbook

Let’s start by creating a simple Ansible playbook.

  1. Open your favorite text editor and create a new file named basic_playbook.yaml.
  2. Copy and paste the following content into the file:
---
- name: Create Hello Ansible Text File
  hosts: localhost
  tasks:
    - name: Create text file
      ansible.builtin.file:
        path: /tmp/hello_ansible.txt
        state: touch

    - name: Write "Hello Ansible" to the file
      ansible.builtin.lineinfile:
        path: /tmp/hello_ansible.txt
        line: "Hello Ansible"

The playbook is a structured set of instructions for Ansible to follow, automating the process of creating a text file and writing a specific line to it on remote servers. Within it, it defines a taskCreate text file“. Within it, you will find the following modules:

You can run the playbook as follows:

ansible-playbook basic_playbook.yml

Then, chck on the Console the outcome:

Finally, check that the file /tmp/hello_ansible.txt is available on your machine:

cat /tmp/hello_ansible.txt 
Hello Ansible

Introducing User-Defined Variables in your Playbook

Our first example was quite rudimentary. We will now improve it a bit so that it resembles real world playbooks. Firstly, we will introduce variables in it. Variables allow you to define dynamic values or parameters that can be reused across different parts of your Ansible configuration. They provide flexibility, allowing you to customize your automation based on different conditions, environments, or requirements.

For example, we will define the path of the file and the content as variables. Here is the new version of our playbook:

---
- name: Create Hello Ansible Text File
  hosts: localhost
  vars:
    file_path: "/tmp/hello_ansible.txt"
    content_line: "Hello Ansible"

  tasks:
    - name: Create text file
      ansible.builtin.file:
        path: "{{ file_path }}"
        state: touch

    - name: Write "{{ content_line }}" to the file
      ansible.builtin.lineinfile:
        path: "{{ file_path }}"
        line: "{{ content_line }}"

Introducing Inventory files in your PlayBook

Finally, we will show how to run your playbook on a list of remote machines. To do that, you can create an inventory file:

[my_servers]
192.168.1.2 ansible_ssh_user=fedora
  1. Host Group:
    • [my_servers]: This line defines a host group named “my_servers.” Host groups are used to organize and group related hosts together. In this case, the group is named “my_servers.”
  2. Host Entry:
    • 192.168.1.2: This is the IP address of a specific host that belongs to the “my_servers” group. It indicates the target machine that Ansible will manage.
  3. Variables for the Host:
    • ansible_ssh_user=fedora: This line specifies a variable for the host with the IP address 192.168.1.2. The variable is ansible_ssh_user, and its value is set to “fedora.”
      • ansible_ssh_user is a special variable used to specify the SSH username that Ansible should use when connecting to the host. In this case, the SSH user is “fedora.”
      • Variables in the inventory file allow you to set specific configuration options for individual hosts or groups of hosts.

Then, modify the section of your playbook which contains the hosts definition:

---
- name: Create Hello Ansible Text File
  hosts: my_servers

Before you run the playbook against the remote machine, make sure the public SSH is available on the remote machine. The simplest way to copy the SSH public key is by means of the ssh-copy-id command which takes as argument the username@host . For example:

ssh-copy-id [email protected]

Conclusion

Congratulations! You’ve created and executed a basic Ansible playbook. This is just the beginning of what Ansible can do. As you become more familiar with Ansible, you can explore its various modules and features to automate complex tasks in your infrastructure.

Found the article helpful? if so please follow us on Socials