Saturday, 6 December 2025

Ansible 17. conditions in ansibles

⚙️ Ansible Conditionals Summary

  • Conditionals are an Ansible feature that allows you to control the execution flow of playbook tasks based on declared conditions.

  • Actions like database backups, system reboots, or user management can be triggered only when relevant conditions are true.

  • Unnecessary tasks can be skipped based on conditions, improving playbook efficiency and reducing potential errors.

  • Conditionals can be used to notify relevant teams only when changes related to their responsibilities occur, enhancing communication and accountability.


A very simple example is below:

[oracle@oel01db ansible-project]$ cat ./inventory/hosts
[db_servers]
192.168.0.156
[oracle@oel01db ansible-project]$
[oracle@oel01db ansible-project]$ cat ./playbooks/craate-user-not-exist.yml
---
- name: Create tom user only if missing
  hosts: all
  become: yes

  tasks:

    - name: Check if tom user exists
      command: id tom
      register: tom_check
      failed_when: false
      changed_when: false

    - name: Create tom user
      user:
        name: tom
        state: present
      when: tom_check.rc != 0

[oracle@oel01db ansible-project]$

[oracle@oel01db ansible-project]$ ansible-playbook -i ./inventory/hosts ./playbooks/craate-user-not-exist.yml

PLAY [Create tom user only if missing] ******************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [192.168.0.156]

TASK [Check if tom user exists] *************************************************************************************************************************************************************************************************************
ok: [192.168.0.156]

TASK [Create tom user] **********************************************************************************************************************************************************************************************************************
changed: [192.168.0.156]

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

[oracle@oel01db ansible-project]$

Verification

[oracle@oel01db ansible-project]$ ansible all -i ./inventory/hosts -m command -a "id tom"
192.168.0.156 | CHANGED | rc=0 >>
uid=54322(tom) gid=54331(tom) groups=54331(tom)
[oracle@oel01db ansible-project]$

Even if we just use

- name: Create tom user
  user:
    name: tom
    state: present

Ansible will NOT fail if tom already exist, reason being 

The user module is idempotent.

SituationWhat Ansible Does
User does NOT existCreates the user
User already existsDoes NOTHING (skips)
We used condition only for learning purpose , in this case condition is not really needed 


No comments:

Post a Comment

Building a Safer PostgreSQL CI/CD Pipeline with GitHub Actions: Dev → PR Review → Test Promotion

In my previous post, we explored a simple push-to-main deployment strategy . While functional, that model is not considered an industry best...