SlideShare a Scribd company logo
1 of 28
Download to read offline
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=value11
option12=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=value11
option12=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=value11
option12=value12
Templating defaults/variables file
Example - defaults/variables file
---
myfile_config_section1_option11 : value11
myfile_config_section1_option12 : value12
# /etc/myfile.cfg
[section1]
option11=value11
option12=value12
Example - defaults/variables file
---
myfile_config:
section1:
option11: value11
option12: value12
# /etc/myfile.cfg
[section1]
option11=value11
option12=value12
Example - defaults/variables file
---
myfile_section1_option11 : value11
myfile_section1_option12 : value12
myfile_config:
section1:
option11: "{{ myfile_section1_option11 }}"
option12: "{{ myfile_section1_option12 }}"
# /etc/myfile.cfg
[section1]
option11=value11
option12=value12
Example - defaults/variables file
---
myfile_section1_option11 : value11
myfile_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=value11
option12=value12
Example - defaults/variables file
---
myfile_section1_option11: value11
myfile_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=value11
option12=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=value11
option12=value12
Example - templating dict keys
---
myfile_section1_name : section1
myfile_config: "{
'{{ myfile_section1_name }}': {
'option11': 'value11',
'option12': 'value12'
}
}"
# /etc/myfile.cfg
[section1]
option11=value11
option12=value12
Inline YAML format with quoted keys and values.
---
myfile_section1_name: section1
myfile_section1_option11_name: option11
myfile_section1_option12_name: option12
myfile_section1_option11_value: value11
myfile_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=value11
option12=value12
Templating templates file
Example - templates file
[section1]
option11={{ myfile_config_section1_option11 }}
option12={{ myfile_config_section1_option12 }}
# /etc/myfile.cfg
[section1]
option11=value11
option12=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=value11
option12=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=value11
option12=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=value11
option12=value12
Example - templates file
# /etc/myfile.cfg
[section1]
option11=value11
option12=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=value11
option12=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 = value11
OPTION12 = 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?

More Related Content

What's hot

Introducing Command Line Applications with Ruby
Introducing Command Line Applications with RubyIntroducing Command Line Applications with Ruby
Introducing Command Line Applications with Ruby
Nikhil Mungel
 
Introduction to asynchronous DB access using Node.js and MongoDB
Introduction to asynchronous DB access using Node.js and MongoDBIntroduction to asynchronous DB access using Node.js and MongoDB
Introduction to asynchronous DB access using Node.js and MongoDB
Adrien Joly
 
YAPC::Asia 2010 Twitter解析サービス
YAPC::Asia 2010 Twitter解析サービスYAPC::Asia 2010 Twitter解析サービス
YAPC::Asia 2010 Twitter解析サービス
Yusuke Wada
 

What's hot (20)

Data driven testing using Integrant & Spec
Data driven testing using Integrant & SpecData driven testing using Integrant & Spec
Data driven testing using Integrant & Spec
 
Cakephpstudy5 hacks
Cakephpstudy5 hacksCakephpstudy5 hacks
Cakephpstudy5 hacks
 
Rest api with Python
Rest api with PythonRest api with Python
Rest api with Python
 
ECMAScript 6 and the Node Driver
ECMAScript 6 and the Node DriverECMAScript 6 and the Node Driver
ECMAScript 6 and the Node Driver
 
Django REST Framework
Django REST FrameworkDjango REST Framework
Django REST Framework
 
Mojo as a_client
Mojo as a_clientMojo as a_client
Mojo as a_client
 
Node.js System: The Approach
Node.js System: The ApproachNode.js System: The Approach
Node.js System: The Approach
 
Introducing Command Line Applications with Ruby
Introducing Command Line Applications with RubyIntroducing Command Line Applications with Ruby
Introducing Command Line Applications with Ruby
 
SPRAT - Spreadsheet API Tester
SPRAT - Spreadsheet API TesterSPRAT - Spreadsheet API Tester
SPRAT - Spreadsheet API Tester
 
Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101Web Apps in Perl - HTTP 101
Web Apps in Perl - HTTP 101
 
[React Native Tutorial] Lecture 3: More on ES6/ES2015
[React Native Tutorial] Lecture 3: More on ES6/ES2015[React Native Tutorial] Lecture 3: More on ES6/ES2015
[React Native Tutorial] Lecture 3: More on ES6/ES2015
 
Introduction to asynchronous DB access using Node.js and MongoDB
Introduction to asynchronous DB access using Node.js and MongoDBIntroduction to asynchronous DB access using Node.js and MongoDB
Introduction to asynchronous DB access using Node.js and MongoDB
 
QA Fest 2015. Яков Крамаренко. Polyglot automation
QA Fest 2015. Яков Крамаренко. Polyglot automation QA Fest 2015. Яков Крамаренко. Polyglot automation
QA Fest 2015. Яков Крамаренко. Polyglot automation
 
RESTful web services
RESTful web servicesRESTful web services
RESTful web services
 
Running BabelJS on Windows (Try ES6 on Windows)
Running BabelJS on Windows (Try ES6 on Windows)Running BabelJS on Windows (Try ES6 on Windows)
Running BabelJS on Windows (Try ES6 on Windows)
 
YAPC::Asia 2010 Twitter解析サービス
YAPC::Asia 2010 Twitter解析サービスYAPC::Asia 2010 Twitter解析サービス
YAPC::Asia 2010 Twitter解析サービス
 
Runmodes and Configs for Fun and Profit
Runmodes and Configs for Fun and ProfitRunmodes and Configs for Fun and Profit
Runmodes and Configs for Fun and Profit
 
Asynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingAsynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time Messaging
 
Workshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJSWorkshop 8: Templating: Handlebars, DustJS
Workshop 8: Templating: Handlebars, DustJS
 
Play á la Rails
Play á la RailsPlay á la Rails
Play á la Rails
 

Similar to Templating in ansible

List command linux fidora
List command linux fidoraList command linux fidora
List command linux fidora
Jinyuan Loh
 
Puppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet Forge
Puppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet ForgePuppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet Forge
Puppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet Forge
Puppet
 
[PyConTW 2013] Write Sublime Text 2 Packages with Python
[PyConTW 2013] Write Sublime Text 2 Packages with Python[PyConTW 2013] Write Sublime Text 2 Packages with Python
[PyConTW 2013] Write Sublime Text 2 Packages with Python
Jenny Liang
 
Debugging and Error handling
Debugging and Error handlingDebugging and Error handling
Debugging and Error handling
Suite Solutions
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
monicafrancis71118
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
cargillfilberto
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
drandy1
 

Similar to Templating in ansible (20)

Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Automation and Ansible
Automation and AnsibleAutomation and Ansible
Automation and Ansible
 
CMake Tutorial
CMake TutorialCMake Tutorial
CMake Tutorial
 
Love Twig
Love TwigLove Twig
Love Twig
 
List command linux fidora
List command linux fidoraList command linux fidora
List command linux fidora
 
It kpi maillist template engine
It kpi maillist template engineIt kpi maillist template engine
It kpi maillist template engine
 
Terminal linux commands_ Fedora based
Terminal  linux commands_ Fedora basedTerminal  linux commands_ Fedora based
Terminal linux commands_ Fedora based
 
Puppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet Forge
Puppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet ForgePuppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet Forge
Puppet Camp Amsterdam 2015: How To Leverage The Power of the Puppet Forge
 
[PyConTW 2013] Write Sublime Text 2 Packages with Python
[PyConTW 2013] Write Sublime Text 2 Packages with Python[PyConTW 2013] Write Sublime Text 2 Packages with Python
[PyConTW 2013] Write Sublime Text 2 Packages with Python
 
Debugging and Error handling
Debugging and Error handlingDebugging and Error handling
Debugging and Error handling
 
Mojolicious
MojoliciousMojolicious
Mojolicious
 
The Ring programming language version 1.5.1 book - Part 9 of 180
The Ring programming language version 1.5.1 book - Part 9 of 180The Ring programming language version 1.5.1 book - Part 9 of 180
The Ring programming language version 1.5.1 book - Part 9 of 180
 
Mastering Kotlin Standard Library
Mastering Kotlin Standard LibraryMastering Kotlin Standard Library
Mastering Kotlin Standard Library
 
Mule data weave_10
Mule data weave_10Mule data weave_10
Mule data weave_10
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
Function
FunctionFunction
Function
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docxCOMM 166 Final Research Proposal GuidelinesThe proposal should.docx
COMM 166 Final Research Proposal GuidelinesThe proposal should.docx
 
Crafting Beautiful CLI Applications in Ruby
Crafting Beautiful CLI Applications in RubyCrafting Beautiful CLI Applications in Ruby
Crafting Beautiful CLI Applications in Ruby
 
Ansible - Introduction
Ansible - IntroductionAnsible - Introduction
Ansible - Introduction
 

More from jtyr (10)

Ansible Inventory Plugins
Ansible Inventory PluginsAnsible Inventory Plugins
Ansible Inventory Plugins
 
Managing VMware VMs with Ansible
Managing VMware VMs with AnsibleManaging VMware VMs with Ansible
Managing VMware VMs with Ansible
 
How does Ansible's agentless architecture work?
How does Ansible's agentless architecture work?How does Ansible's agentless architecture work?
How does Ansible's agentless architecture work?
 
Managing multiple environments with Ansible
Managing multiple environments with AnsibleManaging multiple environments with Ansible
Managing multiple environments with Ansible
 
Jinja2 filters
Jinja2 filtersJinja2 filters
Jinja2 filters
 
Make the prompt great again
Make the prompt great againMake the prompt great again
Make the prompt great again
 
Development of Ansible modules
Development of Ansible modulesDevelopment of Ansible modules
Development of Ansible modules
 
Best practices for ansible roles development
Best practices for ansible roles developmentBest practices for ansible roles development
Best practices for ansible roles development
 
Overcoming problems of the standard Ansible inventory file
Overcoming problems of the standard Ansible inventory fileOvercoming problems of the standard Ansible inventory file
Overcoming problems of the standard Ansible inventory file
 
LEGO IR Controller
LEGO IR ControllerLEGO IR Controller
LEGO IR Controller
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
The Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and InsightThe Zero-ETL Approach: Enhancing Data Agility and Insight
The Zero-ETL Approach: Enhancing Data Agility and Insight
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Design and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data ScienceDesign and Development of a Provenance Capture Platform for Data Science
Design and Development of a Provenance Capture Platform for Data Science
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
TEST BANK For Principles of Anatomy and Physiology, 16th Edition by Gerard J....
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 

Templating in ansible

  • 2. 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
  • 3. What is Ansible templating?
  • 4. 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
  • 5. 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 ○ ...
  • 7. 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=value11 option12=value12
  • 8. 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=value11 option12=value12
  • 9. 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=value11 option12=value12
  • 11. Example - defaults/variables file --- myfile_config_section1_option11 : value11 myfile_config_section1_option12 : value12 # /etc/myfile.cfg [section1] option11=value11 option12=value12
  • 12. Example - defaults/variables file --- myfile_config: section1: option11: value11 option12: value12 # /etc/myfile.cfg [section1] option11=value11 option12=value12
  • 13. Example - defaults/variables file --- myfile_section1_option11 : value11 myfile_section1_option12 : value12 myfile_config: section1: option11: "{{ myfile_section1_option11 }}" option12: "{{ myfile_section1_option12 }}" # /etc/myfile.cfg [section1] option11=value11 option12=value12
  • 14. Example - defaults/variables file --- myfile_section1_option11 : value11 myfile_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=value11 option12=value12
  • 15. Example - defaults/variables file --- myfile_section1_option11: value11 myfile_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=value11 option12=value12
  • 17. Example - templating dict keys --- myfile_section1_name : section1 myfile_config: "{{ myfile_section1_name }}": option11: value11 option12: value12 # /etc/myfile.cfg [{{ myfile_section1_name }} ] option11=value11 option12=value12
  • 18. Example - templating dict keys --- myfile_section1_name : section1 myfile_config: "{ '{{ myfile_section1_name }}': { 'option11': 'value11', 'option12': 'value12' } }" # /etc/myfile.cfg [section1] option11=value11 option12=value12 Inline YAML format with quoted keys and values.
  • 19. --- myfile_section1_name: section1 myfile_section1_option11_name: option11 myfile_section1_option12_name: option12 myfile_section1_option11_value: value11 myfile_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=value11 option12=value12
  • 21. Example - templates file [section1] option11={{ myfile_config_section1_option11 }} option12={{ myfile_config_section1_option12 }} # /etc/myfile.cfg [section1] option11=value11 option12=value12
  • 22. 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=value11 option12=value12
  • 23. 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=value11 option12=value12
  • 24. 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=value11 option12=value12
  • 25. Example - templates file # /etc/myfile.cfg [section1] option11=value11 option12=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 %}
  • 26. Example - templates file # /etc/myfile.cfg [section1] option11=value11 option12=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 %}
  • 27. Example - templates file # /etc/myfile.cfg [section1] OPTION11 = value11 OPTION12 = 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 %}
  • 28. Thank you for your attention! Questions?