Introduction

In the ever-evolving landscape of IT automation, Ansible continues to be a go-to tool for simplifying and streamlining repetitive tasks. While traditionally known for its prowess in system configuration and infrastructure management, Ansible extends its capabilities to even the minutiae of text processing. In this article, we’ll explore a practical use case: automating the transformation of text to uppercase using an Ansible playbook.

The Uppercase Ansible Playbook

Below is an Ansible playbook named upper.yml designed to capitalize a given text and print the result on the screen.

---
- name: Uppercase
  hosts: all
  gather_facts: false
  vars:
    my_text: "hello world"
  tasks:
    - name: Print message on the screen
      ansible.builtin.debug:
        msg: "{{ my_text | upper }}"

Breaking down the playbook components:

  • name: Describes the purpose of the playbook.
  • hosts: Specifies the target hosts; in this case, it’s set to all for execution on all hosts defined in the inventory.
  • gather_facts: Set to false to skip gathering facts about hosts, ensuring a quicker execution.
  • vars: Defines the my_text variable with the initial value set to “hello world.”
  • tasks: Contains a single task to print the uppercase message on the screen.

The Inventory File

Ansible requires an inventory file to specify the hosts where the playbook will run. The inventory file in this example designates the localhost with the connection set to local, indicating that the playbook will be executed on the local machine.

localhost ansible_connection=local
Join 50+ hours of courses in our exclusive community

Executing the Playbook

Running the playbook is a straightforward process. The ansible-playbook command, coupled with the -i flag to specify the inventory file, is used:

$ ansible-playbook -i inventory upper.yml

Upon execution, Ansible processes the defined tasks and outputs the result:

PLAY [Uppercase] ************************************************************************

TASK [Print message on the screen] ******************************************************
ok: [localhost] => {
    "msg": "HELLO WORLD"
}

PLAY RECAP ******************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

The output confirms the successful execution of the playbook on the localhost host, resulting in the message “HELLO WORLD” being printed on the screen.

Customizing and Extending

This example illustrates the simplicity of leveraging Ansible for text manipulation tasks. To adapt the playbook for different text or sources, modify the my_text variable. Additionally, you can expand the playbook’s functionality to capitalize text from external files or dynamic inventories.

Conclusion

As Playbooknstrated, Ansible is not only a powerhouse for system configurations but also a versatile tool for text processing automation. Whether you’re a system administrator, a DevOps professional, or anyone involved in IT operations, Ansible’s capabilities extend to tasks both large and small, showcasing its value in the realm of automation.

Subscribe to the YouTube channel, Medium, and Website, X (formerly Twitter) to not miss the next episode of the Ansible Pilot.

Academy

Learn the Ansible automation technology with some real-life examples in my Udemy 300+ Lessons Video Course.

BUY the Complete Udemy 300+ Lessons Video Course

My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps

BUY the Complete PDF BOOK to easily Copy and Paste the 250+ Ansible code

Want to keep this project going? Please donate

Patreon Buy me a Pizza