When the interviewer presents this question, be specific in your answer about how you manage your daily work assignments. For instance, if you create a to-do list first thing in the morning, explain how you list your tasks and rank them in order of their urgency and importance.
Topics
- Conditionals
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.Further, additional groups can be created to manage hosts based on whether the hosts match other criteria.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
- Trust between team members so tasks can be completed without excessive micromanagement. Take your time to try out the LMS before deciding what fits you best. Market research is a grueling process that may take quite some time, especially when the person in charge is not very well versed with that type of software. Two factors come into play.
- A new tool that blends your everyday work apps into one. It's the all-in-one workspace for you and your team.
Let’s dig into what they are.
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,or it could be something like performing some cleanup steps if a filesystem is getting full.
This is easy to do in Ansible with the when clause, which contains a raw Jinja2 expression without double curly braces (see Variables).It’s actually pretty simple:
You can also use parentheses to group conditions:
Multiple conditions that all need to be true (a logical ‘and’) can also be specified as a list:
A number of Jinja2 “filters” can also be used in when statements, some of which are uniqueand provided by Ansible. Suppose we want to ignore the error of one statement and thendecide to do something conditionally based on success or failure:
Note
the filters have been updated in 2.1 so both success and succeeded work (fail/failed, etc).
Note that was a little bit of foreshadowing on the ‘register’ statement. We’ll get to it a bit later in this chapter.
As a reminder, to see what facts are available on a particular system, you can do:
Sotarks Twitter
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:
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:
Then a conditional execution might look like:
or:
If a required variable has not been set, you can skip or fail using Jinja2’s defined test. For example:
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.
Combining when with with_items (see Loops), be aware that the when statement is processed separately for each item. This is by design:
If you need to skip the whole task depending on the loop variable being defined, used the |default filter to provide an empty iterator:
If using with_dict which does not take a list:
It’s also easy to provide your own facts if you want, which is covered in Developing Modules. To run them, justmake a call to your own custom fact gathering module at the top of your list of tasks, and variables returnedthere will be accessible to future tasks:
Note that if you have several tasks that all share the same conditional statement, you can affix the conditionalto a task include statement as below. All the tasks get evaluated, but the conditional is applied to each and every task:
Note
In versions prior to 2.0 this worked with task includes but not playbook includes. 2.0 allows it to work with both.
Or with a role:
You will note a lot of ‘skipped’ output by default in Ansible when using this approach on systems that don’t match the criteria.Read up on the ‘group_by’ module in the About Modules docs for a more streamlined way to accomplish the same thing.
Note
This is an advanced topic that is infrequently used. You can probably skip this section.
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:
Note
The variable ‘ansible_os_family’ is being interpolated intothe list of filenames being defined for vars_files.
As a reminder, the various YAML files contain just keys and values:
How does this work? If the operating system was ‘CentOS’, the first file Ansible would try to importwould be ‘vars/CentOS.yml’, followed by ‘/vars/os_defaults.yml’ if that filedid 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’, beforefalling 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, butyou can of course push this out with Ansible if you like:
Ansible’s approach to configuration – separating variables from tasks, keeps your playbooksfrom turning into arbitrary code with ugly nested ifs, conditionals, and so on - and resultsin more streamlined & auditable configuration rules – especially because there are aminimum of decision points to track.
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:
Sotarks Osu
Often in a playbook it may be useful to store the result of a given command in a variable and accessit later. Use of the command module in this way can in many ways eliminate the need to write site specific facts, forinstance, 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):
Sotarks Map
As shown previously, the registered variable’s string contents are accessible with the ‘stdout’ value.The registered result can be used in the “with_items” of a task if it is converted intoa list (or already is a list) as shown below. “stdout_lines” is already available on the object aswell though you could also call “home_dirs.stdout.split()” if you wanted, and could split by otherfields:
Sotarks
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: Outlast free mac download.
See also
Sotarks Beatmaps
- Playbooks
- An introduction to playbooks
- Playbook Roles and Include Statements
- Playbook organization by roles
- Best Practices
- Best practices in playbooks
- Variables
- All about variables
- User Mailing List
- Have a question? Stop by the google group!
- irc.freenode.net
- #ansible IRC chat channel