Ansible - List Variable

About

List variable

Initialization

a_list: []
packages: ['mapr-compat-suse', 'mapr-core', 'mapr-core-internal']
another_list:
  - 1
  - 2
  - 3

or from two_sources

unixodbc_cfg_sources:
  - source_name: source 1
    driver_file: /file/1
    driver_properties:
      property_1: value_1
  - source_name: source 2
    driver_file: /file/2
    driver_properties:
      property_2_1: value_2_1
      property_2_2: value_2_2

Default

{{ mylist|default([]) }}

Management

Min

{{ list1 | min }}

Max

{{ [3, 4, 2] | max }}

List

{{ __java_packages | list }}

Flatten

  • Flatten a list (same thing the flatten lookup does):
{{ [3, [4, 2] ] | flatten }}
  • Flatten only the first level of a list (akin to the items lookup):
{{ [3, [4, [2]] ] | flatten(levels=1) }}

Add an element

{{ list }} + [ 'element' ] 
  • or two
{{ list }} + [ 'element', 'element2' ] 
  • or a more complex
[ 'configure' ] + {{ list_arguments }} + ['--add-module=../{{ my_var }}']

Concatenate two lists

{{ list1 + list2 }}

In

'webserver' in group_names

Join

{{ my_list | join(' ') }}
{{ my_list | join('\n') }}

Loop

Ansible - Loop

- name: Install mapr-core
  vars:
    packages_Suse: ['mapr-compat-suse', 'mapr-core', 'mapr-core-internal']
    packages_RedHat: ['mapr-core', 'mapr-core-internal']
    packages_Debian: ['mapr-core', 'mapr-core-internal']
  package: name={{ item }} state=present
  with_items: "{{ vars['packages_' + ansible_os_family] }}"
  register: core_result

Ref


Powered by ComboStrap