Part 3: Creating Ansible Playbooks

Standard way of doing things with playbooks.

Sebastiaan avatar
  • Sebastiaan
  • 4 min read

Why Playbooks over ADHOC commands

ADHOC commands are great for short and quick informational actions but when you want to to get more information or configuration from a group.
Then is one command at a time a really time consuming action, this is where playbooks shine.
Why are playbook better then adhoc commands?, there is more predictablity on playbooks and less manual work on it.

A playbook is a repeatable script that we can run over and over again, with the same result. In these playbooks we can specify precise actions what we want to do and in which order.

Creating the playbook

We are going to create a new file in the folder we’ve created in the previous post /Users/sebas/Desktop/ansible.
This file is can be created through GUI or CLI I chose CLI then the command is touch /Users/sebas/Desktop/ansible/my_first_playbook.yml.

In the minor playbook below we do the following:

  • Get Machine hostname
  • View the output
  • Create a file in /tmp/called eek2.txt.

Copy the playbook below into the file we created my_first_playbook.yml.

---
- name: playbook      # General name of the playbook
  hosts: proxmox      # Which host or hostgroup we want to talk to
  remote_user: root   # The user on the remote machine

  tasks:                
   - name: Get Hostname                   # Name of the task
     ansible.builtin.command: hostname    # Command module, we run the linux command hostname
     register: output                     # Register the output of the builtin.command

   - name: get machine output             # Name of the task
     ansible.builtin.debug:               # Debug module to see the output from what we registerd in previous task
       var: output                        # Uses the output var to display the result of output

   - name: file with content              # Name of the task
     ansible.builtin.copy:                # Copy module does sort of the same as file but is capable of adding content
       dest: "/tmp/eek2.txt"              # File path + filename
       content: |                         # Content block is where we can add information, the pike ( | ) logo is nothing more then start on next line.
         this is on the first line.
         etc.

The command rundown

We are going to use the inventory we’ve created in part two. We can see alot more information with -v tag, which stands for verbose.
This can scale up to three v like -vvv but thats not necessary for now. The ansible command we are going to run:

ansible-playbook -i inventory my_first_playbook.yml -K

Output command

sebas@eyeofmordor ansible % ansible-playbook -i inventory my_first_playbook.yml -K
BECOME password: 

PLAY [playbook] ******************************************************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************************************
[WARNING]: Platform linux on host 192.168.200.155 is using the discovered Python interpreter at /usr/bin/python3.11, but future installation of another Python
interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.18/reference_appendices/interpreter_discovery.html for more
information.
ok: [192.168.200.155]

TASK [what machine is this] ******************************************************************************************************************************************
changed: [192.168.200.155]

TASK [get machine output] ********************************************************************************************************************************************
ok: [192.168.200.155] => {
    "output": {
        "changed": true,
        "cmd": [
            "hostname"
        ],
        "delta": "0:00:00.003310",
        "end": "2025-01-06 22:55:42.248595",
        "failed": false,
        "msg": "",
        "rc": 0,
        "start": "2025-01-06 22:55:42.245285",
        "stderr": "",
        "stderr_lines": [],
        "stdout": "proxmox",
        "stdout_lines": [
            "proxmox"
        ]
    }
}

TASK [file eek create] ***********************************************************************************************************************************************
changed: [192.168.200.155]

TASK [file with content] *********************************************************************************************************************************************
changed: [192.168.200.155]

PLAY RECAP ***********************************************************************************************************************************************************
192.168.200.155            : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Host check

sebas@eyeofmordor ansible % ssh root@192.168.200.155                          
Linux proxmox 6.8.12-5-pve #1 SMP PREEMPT_DYNAMIC PMX 6.8.12-5 (2024-12-03T10:26Z) x86_64

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Mon Jan  6 22:55:43 2025 from 192.168.200.27
root@proxmox:~# ls /tmp/
systemd-private-2405913b3d2e4137b7a6c92065b2a5a1-chrony.service-oHmDrN
eek2.txt  systemd-private-2405913b3d2e4137b7a6c92065b2a5a1-systemd-logind.service-e6apCa
root@proxmox:~# cat /tmp/eek2.txt 
this is on the first line.
etc.
root@proxmox:~# 

We see in the output above, what the result is from our hostname command proxmox and we see the lines added to the file eek2.txt.

Conclusion

Ansible has a wide varity of modules which we can use in playbooks and roles (which is still to come). The consistant way of configuring and creating infrastruture makes it reliable for even the hardest tasks.

Stay tuned for the Part 4: Creating Roles!

Sebastiaan

Written by : Sebastiaan

Sysadmin/Platform/Devops Engineer

Recommended for You

Part 2: Managing Hosts with Ansible

Part 2: Managing Hosts with Ansible

first steps managing a host with Ansible.

Part 1: Ansible? what is that!

Part 1: Ansible? what is that!

Automation and configuration with Ansible.