mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
407 lines
14 KiB
ReStructuredText
407 lines
14 KiB
ReStructuredText
11 years ago
|
Conditionals
|
||
|
============
|
||
|
|
||
11 years ago
|
.. contents:: Topics
|
||
|
|
||
|
|
||
8 years ago
|
Often the result of a play may depend on the value of a variable, fact (something learned about the remote system), or previous task result.
|
||
|
In some cases, the values of variables may depend on other variables.
|
||
7 years ago
|
Additional groups can be created to manage hosts based on whether the hosts match other criteria. This topic covers how conditionals are used in playbooks.
|
||
7 years ago
|
|
||
|
.. note:: There are many options to control execution flow in Ansible. More examples of supported conditionals can be located here: http://jinja.pocoo.org/docs/dev/templates/#comparisons.
|
||
11 years ago
|
|
||
|
|
||
10 years ago
|
.. _the_when_statement:
|
||
|
|
||
11 years ago
|
The When Statement
|
||
|
``````````````````
|
||
|
|
||
8 years ago
|
Sometimes you will want to skip a particular step on a particular host.
|
||
|
This could be something as simple as not installing a certain package if the operating system is a particular version,
|
||
11 years ago
|
or it could be something like performing some cleanup steps if a filesystem is getting full.
|
||
|
|
||
9 years ago
|
This is easy to do in Ansible with the `when` clause, which contains a raw Jinja2 expression without double curly braces (see :doc:`playbooks_variables`).
|
||
11 years ago
|
It's actually pretty simple::
|
||
11 years ago
|
|
||
|
tasks:
|
||
8 years ago
|
- name: "shut down Debian flavored systems"
|
||
11 years ago
|
command: /sbin/shutdown -t now
|
||
|
when: ansible_os_family == "Debian"
|
||
9 years ago
|
# note that Ansible facts and vars like ansible_os_family can be used
|
||
|
# directly in conditionals without double curly braces
|
||
11 years ago
|
|
||
10 years ago
|
You can also use parentheses to group conditions::
|
||
|
|
||
|
tasks:
|
||
8 years ago
|
- name: "shut down CentOS 6 and Debian 7 systems"
|
||
10 years ago
|
command: /sbin/shutdown -t now
|
||
9 years ago
|
when: (ansible_distribution == "CentOS" and ansible_distribution_major_version == "6") or
|
||
|
(ansible_distribution == "Debian" and ansible_distribution_major_version == "7")
|
||
10 years ago
|
|
||
8 years ago
|
Multiple conditions that all need to be true (a logical 'and') can also be specified as a list::
|
||
|
|
||
|
tasks:
|
||
|
- name: "shut down CentOS 6 systems"
|
||
|
command: /sbin/shutdown -t now
|
||
|
when:
|
||
|
- ansible_distribution == "CentOS"
|
||
|
- ansible_distribution_major_version == "6"
|
||
|
|
||
11 years ago
|
A number of Jinja2 "filters" can also be used in when statements, some of which are unique
|
||
|
and provided by Ansible. Suppose we want to ignore the error of one statement and then
|
||
|
decide to do something conditionally based on success or failure::
|
||
|
|
||
|
tasks:
|
||
|
- command: /bin/false
|
||
|
register: result
|
||
|
ignore_errors: True
|
||
9 years ago
|
|
||
11 years ago
|
- command: /bin/something
|
||
7 years ago
|
when: result is failed
|
||
9 years ago
|
|
||
7 years ago
|
# In older versions of ansible use ``success``, now both are valid but succeeded uses the correct tense.
|
||
11 years ago
|
- command: /bin/something_else
|
||
7 years ago
|
when: result is succeeded
|
||
9 years ago
|
|
||
11 years ago
|
- command: /bin/still/something_else
|
||
7 years ago
|
when: result is skipped
|
||
11 years ago
|
|
||
9 years ago
|
|
||
7 years ago
|
.. note:: both `success` and `succeeded` work (`fail`/`failed`, etc).
|
||
9 years ago
|
|
||
11 years ago
|
|
||
7 years ago
|
As a reminder, to see what facts are available on a particular system, you can do the following::
|
||
11 years ago
|
|
||
|
ansible hostname.example.com -m setup
|
||
|
|
||
|
Tip: Sometimes you'll get back a variable that's a string and you'll want to do a math operation comparison on it. You can do this like so::
|
||
|
|
||
|
tasks:
|
||
|
- shell: echo "only on Red Hat 6, derivatives, and later"
|
||
|
when: ansible_os_family == "RedHat" and ansible_lsb.major_release|int >= 6
|
||
|
|
||
|
.. note:: the above example requires the lsb_release package on the target host in order to return the ansible_lsb.major_release fact.
|
||
|
|
||
|
Variables defined in the playbooks or inventory can also be used. An example may be the execution of a task based on a variable's boolean value::
|
||
|
|
||
|
vars:
|
||
|
epic: true
|
||
|
|
||
|
Then a conditional execution might look like::
|
||
|
|
||
|
tasks:
|
||
|
- shell: echo "This certainly is epic!"
|
||
|
when: epic
|
||
|
|
||
|
or::
|
||
8 years ago
|
|
||
11 years ago
|
tasks:
|
||
|
- shell: echo "This certainly isn't epic!"
|
||
|
when: not epic
|
||
|
|
||
8 years ago
|
If a required variable has not been set, you can skip or fail using Jinja2's `defined` test. For example::
|
||
11 years ago
|
|
||
|
tasks:
|
||
|
- shell: echo "I've got '{{ foo }}' and am not afraid to use it!"
|
||
|
when: foo is defined
|
||
|
|
||
11 years ago
|
- fail: msg="Bailing out. this play requires 'bar'"
|
||
9 years ago
|
when: bar is undefined
|
||
11 years ago
|
|
||
8 years ago
|
This is especially useful in combination with the conditional import of vars files (see below).
|
||
|
As the examples show, you don't need to use `{{ }}` to use variables inside conditionals, as these are already implied.
|
||
11 years ago
|
|
||
9 years ago
|
.. _loops_and_conditionals:
|
||
|
|
||
|
Loops and Conditionals
|
||
|
``````````````````````
|
||
7 years ago
|
Combining `when` with loops (see :doc:`playbooks_loops`), be aware that the `when` statement is processed separately for each item. This is by design::
|
||
11 years ago
|
|
||
|
tasks:
|
||
|
- command: echo {{ item }}
|
||
7 years ago
|
loop: [ 0, 2, 4, 6, 8, 10 ]
|
||
11 years ago
|
when: item > 5
|
||
|
|
||
9 years ago
|
If you need to skip the whole task depending on the loop variable being defined, used the `|default` filter to provide an empty iterator::
|
||
|
|
||
|
- command: echo {{ item }}
|
||
7 years ago
|
loop: "{{ mylist|default([]) }}"
|
||
9 years ago
|
when: item > 5
|
||
|
|
||
|
|
||
7 years ago
|
If using a dict in a loop::
|
||
9 years ago
|
|
||
|
- command: echo {{ item.key }}
|
||
7 years ago
|
loop: "{{ lookup('dict', mydict|default({})) }}"
|
||
9 years ago
|
when: item.value > 5
|
||
|
|
||
|
.. _loading_in_custom_facts:
|
||
|
|
||
11 years ago
|
Loading in Custom Facts
|
||
|
```````````````````````
|
||
|
|
||
8 years ago
|
It's also easy to provide your own facts if you want, which is covered in :doc:`dev_guide/developing_modules`. To run them, just
|
||
11 years ago
|
make a call to your own custom fact gathering module at the top of your list of tasks, and variables returned
|
||
|
there will be accessible to future tasks::
|
||
|
|
||
|
tasks:
|
||
|
- name: gather site specific fact data
|
||
|
action: site_facts
|
||
|
- command: /usr/bin/thingy
|
||
11 years ago
|
when: my_custom_fact_just_retrieved_from_the_remote_system == '1234'
|
||
9 years ago
|
|
||
|
.. _when_roles_and_includes:
|
||
|
|
||
7 years ago
|
Applying 'when' to roles, imports, and includes
|
||
|
```````````````````````````````````````````````
|
||
11 years ago
|
|
||
|
Note that if you have several tasks that all share the same conditional statement, you can affix the conditional
|
||
9 years ago
|
to a task include statement as below. All the tasks get evaluated, but the conditional is applied to each and every task::
|
||
11 years ago
|
|
||
7 years ago
|
- import_tasks: tasks/sometasks.yml
|
||
11 years ago
|
when: "'reticulating splines' in output"
|
||
|
|
||
9 years ago
|
.. note:: In versions prior to 2.0 this worked with task includes but not playbook includes. 2.0 allows it to work with both.
|
||
|
|
||
11 years ago
|
Or with a role::
|
||
|
|
||
|
- hosts: webservers
|
||
|
roles:
|
||
|
- { role: debian_stock_config, when: ansible_os_family == 'Debian' }
|
||
|
|
||
|
You will note a lot of 'skipped' output by default in Ansible when using this approach on systems that don't match the criteria.
|
||
11 years ago
|
Read up on the 'group_by' module in the :doc:`modules` docs for a more streamlined way to accomplish the same thing.
|
||
11 years ago
|
|
||
7 years ago
|
When used with `include_*` tasks instead of imports, the conditional is applied _only_ to the include task itself and not any other
|
||
|
tasks within the included file(s). A common situation where this distinction is important is as follows::
|
||
|
|
||
|
# include a file to define a variable when it is not already defined
|
||
|
|
||
|
# main.yml
|
||
|
- include_tasks: other_tasks.yml
|
||
|
when: x is not defined
|
||
|
|
||
|
# other_tasks.yml
|
||
|
- set_fact:
|
||
|
x: foo
|
||
|
- debug:
|
||
|
var: x
|
||
|
|
||
|
In the above example, if ``import_tasks`` had been used instead both included tasks would have also been skipped. With ``include_tasks``
|
||
|
instead, the tasks are executed as expected because the conditional is not applied to them.
|
||
|
|
||
9 years ago
|
.. _conditional_imports:
|
||
|
|
||
11 years ago
|
Conditional Imports
|
||
|
```````````````````
|
||
|
|
||
7 years ago
|
.. note:: This is an advanced topic that is infrequently used.
|
||
11 years ago
|
|
||
|
Sometimes you will want to do certain things differently in a playbook based on certain criteria.
|
||
|
Having one playbook that works on multiple platforms and OS versions is a good example.
|
||
|
|
||
|
As an example, the name of the Apache package may be different between CentOS and Debian,
|
||
|
but it is easily handled with a minimum of syntax in an Ansible Playbook::
|
||
|
|
||
|
---
|
||
|
- hosts: all
|
||
|
remote_user: root
|
||
|
vars_files:
|
||
|
- "vars/common.yml"
|
||
|
- [ "vars/{{ ansible_os_family }}.yml", "vars/os_defaults.yml" ]
|
||
|
tasks:
|
||
8 years ago
|
- name: make sure apache is started
|
||
|
service: name={{ apache }} state=started
|
||
11 years ago
|
|
||
|
.. note::
|
||
|
The variable 'ansible_os_family' is being interpolated into
|
||
|
the list of filenames being defined for vars_files.
|
||
|
|
||
|
As a reminder, the various YAML files contain just keys and values::
|
||
|
|
||
|
---
|
||
|
# for vars/CentOS.yml
|
||
|
apache: httpd
|
||
|
somethingelse: 42
|
||
|
|
||
|
How does this work? If the operating system was 'CentOS', the first file Ansible would try to import
|
||
|
would be 'vars/CentOS.yml', followed by '/vars/os_defaults.yml' if that file
|
||
|
did not exist. If no files in the list were found, an error would be raised.
|
||
|
On Debian, it would instead first look towards 'vars/Debian.yml' instead of 'vars/CentOS.yml', before
|
||
|
falling back on 'vars/os_defaults.yml'. Pretty simple.
|
||
|
|
||
|
To use this conditional import feature, you'll need facter or ohai installed prior to running the playbook, but
|
||
|
you can of course push this out with Ansible if you like::
|
||
|
|
||
|
# for facter
|
||
10 years ago
|
ansible -m yum -a "pkg=facter state=present"
|
||
|
ansible -m yum -a "pkg=ruby-json state=present"
|
||
11 years ago
|
|
||
|
# for ohai
|
||
10 years ago
|
ansible -m yum -a "pkg=ohai state=present"
|
||
11 years ago
|
|
||
|
Ansible's approach to configuration -- separating variables from tasks, keeps your playbooks
|
||
|
from turning into arbitrary code with ugly nested ifs, conditionals, and so on - and results
|
||
|
in more streamlined & auditable configuration rules -- especially because there are a
|
||
|
minimum of decision points to track.
|
||
|
|
||
|
Selecting Files And Templates Based On Variables
|
||
|
````````````````````````````````````````````````
|
||
|
|
||
|
.. note:: This is an advanced topic that is infrequently used. You can probably skip this section.
|
||
|
|
||
|
Sometimes a configuration file you want to copy, or a template you will use may depend on a variable.
|
||
|
The following construct selects the first available file appropriate for the variables of a given host, which is often much cleaner than putting a lot of if conditionals in a template.
|
||
|
|
||
|
The following example shows how to template out a configuration file that was very different between, say, CentOS and Debian::
|
||
|
|
||
|
- name: template a file
|
||
7 years ago
|
template:
|
||
|
src: "{{ item }}"
|
||
|
dest: /etc/myapp/foo.conf
|
||
7 years ago
|
loop: "{{lookup('first_found', { 'files': myfiles, 'paths': mypaths})}}"
|
||
|
vars:
|
||
|
myfiles:
|
||
|
- "{{ansible_distribution}}.conf"
|
||
|
- default.conf
|
||
|
mypaths: ['search_location_one/somedir/', '/opt/other_location/somedir/']
|
||
11 years ago
|
|
||
|
Register Variables
|
||
|
``````````````````
|
||
|
|
||
|
Often in a playbook it may be useful to store the result of a given command in a variable and access
|
||
|
it later. Use of the command module in this way can in many ways eliminate the need to write site specific facts, for
|
||
|
instance, you could test for the existence of a particular program.
|
||
|
|
||
|
The 'register' keyword decides what variable to save a result in. The resulting variables can be used in templates, action lines, or *when* statements. It looks like this (in an obviously trivial example)::
|
||
|
|
||
|
- name: test play
|
||
|
hosts: all
|
||
|
|
||
|
tasks:
|
||
|
|
||
|
- shell: cat /etc/motd
|
||
|
register: motd_contents
|
||
|
|
||
|
- shell: echo "motd contains the word hi"
|
||
|
when: motd_contents.stdout.find('hi') != -1
|
||
|
|
||
|
As shown previously, the registered variable's string contents are accessible with the 'stdout' value.
|
||
7 years ago
|
The registered result can be used in the loop of a task if it is converted into
|
||
11 years ago
|
a list (or already is a list) as shown below. "stdout_lines" is already available on the object as
|
||
|
well though you could also call "home_dirs.stdout.split()" if you wanted, and could split by other
|
||
|
fields::
|
||
|
|
||
7 years ago
|
- name: registered variable usage as a loop list
|
||
11 years ago
|
hosts: all
|
||
|
tasks:
|
||
|
|
||
7 years ago
|
- name: retrieve the list of home directories
|
||
|
command: ls /home
|
||
|
register: home_dirs
|
||
|
|
||
|
- name: add home dirs to the backup spooler
|
||
|
file:
|
||
|
path: /mnt/bkspool/{{ item }}
|
||
|
src: /home/{{ item }}
|
||
|
state: link
|
||
|
loop: "{{ home_dirs.stdout_lines }}"
|
||
|
# same as loop: "{{ home_dirs.stdout.split() }}"
|
||
11 years ago
|
|
||
|
|
||
9 years ago
|
As shown previously, the registered variable's string contents are accessible with the 'stdout' value.
|
||
|
You may check the registered variable's string contents for emptiness::
|
||
|
|
||
|
- name: check registered variable for emptiness
|
||
|
hosts: all
|
||
|
|
||
|
tasks:
|
||
|
|
||
|
- name: list contents of directory
|
||
|
command: ls mydir
|
||
|
register: contents
|
||
|
|
||
|
- name: check contents for emptiness
|
||
7 years ago
|
debug:
|
||
|
msg: "Directory is empty"
|
||
9 years ago
|
when: contents.stdout == ""
|
||
|
|
||
7 years ago
|
Commonly Used Facts
|
||
|
```````````````````
|
||
|
|
||
|
The following Facts (see :ref:`_vars_and_facts`) are frequently used in Conditionals - see above for examples.
|
||
|
|
||
|
.. _ansible_distribution:
|
||
|
|
||
|
ansible_distribution
|
||
|
--------------------
|
||
|
|
||
|
Possible values::
|
||
|
|
||
|
Alpine
|
||
|
Altlinux
|
||
|
Amazon
|
||
|
Archlinux
|
||
|
ClearLinux
|
||
|
Coreos
|
||
|
Debian
|
||
|
Gentoo
|
||
|
Mandriva
|
||
|
NA
|
||
|
OpenWrt
|
||
|
OracleLinux
|
||
|
RedHat
|
||
|
Slackware
|
||
|
SMGL
|
||
|
SUSE
|
||
|
VMwareESX
|
||
|
|
||
|
.. See `OSDIST_LIST`
|
||
|
|
||
|
.. _ansible_distribution_major_version:
|
||
|
|
||
|
ansible_distribution_major_version
|
||
|
----------------------------------
|
||
|
|
||
|
This will be the major version of the operating system. For example, the value will be `16` for Ubuntu 16.04.
|
||
|
|
||
|
.. _ansible_os_family:
|
||
|
|
||
|
ansible_os_family
|
||
|
-----------------
|
||
|
|
||
|
Possible values::
|
||
|
|
||
|
AIX
|
||
|
Alpine
|
||
|
Altlinux
|
||
|
Archlinux
|
||
|
Darwin
|
||
|
Debian
|
||
|
FreeBSD
|
||
|
Gentoo
|
||
|
HP-UX
|
||
|
Mandrake
|
||
|
RedHat
|
||
|
SGML
|
||
|
Slackware
|
||
|
Solaris
|
||
|
Suse
|
||
|
|
||
|
.. See `OS_FAMILY_MAP`
|
||
11 years ago
|
|
||
11 years ago
|
.. seealso::
|
||
|
|
||
|
:doc:`playbooks`
|
||
|
An introduction to playbooks
|
||
8 years ago
|
:doc:`playbooks_reuse_roles`
|
||
11 years ago
|
Playbook organization by roles
|
||
|
:doc:`playbooks_best_practices`
|
||
|
Best practices in playbooks
|
||
|
:doc:`playbooks_variables`
|
||
|
All about variables
|
||
|
`User Mailing List <http://groups.google.com/group/ansible-devel>`_
|
||
|
Have a question? Stop by the google group!
|
||
|
`irc.freenode.net <http://irc.freenode.net>`_
|
||
|
#ansible IRC chat channel
|