templating in ansible

28
Templating in Ansible Jiri Tyr

Upload: jtyr

Post on 17-Mar-2018

68 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Templating in ansible

Templating in Ansible

Jiri Tyr

Page 2: Templating in ansible

About me● Using Ansible for 3+ years● Ansible contributor

○ Modules: yum_repository, jenkins_plugin, ldap_attr, ldap_entry○ Jinja2 filter: comment○ Bug fixing (mount module)○ Code reviews

● Author of nearly 100 public Ansible roles○ https://github.com/jtyr○ https://galaxy.ansible.com/jtyr

Page 3: Templating in ansible

What is Ansible templating?

Page 4: Templating in ansible

What is templating● Allows dynamic expressions and access to variables● Used in

○ Tasks○ Defaults/Variables○ Templates

● Happens before the task is sent for execution to target machine● Using Jinja2 language

Page 5: Templating in ansible

Jinja2 language● Modern and designer-friendly templating language for Python● Heavily inspired by Django and Python● Generates any text-based format● Consists of tags

○ {% %} - statement○ {{ }} - expression○ {# #} - comment

● Tags can contain○ Flow controls

■ if / else■ for

○ Filters○ ...

Page 6: Templating in ansible

Templating tasks

Page 7: Templating in ansible

Example - tasks---

- hosts: all tasks: - template: src: myfile.cfg .j2 dest: /etc/myfile.cfg owner: root group: root mode: 0644

# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 8: Templating in ansible

Example - tasks---

- hosts: all vars: filename: /etc/myfile.cfg owner: root group: root mode: 0644 tasks: - template: src: myfile.cfg.j2 dest: "{{ filename }}" owner: "{{ owner }}" group: "{{ group }}" mode: "{{ mode }}"

# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 9: Templating in ansible

Example - tasks---

- hosts: all vars: filename: /etc/myfile.cfg owner: root group: root mode: "{{ 0644 if owner == 'root' else 0640 }}" tasks: - template: src: myfile.cfg.j2 dest: "{{ filename }}" owner: "{{ owner }}" group: "{{ group }}" mode: "{{ mode }}"

# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 10: Templating in ansible

Templating defaults/variables file

Page 11: Templating in ansible

Example - defaults/variables file---

myfile_config_section1_option11 : value11myfile_config_section1_option12 : value12

# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 12: Templating in ansible

Example - defaults/variables file---

myfile_config: section1: option11: value11 option12: value12

# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 13: Templating in ansible

Example - defaults/variables file---

myfile_section1_option11 : value11myfile_section1_option12 : value12

myfile_config: section1: option11: "{{ myfile_section1_option11 }}" option12: "{{ myfile_section1_option12 }}"

# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 14: Templating in ansible

Example - defaults/variables file---

myfile_section1_option11 : value11myfile_section1_option12 : value12

myfile_option11: option11: "{{ myfile_section1_option11 }}"myfile_option12: option12: "{{ myfile_section1_option12 }}"

myfile_section1: "{{ myfile_option11.update(myfile_option12) }}{{ myfile_option11 }}"

myfile_config: section1: "{{ myfile_section1 }}"

# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 15: Templating in ansible

Example - defaults/variables file---

myfile_section1_option11: value11myfile_section1_option12: value12

myfile_section1__default: option11: "{{ myfile_section1_option11 }}" option12: "{{ myfile_section1_option12 }}"

myfile_section1__custom: []

myfile_section1: "{{ myfile_section1__default.update(myfile_section1__custom)}}{{ myfile_section1__default}}"

myfile_config__default: section1: "{{ myfile_section1 }}"

myfile_config__custom: {}

myfile_config: "{{ myfile_config__default.update(myfile_config__custom) }}{{ myfile_config__default }}"

# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 16: Templating in ansible

Templating dict key

Page 17: Templating in ansible

Example - templating dict keys---

myfile_section1_name : section1

myfile_config: "{{ myfile_section1_name }}": option11: value11 option12: value12

# /etc/myfile.cfg[{{ myfile_section1_name }} ]option11=value11option12=value12

Page 18: Templating in ansible

Example - templating dict keys---

myfile_section1_name : section1

myfile_config: "{ '{{ myfile_section1_name }}': { 'option11': 'value11', 'option12': 'value12' }}"

# /etc/myfile.cfg[section1]option11=value11option12=value12

Inline YAML format with quoted keys and values.

Page 19: Templating in ansible

---

myfile_section1_name: section1myfile_section1_option11_name: option11myfile_section1_option12_name: option12myfile_section1_option11_value: value11myfile_section1_option12_value: value12

myfile_section1: "{ '{{ myfile_section1_option11_name }}': '{{ myfile_section1_option11_value }}', '{{ myfile_section1_option11_name }}': '{{ myfile_section1_option11_value }}'}"

myfile_config: "{ '{{ myfile_section1_name }}': {{ myfile_section1 }}

}"

Example - templating dict keys# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 20: Templating in ansible

Templating templates file

Page 21: Templating in ansible

Example - templates file[section1]option11={{ myfile_config_section1_option11 }}option12={{ myfile_config_section1_option12 }}

# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 22: Templating in ansible

Example - templates file{% for sect, opts in myfile_config.iteritems() %}{# This is the section #}[{{ sect }}]{% for opt, val in opts.iteritems() %}{# This is the key-value #}{{ opt }}={{ val }}{% endfor %}{% endfor %}

# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 23: Templating in ansible

Example - templates file{% for sect, opts in myfile_config.iteritems() %}{# This is the section #}[{{ sect }}]{% for opt, val in opts.iteritems() %}{# This is the key-value #}{{ opt }}={{ val }}{% endfor %}{% endfor %}

# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 24: Templating in ansible

Example - templates file{% for sect, opts in myfile_config.iteritems() %}{# This is the section #}{# #}[{{ sect }}]{% for opt, val in opts.iteritems() %}{# This is the key-value #}{# #}{{ opt }}={{ val }}{% endfor %}{% endfor %}

# /etc/myfile.cfg[section1]option11=value11option12=value12

Page 25: Templating in ansible

Example - templates file# /etc/myfile.cfg[section1]option11=value11option12=value12

{% for sect, opts in myfile_config.iteritems() %} {#- This is the section -#} [{{ sect }}]{{ "\n" }} {%- for opt, val in opts.iteritems() %} {#- This is the key-value -#} {{ opt }}={{ val }}{{ "\n" }} {%- endfor %}{% endfor %}

Page 26: Templating in ansible

Example - templates file# /etc/myfile.cfg[section1]option11=value11option12=value12

{% for sect, opts in myfile_config.iteritems() %} {#- This is the section -#} [{{ sect }}]{{ "\n" }} {%- for opt, val in opts.iteritems() %} {#- This is the key-value -#} {{ opt ~ "=" ~ val ~ "\n" }} {%- endfor %}{% endfor %}

Page 27: Templating in ansible

Example - templates file# /etc/myfile.cfg[section1]OPTION11 = value11OPTION12 = value12

{% for sect, opts in myfile_config.iteritems() %} {#- This is the section -#} [{{ sect }}]{{ "\n" }} {%- for opt, val in opts.iteritems() %} {#- This is the key-value -#} {{ opt | upper ~ myfile_config_sep ~ val ~ "\n" }} {%- endfor %}{% endfor %}

Page 28: Templating in ansible

Thank you for your attention!

Questions?