diff --git a/ansible.cfg b/ansible.cfg new file mode 100644 index 0000000..fa45324 --- /dev/null +++ b/ansible.cfg @@ -0,0 +1,3 @@ +[defaults] +jinja2_extensions = jinja2.ext.do +retry_files_enabled = False diff --git a/load-vars/load-vars.yaml b/load-vars/load-vars.yaml new file mode 100644 index 0000000..f34fe03 --- /dev/null +++ b/load-vars/load-vars.yaml @@ -0,0 +1,30 @@ + +- name: Variables loaded dynamically are available everywhere in the playbook + gather_facts: no + hosts: localhost + tasks: + - include_vars: extra.yaml + roles: + - my-vars + post_tasks: + - debug: + var: extra + - debug: + var: extra_role + - assert: + that: + - extra is defined + - extra_role is defined + +- name: Even in another playbook ! + gather_facts: no + hosts: localhost + tasks: + - debug: + var: extra + - debug: + var: extra_role + - assert: + that: + - extra is defined + - extra_role is defined diff --git a/load-vars/roles/my-vars/tasks/main.yaml b/load-vars/roles/my-vars/tasks/main.yaml new file mode 100644 index 0000000..f68cea1 --- /dev/null +++ b/load-vars/roles/my-vars/tasks/main.yaml @@ -0,0 +1,4 @@ +--- + +- name: Include extra variables dynamically + include_vars: extra.yaml \ No newline at end of file diff --git a/load-vars/roles/my-vars/vars/extra.yaml b/load-vars/roles/my-vars/vars/extra.yaml new file mode 100644 index 0000000..806d95d --- /dev/null +++ b/load-vars/roles/my-vars/vars/extra.yaml @@ -0,0 +1,3 @@ +--- + +extra_role: "I'm also there !" \ No newline at end of file diff --git a/load-vars/vars/extra.yaml b/load-vars/vars/extra.yaml new file mode 100644 index 0000000..3574a18 --- /dev/null +++ b/load-vars/vars/extra.yaml @@ -0,0 +1,2 @@ +--- + extra: "Yes, I'm there !" \ No newline at end of file diff --git a/selectattr-filter.yaml b/selectattr-filter.yaml new file mode 100644 index 0000000..e952c5b --- /dev/null +++ b/selectattr-filter.yaml @@ -0,0 +1,34 @@ +--- + +- name: The selectattr filter can be used to filter a list, based on a criteria + gather_facts: no + hosts: localhost + vars: + input: + - id: 2555418101286 + system_name: hits + - id: 2555418119094 + system_name: Say_Hello + - id: 2555418119095 + system_name: Say_Goodbye + tasks: + - block: + - debug: + var: output + - assert: + that: + - output|length == 1 + - (output|first).system_name == 'Say_Hello' + vars: + output: "{{ input|selectattr('id', 'equalto', id)|list }}" + id: 2555418119094 + - block: + - debug: + var: output + - assert: + that: + - output|length == 1 + - (output|first).id == 2555418119094 + vars: + output: "{{ input|selectattr('system_name', 'equalto', system_name)|list }}" + system_name: Say_Hello diff --git a/set-theory-filters.yaml b/set-theory-filters.yaml new file mode 100644 index 0000000..dc227fe --- /dev/null +++ b/set-theory-filters.yaml @@ -0,0 +1,25 @@ +--- + +- name: 'Set Theory Filters: union operates on set of structured objects' + gather_facts: no + hosts: localhost + tasks: + - block: + - debug: + var: output + - assert: + that: + - output|length == 3 + vars: + output: "{{ input1|union(input2) }}" + input1: + - id: 1 + name: john + - id: 3 + name: smith + input2: + - id: 1 + name: john + - id: 2 + name: jane + diff --git a/variables-in-templates/templates/sub-template.j2 b/variables-in-templates/templates/sub-template.j2 new file mode 100644 index 0000000..2afeb8d --- /dev/null +++ b/variables-in-templates/templates/sub-template.j2 @@ -0,0 +1 @@ +{{ input|default("KO") }}{{ input_template|default("KO") }}OK \ No newline at end of file diff --git a/variables-in-templates/templates/template.j2 b/variables-in-templates/templates/template.j2 new file mode 100644 index 0000000..470f15e --- /dev/null +++ b/variables-in-templates/templates/template.j2 @@ -0,0 +1,2 @@ +{%- set input_template = "OK" -%} +{{ lookup('template', 'sub-template.j2') }} \ No newline at end of file diff --git a/variables-in-templates/variables-in-templates.yaml b/variables-in-templates/variables-in-templates.yaml new file mode 100644 index 0000000..7dc838d --- /dev/null +++ b/variables-in-templates/variables-in-templates.yaml @@ -0,0 +1,16 @@ +--- + +- name: 'Variable in templates: a template can use a variable from task or playbook but not from an outter template' + gather_facts: no + hosts: localhost + vars: + input: 'OK' + tasks: + - block: + - debug: + var: output + - assert: + that: + - output == "OKKOOK" + vars: + output: "{{ lookup('template', 'template.j2') }}"