using-jinja2-templates-and-filters-slides
using-jinja2-templates-and-filters-slides
1
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Deploying Files with Jinja2 Templates
2
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Objectives
3
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Deploying Files to Managed hosts
● There are a number of Ansible modules that can be used to deploy files, including:
○ copy to copy a file to the managed hosts
○ file to make sure a file, directory, or link exists (or does not) and has certain settings
○ synchronize to copy entire directories of content
● You can also edit files in place with Ansible modules
○ lineinfile to make sure a certain line exists in a file, for example
● However, what if a file you want to deploy needs to be customized for each managed host?
○ Use Jinja2 template files and the template module
4
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Jinja2 Template Files
5
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Deploying Jinja2 Templates
6
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Jinja2 Templates and Facts
● Ansible facts are special variables that contain information unique to the managed host
● By default, they are set by an implied task at the start of the play (Gathering Facts)
● You can also collect facts at any time by running the setup module
● These facts are stored in a special variable, - name: Display some facts
ansible_facts, structured as a dictionary hosts: all
● They include network addresses, hostnames, tasks:
storage configuration, operating system, and - name: Display all facts
many other things debug:
var: ansible_facts
● The example play at right displays all facts
for the managed host, and then just the fact - name: Display a list of all IPv4 addresses
debug:
that has the list of IPv4 addresses for the var: ansible_facts['all_ipv4_addresses']
managed host
7
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Example: Jinja2 Templates and Facts
● At top right is a Jinja2 template, motd.j2, to be
This host is {{ ansible_facts['fqdn'] }}
deployed as /etc/motd on the managed hosts.
The fact ansible_facts['fqdn'] will be replaced Unauthorized access is prohibited.
with the fully-qualified DNS name of the host.
8
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Example of a Comment
● You can use the syntax {# COMMENT #} for comments that should not appear in the final file.
● In the following example, the first line includes a comment that will not be included in the final file.
The variable references in the second line are replaced with the values of the system facts being
referenced.
{# /etc/hosts line #}
{{ ansible_facts['default_ipv4']['address'] }} {{ ansible_facts['fqdn'] }}
9
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Control Structures in Jinja2 Template Files
● The result of this template is to create a file in /etc/hosts format containing the IPv4 address and
FQDN of every host in the inventory
10
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Using Jinja2 Conditionals
11
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Processing Variables with Jinja2 Filters
12
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Objectives
13
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Jinja2 Filters
● Filters can be incredibly useful to prepare data for use in your playbook or template.
14
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Processing Data with Jinja2 Filters
● To apply a filter to a variable:
○ Reference the variable, but follow its name with a pipe character
○ After the pipe character, add the name of the filter you want to apply
○ Some filters might require additional arguments or options in parentheses
○ You can chain multiple filters in a pipeline
● For example, the capitalize filter capitalizes the first letter of a string
● Assume the value of myname is james, and you have the following Jinja2 statement:
{{ myname | capitalize }}
15
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Multiple filters
● The next example shows multiple filters used together
● The unique filter gets a unique set of items in a list, removing duplicates
● The sort filter sorts a list of items
- name: Multiple filter example
hosts: localhost
● The play at right filters mylist through two vars:
filters, unique and sort mylist:
- 3
- 9
● The resulting output will be the list below: - 1
- 7
-1 - 9
-3 tasks:
-7 - name: Display sorted list of unique items
-9 debug:
msg: "{{ mylist | unique | sort }}"
16
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Example: The ipaddr Filter
● As an example, the ipaddr filter can perform a number of operations on IP addresses
17
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Example: The ipaddr Filter
● A more complex example actually uses ipaddr to reformat the output
Important
● There are a large number of filters available, both as standard filters from Jinja2 and as additional
filters provided by Ansible, too many to cover in a few minutes.
● A small selection of the filters you should investigate on your own include:
20
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Objectives
21
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Using Lookup Plugins to Import Data
● ansible-doc -t lookup file will display documentation for the file lookup plugin
22
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Lookup and Query
The example at right will use - name: Example of a lookup plugin in use
the dig lookup plugin to look hosts: all
vars:
up the DNS MX records for mxvar: "{{ query('dig', 'gmail.com', 'qtype=MX') }}"
gmail.com and returns a list tasks:
where each item is one record. - name: List each MX record for gmail.com
debug:
msg: An MX record is: {{ item }}
It then prints the list one item loop: "{{ mxvar }}"
at a time.
23
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Example: File Lookups
● The file lookup plugin can be used to load the contents of a file into a variable
● If you provide a relative path, the plugin looks for files in the playbook's files directory
The play at right will look use the - name: Add authorized keys
authorized_key module to copy the hosts: all
contents of files/naoko.key.pub vars:
users:
into the ~naoko/.ssh/authorized_keys - naoko
file for user naoko on each managed tasks:
host. - name: Add authorized keys
authorized_key:
user: "{{ item }}"
We use the lookup plugin because the key: "{{ lookup('file', item + '.key.pub') }}"
value of key must be her actual public loop: "{{ users }}"
key, not a file name.
24
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Example: Command Output Lookups with Lines as Items
● The lines lookup plugin will read output from a command, making each line an item in the list
● This can be useful in conjunction with filters
The example task at right uses - name: Print the name of each account in /etc/passwd
lines to build a list consisting of debug:
the lines in the /etc/passwd file. msg: A user is {{ item | regex_replace(':.*$') }}
loop: "{{ query('lines', 'cat /etc/passwd') }}"
25
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Example: Template Lookups
● The template lookup plugin will take a Jinja2 template and evaluate that when setting the value.
● If you pass a relative path to the template, Ansible will look in the playbook's templates directory.
● For example, assume that templates/my.template.j2 has the content: Hello {{ my_name }}!
26
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Example: URL Lookups
● The url lookup plugin is useful to grab the content of a web page or the output of an API
● This example talks to an Amazon API and prints the IPv4 and IPv6 networks used by AWS
tasks:
- name: display IPv4 ranges
debug:
msg: "{{ item['ip_prefix'] }}"
loop: "{{ amazon_ip_ranges['prefixes'] }}"
27
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.
Learning More about Lookup Plugins
● Lookup plugins are powerful, especially once you are skilled at using variables and filters
28
©2020 Red Hat, Inc., licensed to Pluralsight, LLC. All trademarks, service marks, and logos used herein are the property of their respective owners.