Difference: AnsibleTipsTricks (1 vs. 19)

Revision 1905 Jul 2023 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 121 to 121
 

Select items from list of dictionaries

Use "{{ variable | selectattr('key_name', 'match', 'value') | list }}"

Added:
>
>
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

Revision 1814 Jun 2023 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 32 to 32
  Results in:
Changed:
<
<
"msg": "This is the first part and second part of the string"
>
>
"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:

Revision 1712 Jun 2023 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 22 to 22
 Use a backslash (\) to split long lines:
Changed:
<
<
- fail:
>
>
- set_fact:
  msg: "This is the first part and second part of the string"
Added:
>
>
- debug: var: msg
 

Results in:

Deleted:
<
<
fatal: [localhost]: FAILED! => {"changed": false, "msg": "This is the first part and second part of the string"}
 
Changed:
<
<
For other ways of splitting long lines, see: https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines
>
>
"msg": "This is the first part and second part of the string"

Use a greater-than sign (>) to split long lines, without interior line breaks:

    - set_fact:
        msg: >
          This is the first part
          and second part of the string

    - debug:
        var: msg

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:

    - set_fact:
        msg: |
          This is the first part
          and second part of the string

    - debug:
        var: msg

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

Revision 1611 Jun 2023 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 148 to 148
  msg: "{{ ansible_facts[item]['mtu'] }}" with_items: "{{ ansible_interfaces }}"
Added:
>
>

Fixing incorrect module names

Run ansible-playbook with -vvv and look for lines containing redirecting.

  -- Ivo van Geel - 21 Sep 2021

Revision 1503 Mar 2023 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 75 to 75
 

Convert date to Unix time (epoch)

Changed:
<
<
Use "{{ ('2022-01-01' | to_datetime).strftime('%s') }}"
>
>
Use "{{ ('2023-02-28' | to_datetime('%Y-%m-%d')).strftime('%s') }}"
 

Extract subitems from list of dictionaries

Revision 1415 Dec 2022 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 66 to 66
  debug: msg: "{{ msg.split('\n') }}"
Added:
>
>

Magic variables

See: Special Variables

 

manipulating data

Revision 1312 Apr 2022 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 134 to 134
  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'
Added:
>
>

Loop over network interfaces

Example, showing MTU size:

- debug:
    msg: "{{ ansible_facts[item]['mtu'] }}"
  with_items: "{{ ansible_interfaces }}"
 

-- Ivo van Geel - 21 Sep 2021

Revision 1211 Feb 2022 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 87 to 87
 

Apply a filter to a list of items

Changed:
<
<
Use "{{ variable | map('', 'filter_option1', 'filter_option2') | list }}"
>
>
Use "{{ variable | map('filter_name', 'filter_option1', 'filter_option2') | list }}"
  For example:

Revision 1108 Feb 2022 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 14 to 14
 dictionary:
key1: "value 1"
key2: "value 2"
Added:
>
>

YAML syntax

Split long lines

Use a backslash (\) to split long lines:

- fail:
    msg: "This is the first part \
          and second part of the string"

Results in: fatal: [localhost]: FAILED! => {"changed": false, "msg": "This is the first part and second part of the string"}

For other ways of splitting long lines, see: https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines

 

Dump all variables

Revision 1028 Jan 2022 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 51 to 51
 

manipulating data

Added:
>
>

Convert date to Unix time (epoch)

Use "{{ ('2022-01-01' | to_datetime).strftime('%s') }}"

 

Extract subitems from list of dictionaries

Revision 917 Jan 2022 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 83 to 83
 

Sort list of dictionaries by subitem

Use "{{ variable | sort(attribute='key_name') }}"

Added:
>
>

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

Revision 816 Dec 2021 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 63 to 63
 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

Added:
>
>

Apply a filter to a list of items

Use "{{ variable | map('', '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

Revision 707 Dec 2021 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 63 to 63
 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

Added:
>
>

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

Revision 617 Nov 2021 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 67 to 67
 

Sort list of dictionaries by subitem

Use "{{ variable | sort(attribute='key_name') }}"

Added:
>
>

Add a string to a string

Use "{{ string1 + string2 }}"

Or: "{{ string1 + ', ' + string2 }}"

 

Add item to list

Revision 517 Nov 2021 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 61 to 61
 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

Added:
>
>
See the Jinja2 documentation for a list of available tests: https://jinja.palletsprojects.com/en/2.9.x/templates/#list-of-builtin-tests
 

Sort list of dictionaries by subitem

Revision 416 Nov 2021 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 73 to 73
 

Add item to dictionary

Use "{{ variable | default({}) | combine({key_name: key_value}) }}"

Added:
>
>

Replace multiple lines using replace module (lineinfile does not support multi-line regexp)

Example:

- 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'
  -- Ivo van Geel - 21 Sep 2021

Revision 313 Oct 2021 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 54 to 54
 

Extract subitems from list of dictionaries

Changed:
<
<
Use "{{ variable | map(attribute='keyname') | list }}"
>
>
Use "{{ variable | map(attribute='key_name') | list }}"
 

Select items from list of dictionaries

Changed:
<
<
Use "{{ variable | selectattr('keyname', 'match', 'value') | list }}"
>
>
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

Changed:
<
<
Use "{{ variable | sort(attribute='keyname') }}"
>
>
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}) }}"

  -- Ivo van Geel - 21 Sep 2021

Revision 230 Sep 2021 - IvoVanGeel

Line: 1 to 1
 
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Line: 14 to 14
 dictionary:
key1: "value 1"
key2: "value 2"
Added:
>
>

Dump all variables

---
- 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') }}"
 

manipulating data

Revision 121 Sep 2021 - IvoVanGeel

Line: 1 to 1
Added:
>
>
META TOPICPARENT name="WebHome"

Ansible tips & tricks

Ansible data types

integer: 3211241
boolean: true/false
float: 3.141592653
string: "qwerty"
list:
- "item 1"
- "item 2"
dictionary:
key1: "value 1"
key2: "value 2"

manipulating data

Extract subitems from list of dictionaries

Use "{{ variable | map(attribute='keyname') | list }}"

Select items from list of dictionaries

Use "{{ variable | selectattr('keyname', '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='keyname') }}"

-- Ivo van Geel - 21 Sep 2021

 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2010-2019 by LANIS