Ansible - Conditionals đ
Description đ
Conditionals
are used to control the flow of execution of tasks in Ansible
. They are used to check the state of a system and then execute a task based on the result of the check.
Examples đ§Š
-
when example; installs
nginx
onDebian
andRedHat
based systems# when.yml --- - name: install nginx hosts: all tasks: - name: install nginx apt: name: nginx state: present when: ansible_os_family == "Debian" - name: install nginx yum: name: nginx state: present when: ansible_os_family == "RedHat"
-
record output of tasks; mail if service is not running (first checking the service)
# register.yml --- - name: Check Status of Services hosts: localhost tasks: - name: Check HTTPD Status command: service httpd status register: httpd_status - name: Mail HTTPD Status if not running mail: to: someone@job.com subject: "HTTPD is not running" body: "" when: httpd_status.stdout.find("down") != -1
-
loop example; installs packages based on requirement over an array of packages
# loop.yml --- - name: Install Packages hosts: all vars: packages: - name: nginx required: True - name: httpd required: False - name: apache2 required: False tasks: - name: Install (using apt) apt: name: "" state: present when: item.required == True loop: ""
- use loops to create arrays
- when looping over an array of dictionaries you are able to use dot notation for the nested items
- arrays of dictionaries can also be represented in a json format
# loop_arr.yml --- - name: Create Users hosts: localhost var: num_users: 6 tasks: - name: Create Users user: name: '' id: '' state: present loop: - name: user_1 id: 1 - name: user_2 id: 2 - name: user_3 id: 3 - name: user_4 id: 4 - name: user_5 id: 5 - name: user_6 id: 6 - name: Create Users (json) user: name: '' id: '' state: present loop: - {name: user_1, id: 1} - {name: user_2, id: 2} - {name: user_3, id: 3} - {name: user_4, id: 4} - {name: user_5, id: 5} - {name: user_6, id: 6}
- with_* directive lets you loop over a list of items
with_items
is the most commonwith_dict
is used to loop over a dictionarywith_file
is used to loop over a filewith_url
is used to loop over urlswith_env
is used to loop over environment variablesWith_k8s
is used to loop over kubernetes resourcesWith_inventory_hostnames
is used to loop over inventory hostnames
# with_star.yml --- - name: Create Users hosts: localhost var: num_users: 6 tasks: - name: Create Users (with_items) user: name: '' id: '' state: present with_items: - name: user_1 id: 1 - name: user_2 id: 2 - name: user_3 id: 3 - name: user_4 id: 4 - name: user_5 id: 5 - name: user_6 id: 6 - name: View Config Files (with_file) debug: var=item with_file: - '/etc/hosts' - '/etc/resolv.conf' - '/etc/ntp.conf' - name: Get From URLS (with_url) debug: var=item with_url: - 'https://www.dell.com' - 'https://www.google.com' - 'https://www.amazon.com'
- use loops to create arrays