---+ Ansible tips & tricks ---++ Ansible data types integer: 3211241 <br> boolean: true/false <br> float: 3.141592653 <br> string: "qwerty" <br> list: <br> - "item 1" <br> - "item 2" <br> dictionary: <br> key1: "value 1" <br> key2: "value 2" <br> ---++ YAML syntax ---+++ Split long lines Use a backslash (\) to split long lines: <verbatim> - set_fact: msg: "This is the first part \ and second part of the string" - debug: var: msg </verbatim> Results in: "msg": "This is the first part and second part of the string". Note that the space between 'part' and 'and' is before the backslash. Use a greater-than sign (>) to split long lines, without interior line breaks: <verbatim> - set_fact: msg: > This is the first part and second part of the string - debug: var: msg </verbatim> Results in: "msg": "This is the first part and second part of the string\n" Use a pipe sign (|) to split long lines, preserving interior line breaks: <verbatim> - set_fact: msg: | This is the first part and second part of the string - debug: var: msg </verbatim> Results in: "msg": "This is the first part \nand second part of the string\n" Add a minus sign (-) after a greater-than or pipe symbol to remove the line break at the end of the line (use >- or |-) See: https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines ---++ Dump all variables <verbatim> --- - name: dump all hosts: all tasks: - name: Print some debug information vars: msg: | Module Variables ("vars"): -------------------------------- {{ vars | to_nice_json }} Environment Variables ("environment"): -------------------------------- {{ environment | to_nice_json }} GROUP NAMES Variables ("group_names"): -------------------------------- {{ group_names | to_nice_json }} GROUPS Variables ("groups"): -------------------------------- {{ groups | to_nice_json }} HOST Variables ("hostvars"): -------------------------------- {{ hostvars | to_nice_json }} debug: msg: "{{ msg.split('\n') }}" </verbatim> ---++ Magic variables See: [[https://docs.ansible.com/ansible/latest/reference_appendices/special_variables.html][Special Variables]] ---++ manipulating data ---+++ Convert date to Unix time (epoch) Use "{{ ('2023-02-28' | to_datetime('%Y-%m-%d')).strftime('%s') }}" ---+++ Extract subitems from list of dictionaries Use "{{ variable | map(attribute='key_name') | list }}" ---+++ Select items from list of dictionaries Use "{{ variable | selectattr('key_name', 'match', 'value') | list }}" Instead of selectattr, you can also use rejectattr. Instead of match, you can also use search or regex. See: https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings See the Jinja2 documentation for a list of available tests: https://jinja.palletsprojects.com/en/2.9.x/templates/#list-of-builtin-tests ---+++ Apply a filter to a list of items Use "{{ variable | map('filter_name', 'filter_option1', 'filter_option2') | list }}" For example: "{{ output.stdout_lines | map('regex_replace', '^(\S+)\s.*$', '\\1') | list }}" Be careful on the context in which you use this. In some cases you must use a single backslash (displaying a variable with debug for example): '\1' ---+++ Filter a list of strings Use "{{ variable | select('match', 'some string') | list }}" Instead of match, you can also use search or regex. See: https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings ---+++ Sort list of dictionaries by subitem Use "{{ variable | sort(attribute='key_name') }}" ---+++ Searching a string in a when statement Use: when: my_string is match(pattern) Instead of match, you can also use search or regex. See: https://docs.ansible.com/ansible/latest/user_guide/playbooks_tests.html#testing-strings ---+++ Add a string to a string Use "{{ string1 + string2 }}" Or: "{{ string1 + ', ' + string2 }}" ---+++ Add item to list Use "{{ variable | default([]) + [item] }}" ---+++ Add item to dictionary Use "{{ variable | default({}) | combine({key_name: key_value}) }}" ---++ Replace multiple lines using replace module (lineinfile does not support multi-line regexp) Example: <verbatim> - name: Comment out hint zone "." in named.conf replace: path: "/etc/named.conf" regexp: '^(zone\s+"\."\s+IN\s+{[^\n]*)\n(\s*type\s+hint;[^\n]*)\n(\s*file\s+"named\.ca";[^\n]*)\n(};)' replace: '# \1\n# \2\n# \3\n# \4' </verbatim> ---++ Loop over network interfaces Example, showing MTU size: <verbatim> - debug: msg: "{{ ansible_facts[item]['mtu'] }}" with_items: "{{ ansible_interfaces }}" </verbatim> ---++ Fixing incorrect module names Run ansible-playbook with -vvv and look for lines containing redirecting. -- %USERSIG{IvoVanGeel - 21 Sep 2021}%
E
dit
|
A
ttach
|
Watch
|
P
rint version
|
H
istory
: r19
<
r18
<
r17
<
r16
<
r15
|
B
acklinks
|
V
iew topic
|
M
ore topic actions
Topic revision: r19 - 05 Jul 2023
-
IvoVanGeel
Home
Site map
LANIS web
Main web
Sandbox web
TWiki web
Main Web
Users
Groups
Index
Search
Changes
Notifications
RSS Feed
Statistics
Preferences
View
Raw View
Print version
Find backlinks
History
More topic actions
Edit
Raw edit
Attach file or image
Edit topic preference settings
Set new parent
More topic actions
Account
Log In
E
dit
A
ttach
Copyright © 2010-2019 by LANIS