Ansible

Ansible Block and When Directives

Ansible is a fantastic tool for automating the DevOps processes. It offers an incredible interface and feature approach using a declarative language. This makes creating and implementing of Ansible playbooks easy and approachable to those unfamiliar with programming concepts.

One of the features provided in Ansible is the ability to group a set of tasks into a component that is executed as a single unit.

Therefore, this tutorial teaches us how to group a set of Ansible tasks into a single unit using the block directive. We will also explore how to apply the conditional execution into the resulting blocks using the “when” keyword.

Ansible Block and When

Ansible allows us to group the tasks using the “block” keyword. Grouping tasks in Ansible is advantageous as it enables you to apply certain conditions or features to multiple tasks simultaneously, such as error handling.

Grouping tasks can also help to make your playbooks more readable or easy to maintain in the long run.

Next is the “when” directive. This directive allows us to set the conditions to execute or skip a task. It’s like an “if” statement in programming which provides a conditional logic to a playbook.

Using the Block Directive

An Ansible block groups the tasks together which is particularly useful when combined with when, rescue, and always.

The following playbook shows how we can use the “block” keyword to group a set of tasks together:

- name: Install packages
  block:
    - name: Install nginx
      yum:
        name: nginx
        state: latest

    - name: Install postgresql
      yum:
        name: postgresql
        state: latest

The provided block groups two tasks: installing Nginx and PostgreSQL.

Using the When Directive

Next is the “when” directive in Ansible. This directive allows us to introduce a conditional logic based on the value of a given variable.

For example, suppose you wish to run a task if the operating system is from the RedHat Family. We can create a basic playbook as follows:

tasks:
  - name: Install nginx on CentOS
    yum:
      name: nginx
      state: latest
    when: ansible_os_family == 'RedHat'

In this case, the playbook only installs the Nginx server if the operating system is a member of the RedHat family such as CentOS.

Combining the Block and When Directives

The “block” and “when” features in Ansible become more powerful when you combine both of them. We can connect the Ansible grouping feature using “block” and the conditional logic using “when” to build a set of tasks that execute only when a given condition is true.

An example on how this works is as demonstrated in the following:

- name: Install web and database services on CentOS
  block:
    - name: Install nginx
      yum:
        name: nginx
        state: latest

    - name: Install postgresql
      yum:
        name: postgresql
        state: latest
  when: ansible_os_family == 'RedHat'

In this case, both tasks within the block will only execute if the target machine is a member of the RedHat family.

Nested Blocks

Ansible also allows us to create more complex playbooks using nested blocks. This is similar to creating a series of nested “if” statements as necessary.

An example is as follows:

- name: Setup for RedHat family
  block:
    - name: Install common packages
      yum:
        name: "{{ item }}"
        state: latest
      loop:
        - git
        - wget
    - name: Setup for CentOS 8
      block:
        - name: Install EPEL repository
          command: yum install epel-release -y
      when: ansible_distribution_major_version == '8'
  when: ansible_os_family == 'RedHat'

In this case, the previous playbook installs the standard packages for all systems in the RedHat family. However, it also configures the EPEL repository if the OS family happens to be CentOS 8.

Conclusion

This tutorial provided you with the foundational knowledge of working with the “block” and “when” directives in Ansible. Using practical examples, you also learned how to nest multiple blocks and introduce the conditional logic for each.

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