[Ansible] I want to call my own function from the template module (macro)

Introduction

ansible 2.9.1

If you are using the template module, you may want to create your own function and call it. jinja2 has a function called macro, which calls a file in which a unique function is written from another j2 file. It seems that it can be used, but I will actually try it if it can also be used with the template module.

Reference file

(1)Playbook

template_sample.j2


---
- name: template TEST
  hosts: localhost
  gather_facts: no
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:
    - name: 'template'
      template:
        src: base.j2        #Playbook from which macro is called
        dest: result.txt

___ (2) j2 file (base.j2: caller file) ___ Line 1: Load macro in macro.j2, which is in the same directory as this j2 file 2nd line: Pass the variable to a unique function called test1 in macro.j2 Line 3: Pass the variable to a unique function called test2 in macro.j2

base.j2


{% import "macro.j2" as macro %}
{{ macro.test1("Taro", "Tokyo") }}
{{ macro.test2("Hanako", "tennis") }}

___ (3) j2 file (macro.j2: file in which original functions are written) ___ Start writing the function as "macro function name (variable 1, variable 2, ...)" Finally, let's close it with "end macro"

macro.j2


{% macro test1(var1, var2)%}
My name is {{ var1 }}
I live in {{ var2 }}
{% endmacro %}

{% macro test2(var1, var2)%}
My name is {{ var1 }}
I like {{ var2 }}
{% endmacro %}

Execution result

I was able to execute it as expected. Let's cut out a similar description into a macro file and create a smart template.

result.txt


My name is Taro
I live in Tokyo

My name is Hanako
I like tennis

Related article

[Ansible x NW automation] I want to create NW device settings with the template module

Recommended Posts

[Ansible] I want to call my own function from the template module (macro)
I want to add my own structure to the structure created by Python's C extension module!
I want to make fits from my head
I want to use the activation function Mish
Call your own python module from the ROS package
[Ansible] How to call variables when creating your own module
I want to calculate the allowable downtime from the operating rate
I want to get the name of the function / method being executed
I want to check the position of my face with OpenCV!
I want to pin Spyder to the taskbar
I want to output to the console coolly
I want to handle the rhyme part1
I want to send a signal only from the sub thread to the main thread
I want to handle the rhyme part3
I want to use jar from python
I tried how to improve the accuracy of my own Neural Network
I want to use the Django Debug Toolbar in my Ajax application
I want to express my feelings with the lyrics of Mr. Children
I want to display the progress bar
Python: I want to measure the processing time of a function neatly
I want to handle the rhyme part2
I want to handle the rhyme part5
I want to handle the rhyme part4
[Python] I want to know the variables in the function when an error occurs!
[LINE Messaging API] I want to send a message from the program to everyone's LINE
I want to see a list of WebDAV files in the Requests module
[Python] I tried to get the type name as a string from the type function
I want to get the file name, line number, and function name in Python 3.4
I want to connect to PostgreSQL from various languages
I want to have recursion come to my mind
I want to email from Gmail using Python.
[Python] I want to manage 7DaysToDie from Discord! 1/3
I want to perform SageMaker inference from PHP
I want to handle the rhyme part7 (BOW)
[I tried using Pythonista3] Importing my own module
I want to use ceres solver from python
[Python] I want to manage 7DaysToDie from Discord! 2/3
I want to make C ++ code from Python code!
I want to customize the appearance of zabbix
I want to display the progress in Python!
[Python] I made a system to introduce "recipes I really want" from the recipe site!
I want to replace the variables in the python template file and mass-produce it in another file.
I tried to learn the sin function with chainer
I want to detect images of cats from Instagram
How to run the Ansible module added in Ansible Tower
I tried to detect the iris from the camera image
I want to grep the execution result of strace
I want to scroll the Django shift table, but ...
I wanted to use the Python library from MATLAB
I want to inherit to the back with python dataclass
I want to fully understand the basics of Bokeh
I tried to approximate the sin function using chainer
[Python3] I want to generate harassment names from Japanese!
I want to write in Python! (3) Utilize the mock
I want to handle the rhyme part6 (organize once)
I want to automate ssh using the expect command!
I want to publish the product at the lowest cost
[Note] I can't call the installed module in jupyter
I read the Chainer reference (updated from time to time)
[Python] How to call a c function from python (ctypes)
I want to use the R dataset in python