Templates and Filters

Templates

  • Use template module to dploy a jinja2 template file

- name: Make sure sshd_config is customized
  template:
    src: sshd_config.j2
    dest: /etc/ssh/sshd_config
    owner: root
    group: root
    mode: "0600"
    setype: etc_t

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, ansible_facts, structured as a dictionary

  • They include network addresses, hostnames, storage configuration, operating system, and many other things

# displays all facts for the managed host, and then just the fact that has the list of IPv4 addresses for the managed host
- name: Display some facts
  hosts: all
  
  tasks:
    - name: Display all facts
      debug:
        var: ansible_facts
    - name: Display a list of all IPv4 addresses
      debug:
        var: ansible_facts['all_ipv4_addresses']

Some other facts

  • ansible_facts['fqdn']

  • ansible_facts['default_ipv4']['address']

Template Syntax

Comment

  • {# Comment #}

For loop

  • The for statement provides a way to loop over a set of items

  • groups['all'] is a special variable that lists all the members of group all

  • The following example Jinja2 template file uses a for statement to set the variable host to each item in the groups['all'] list, in turn

  • hostvars['host'] is another special variable contains the facts for the current value of host

Conditionals

  • Jinja2 templates use the syntax

for expressions or logic.

  • You can use these expressions in template files but you should not use them in Ansible Playbooks.

  • The if/endif statements allow you to put content in a deployed file based on whether another variable is set.

Filters

  • Filters are used to modify or process the value from the variable

  • Some filters are provided by the Jinja2 language itself

  • Others are extensions included with Ansible as plug-ins.

  • It is also possible to create custom filters

  • Information about the filters that are available is at: https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html

  • Filters can be incredibly useful to prepare data for use in your playbook or template

  • 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

List intersect filter

Processing Variables with Jinja2 Filters

  • Filters do not change the value stored in the variable.

  • The Jinja2 expression processes that value and uses the result without changing the variable itself.

  • Learn more by reviewing the documentation at: https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html

Multiple Filters

Other Filters

ipaddr Filter

  • If passed a single IP address, it will return True if it is in the right format and False if it is not

  • If passed a list of IP addresses, it will return a list of the ones that are valid

list of filters

  • mandatory

  • default

  • int/float

  • min/max/sum

  • first/last/length

  • unique

  • union

  • intersect

  • difference

  • combine

  • dict2items

  • items2dict

  • lower/upper

  • capitalize

  • random

  • reverse

  • sort

  • flatten

Templating External Data with Lookup Plugins

  • A lookup plugin is an Ansible extension to the Jinja2 templating language

  • They import and format data from external sources for use in variables and templates

  • Allows you to use the contents of a file as a value for a variable

  • Allows you to look up information from other sources and put them in a template

  • ansible-doc -t lookup -l will list all available lookup plugins

  • ansible-doc -t lookup file will display documentation for the file lookup plugin

Lookup and Query

  • There are two ways to call a lookup plugin:

    • lookup returns a string of comma-separated items

    • query returns an actual YAML list of items

  • query is often easier to use without further processing

Dig Lookup Plugin

  • use the dig lookup plugin to look up the DNS MX records for gmail.com and returns a list where each item is one record. It then prints the list one item at a time

File Lookup Plugin

  • The file lookup plugin can be used to load the contents of a file into a variable

  • If a relative path is provided, the plugin looks for files in the playbook's files directory

  • use the authorized_key module to copy the contents of files/naoko.key.pub into the ~naoko/.ssh/authorized_keys file for user naoko on each managed host.

  • lookup plugin is used because the value of key must be her actual public key, not a file name

Lines Lookup Plugin

  • 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

  • Uses lines to build a list consisting of the lines in the /etc/passwd file.

  • It then loops over that list, using the debug module and the regex_replace filter to print out the name of each user account listed in that file.

Template Lookup Plugin

  • The template lookup plugin will take a Jinja2 template and evaluate that when setting the value.

  • If a relative path is passed to the template, Ansible will look in the playbook's templates directory.

  • Content of templates/my.template.j2: Hello {{ my_name }}!

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

Resource

Last updated