Templating (Jinja2) — Ansible Documentation

- AnsibleFest
- Products
- Community
- Webinars & Training
- Blog

Documentation

# Templating (Jinja2)

As already referenced in the variables section, Ansible uses Jinja2 templating to enable dynamic expressions and access to variables. Ansible greatly expands the number of filters and tests available, as well as adding a new plugin type: lookups.

Please note that all templating happens on the Ansible controller before the task is sent and executed on the target machine. This is done to minimize the requirements on the target (jinja2 is only required on the controller) and to be able to pass the minimal information needed for the task, so the target machine does not need a copy of all the data that the controller has access to.

Topics

- Templating (Jinja2)
  - Get the current time

- Filters
  - Filters For Formatting Data
  - Forcing Variables To Be Defined
  - Defaulting Undefined Variables
  - Omitting Parameters
  - List Filters
  - Set Theory Filters
  - Dict Filter
  - items2dict filter
  - zip and zip_longest filters
  - subelements Filter
  - Random Mac Address Filter
  - Random Number Filter
  - Shuffle Filter
  - Math
  - JSON Query Filter
  - IP address filter
  - Network CLI filters
  - Network XML filters
  - Hashing filters
  - Combining hashes/dictionaries
  - Extracting values from containers
  - Comment Filter
  - URL Split Filter
  - Regular Expression Filters
  - Kubernetes Filters
  - Other Useful Filters
  - Combination Filters
  - Product Filters
  - Debugging Filters
  - Computer Theory Assertions
  - Human Readable
  - Human to Bytes
- Tests
  - Test syntax
  - Testing strings
  - Version Comparison
  - Set theory tests
  - Test if a list contains a value
  - Testing paths
  - Task results
- Lookups
  - Lookups and loops
  - Lookups and variables
- Python Version and Templating
  - Dictionary Views
  - dict.iteritems()

## Get the current time

New in version 2.8.

The `now()` Jinja2 function, allows you to retrieve python datetime object or a string representation for the current time.

The `now()` function supports 2 arguments:

utc
  Specify `True` to get the current time in UTC. Defaults to `False`
fmt
  Accepts a strftime string that will be used to return a formatted date time string

See also

Working With Playbooks
  An introduction to playbooks
Conditionals
  Conditional statements in playbooks
Loops
  Looping in playbooks
Roles
  Playbook organization by roles
Best Practices
  Best practices in playbooks
User Mailing List
  Have a question? Stop by the google group!
irc.freenode.net
  #ansible IRC chat channel