Ansible

Ansible Pre_Tasks

Ansible is an open-source automation tool for managing configurations, deployments, and orchestration. It uses the declarative language (YAML) to describe the system configuration, ensuring consistency and repeatability.

However, when working with Anisble playbooks, you will encounter scenarios where you need some tasks to run before the primary set of tasks. This is where Ansible pre_tasks come to aid.

Ansible pre_tasks is a directive that allows us to define a set of tasks that should be executed before the roles or tasks that are defined in the main part of the playbook are implemented.

This tutorial dives deep into pre_tasks, explaining their significance and how to use them in various scenarios effectively.

Ansible Pre_Tasks Basic Structure

Before diving into how to use the Ansible pre_tasks directive, let us demonstrate its functionality with a basic syntax in a playbook.

---
- name: Sample Playbook
  hosts: all
  pre_tasks:
    - name: This is a pre-task
      command: echo "I run before the main tasks"
  tasks:
    - name: This is a main task
      command: echo "I am a main task"
  post_tasks:
    - name: This is a post-task
      command: echo "I run after the main tasks"

If you run the previous sample playbook, Ansible executes it in the following order:

  1. Pre_tasks
  2. Main_Tasks
  3. Post_tasks if present

As you can see, pre-tasks are run first, then the main tasks, and the post-tasks are executed last.

Why Use Pre_Tasks?

When are some of the scenarios that you may need to use pre-tasks?

Dependency Availability – Before deploying a software package, you may need to ensure that the necessary dependencies or software repositories are available.

System Updates – Another common occurrence is updating the system packages before running other tasks.

Configuration Availability – In other cases, you may need to fetch or compute specific configuration parameters before deploying a service.

Example Usage:

The following are some example playbooks that demonstrate how to use the pre_tasks feature in Ansible:

Example 1: Updating the System Packages

Before running any significant tasks, we might want to update the system packages. We can use the pre_tasks as shown in the following playbook example:

---
- name: Update System and Install Software
  hosts: all
  become: yes
  pre_tasks:
    - name: Update all system packages
      apt:
        update_cache: yes
        upgrade: dist
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: present

This should download the latest system packages before installing the Nginx server on the remote hosts.

Example 2: Ensure that a Service is Stopped Before Configuration

Another common use of the pre_tasks directive is ensuring that a service is stopped before updating the configuration. An example is as follows:

---
- name: Configure Service
  hosts: all
  pre_tasks:
    - name: Stop the service
      service:
        name: nginx
        state: stopped
  tasks:
    - name: Update server configuration
      copy:
        src: /etc/nginx/proxy.conf.bak
        dest: /etc/nginx/proxy.conf
      notify: Restart nginx
  handlers:
    - name: Restart nginx
      service:
        name: nginx
        state: restarted

Example 3: Check the Disk Space Before Deployment

We can also create a pre_task that checks for the disk space before deploying a given application. If the disk space is insufficient, the deployment will fail as demonstrated in the following example:

---
- name: Deploy Application
  hosts: all
  pre_tasks:
    - name: Check disk space
      shell: df -h / | tail -1 | awk '{print $4}'
      register: disk_space
      failed_when: "disk_space.stdout|int < 5000"
  tasks:
    - name: Deploy app
      command: ./deploy_app.sh

In this case, the deployment will not proceed if less than 5GB of disk space is available.

Conclusion

This tutorial explored the workings of the Ansible pre-tasks to define a set of tasks that run before the main roles are defined in your playbook. As you can see, Ansible pre_tasks provide an exceptional and flexible way of ensuring that the requirements or preconditions for various tasks are ready.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list