Browse Source

new examples

master
Nicolas Massé 7 years ago
parent
commit
bd53c7a366
  1. 3
      ansible.cfg
  2. 30
      load-vars/load-vars.yaml
  3. 4
      load-vars/roles/my-vars/tasks/main.yaml
  4. 3
      load-vars/roles/my-vars/vars/extra.yaml
  5. 2
      load-vars/vars/extra.yaml
  6. 34
      selectattr-filter.yaml
  7. 25
      set-theory-filters.yaml
  8. 1
      variables-in-templates/templates/sub-template.j2
  9. 2
      variables-in-templates/templates/template.j2
  10. 16
      variables-in-templates/variables-in-templates.yaml

3
ansible.cfg

@ -0,0 +1,3 @@
[defaults]
jinja2_extensions = jinja2.ext.do
retry_files_enabled = False

30
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

4
load-vars/roles/my-vars/tasks/main.yaml

@ -0,0 +1,4 @@
---
- name: Include extra variables dynamically
include_vars: extra.yaml

3
load-vars/roles/my-vars/vars/extra.yaml

@ -0,0 +1,3 @@
---
extra_role: "I'm also there !"

2
load-vars/vars/extra.yaml

@ -0,0 +1,2 @@
---
extra: "Yes, I'm there !"

34
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

25
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

1
variables-in-templates/templates/sub-template.j2

@ -0,0 +1 @@
{{ input|default("KO") }}{{ input_template|default("KO") }}OK

2
variables-in-templates/templates/template.j2

@ -0,0 +1,2 @@
{%- set input_template = "OK" -%}
{{ lookup('template', 'sub-template.j2') }}

16
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') }}"
Loading…
Cancel
Save