⚙️ 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.| Situation | What Ansible Does |
| User does NOT exist | Creates the user |
| User already exists | Does NOTHING (skips) |
No comments:
Post a Comment