---+ 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> ---++ 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> ---++ manipulating data ---+++ 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 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') }}" ---+++ Add item to list Use "{{ variable | default([]) + [item] }}" ---+++ Add item to dictionary Use "{{ variable | default({}) | combine({key_name: key_value}) }}" -- %USERSIG{IvoVanGeel - 21 Sep 2021}%
Edit
|
Attach
|
Watch
|
P
rint version
|
H
istory
:
r19
|
r5
<
r4
<
r3
<
r2
|
B
acklinks
|
V
iew topic
|
More topic actions...
Topic revision: r3 - 13 Oct 2021
-
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
Edit
Attach
Copyright © 2010-2019 by LANIS