Templating in ansible
-
Upload
jtyr -
Category
Technology
-
view
67 -
download
0
Transcript of Templating in ansible
Templating in Ansible
Jiri Tyr
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
What is Ansible templating?
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
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○ ...
Templating tasks
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
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
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
Templating defaults/variables file
Example - defaults/variables file---
myfile_config_section1_option11 : value11myfile_config_section1_option12 : value12
# /etc/myfile.cfg[section1]option11=value11option12=value12
Example - defaults/variables file---
myfile_config: section1: option11: value11 option12: value12
# /etc/myfile.cfg[section1]option11=value11option12=value12
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
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
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
Templating dict key
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
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.
---
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
Templating templates file
Example - templates file[section1]option11={{ myfile_config_section1_option11 }}option12={{ myfile_config_section1_option12 }}
# /etc/myfile.cfg[section1]option11=value11option12=value12
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
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
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
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 %}
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 %}
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 %}
Thank you for your attention!
Questions?