Variables
Variables can be used in tasks, as well as in template files.
You reference variables by using {{ variable }}. Ansible replaces this {{ variable }} with the value of the variable value
eg
vars:
conf_file: /etc/nginx/sites/default
Ansible will substitute "{{ conf_file }}" with /etc/nginx/sites/default when it executes this task.
Ansible uses the Jinja2 template engine to implement templating
We use the .j2 extension to indicate that the file is a Jinja2 template. However, we can use a any extension if you like.
Ansible also uses the Jinja2 template engine to evaluate variables in playbooks.
Loop
When you want to run a task with each item from a list, you can use loop.
A loop executes the task multiple times, each time replacing item with different values from the specified list.
Handlers
Handlers are one of the conditional forms, A handler is similar to a task, but it runs only if it has been notified by a task. A task will run the notification if Ansible recognizes that the task was changed.
handlers:
- name: Restart nginx
service:
name: nginx
state: restarted
- name: Manage nginx
template:
src:
dest:
notify: Restart nginx
Handlers usually run at the end of the play after all of the tasks have been run.
To force a notified handler in the middle of a play, we need to use flush_handlers
- name: Restart nginx
meta: flush_handlers
If a play contains multiple handlers, the handlers always run in the order that they are defined in the handlers section, not the notification order.
They run only once, even if they are notified multiple times.
Variables in Separate Files
vars_files:
- nginx.yml
viewing the value of variables
To view the values of variables, you can use the debug module. The debug module allows you to print the value of a variable to the console during playbook execution.
- name: Print the value of a variable
debug:
var: variable_name
- debug: var=myvarname
Variable Interpolation
- name: Display the variable
debug:
msg: "The file used was {{ conf_file }}"
Variables can be concatenated between the double braces by using the tilde operator ~, as shown here:
- name: Concatenate variables
debug:
msg: "The URL is https://{{ server_name ~'.'~ domain_name }}/"
Registering Variables
The register keyword allows you to capture the output of a task and store it in a variable for later use.
- name: Capture output of whoami command
command: whoami
register: login
Example of using register to capture the output of a command and then display it using the debug module:
---
- name: Show return value of command module
hosts: fedora
gather_facts: false
tasks:
- name: Capture output of id command
command: id -un
register: login
- debug: var=login
- debug: msg="Logged in as user {{ login.stdout }}"
...
Output of the above playbook:
TASK [Capture output of id command] ******************************************************************************************
changed: [localhost]
TASK [debug] ****************************************************************************************************************
ok: [localhost] => {
"login": {
"changed": true,
"cmd": ["id", "-un"],
"delta": "0:00:00.003123",
"end": "2024-06-01 12:00:00.000000",
"rc": 0,
"start": "2024-06-01 12:00:00.000000",
"stderr": "",
"stdout": "user"
}
}
TASK [debug] ****************************************************************************************************************
ok: [localhost] => {
"msg": "Logged in as user user"
}
The changed key is present in the return value of all Ansible modules, and Ansible uses it to determine whether a state change has occurred. For the command and shell modules, this will always be set to true unless overridden with the changed_when clause
- name: Capture output of whoami command
command: whoami
register: login
changed_when: false
The cmd key contains the invoked command as a list of strings.
The rc key contains the return code. If it is nonzero, Ansible willassume the task failed to execute successfully.
The stderr key contains any text written to standard error, as a single string.
The stdout key contains any text written to standard out, as a single string.
The stdout_lines key contains any text written to split by newlines, as a list of strings.
ACCESSING DICTIONARY KEYS IN A VARIABLE
If a variable contains a dictionary, you can access the keys of the dictionary by using either a dot (.) or a subscript ([])
{{ result.stat }}
{{ result['stat'] }}
result['stat']['mode']
result['stat'].mode
result.stat['mode']
result.stat.mode
- name: Display result.stat detail
debug: var=result['stat'][stat_key]
- name: Access dictionary keys using dot notation
debug:
msg: "The command was {{ login.cmd }} and the return code was {{ login.rc }}"
- name: Access dictionary keys using subscript notation
debug:
msg: "The command was {{ login['cmd'] }} and the return code was {{ login['rc'] }}"
No comments:
Post a Comment