Learning Ansible
There are a couple of variable types in Ansible. The most common ones are:
This is just a value like:
my_string: "Hello world!"
You can also express integers (numbers like 1
, 2
, etc.) and booleans as a string:
my_string_that_looks_like_an_integer: "1"
my_string_that_looks_like_a_boolean: "yes"
In the above bases, the value is a string, not an integer or boolean.
This is a number, like:
my_integer: 1
Integers can be used to calculate or compare values.
- name: Show the value of 1 + 1
debug:
msg: "1"
- name: See if the value is 1 or up
debug:
msg: Yes, is larger than 1."
when: number > 1
A boolean is a value that can be true
or false
. There are a couple of ways to express a boolean:
true
or false
(Casing does not matter.)yes
or no
(Casing does not matter.)on
or off
(Casing does not matter.)1
or 0
I find yes
and no
easy to read, and find 1
and 0
hard to read. Ansible Lint and yamllint both advice to use true
and false
.
Running a job conditionally is a common use-case for a boolean. The syntax is a bit weird:
- name: Run this task only when the variable is true
debug:
msg: "Some message."
when:
- my_boolean
A list is a collection of values. A list is expressed like this:
my_list:
- value1
- value2
- value3
A list may contain any type of value, including other lists or dictionaries.
my_list:
- name: value1
- name: value2
something: foobar
- name: value3
another_things:
- name: foo
- name: bar
A dictionary or map is a collection of key-value pairs. A dictionary is expressed like this:
my_dictionary:
key1: value1
key2: value2
key3: value3
A dictionary looks a bit like the contents of a fysical dictionary, a word and a description.
Describe your family in an Ansible list called family_members
. Each family member should have a name, age and a list of hobbies.
Have a look at this example.