Jinja2 Template to use as SRC Path

17 views
Skip to first unread message

wpgpo...@gmail.com

unread,
Jul 26, 2020, 3:34:07 PM7/26/20
to Ansible Project
Hello Group,

Requesting for advise or solution.
The idea/objective is to have the specific filename to copy to the specific node as the SRC path.

Filename
node1-appprops.config
node2-appprops.config

Server Host
app_server_node1
app_server_node2

SRC Patch
/ConfigurationFolder/WrapperConfig/temp

Someone says to use Jinja2 Template and I have created the file, copy_appprops_node.j2. And, the j2 contains like...

J2 Template
{% if ansible_hostname is match(" app*node1") %}
/ConfigurationFolder/WrapperConfig/temp/{{nodeID1}}- appprops.config
{% endif %}
{% if ansible_hostname is match(" app*node2") %}
/ConfigurationFolder/WrapperConfig/temp/{{nodeID2}}- appprops.config
{% endif %}

Playbook YML File (running by different user)
---
- name: Test Template
  hosts: app_host
  vars:
    nodeID1: node1
    nodeID2: node2

  tasks:
    - name: Copying the properties file
      template:
        src: copy_appprops_node.j2
        dest: "/appserver/instance/conf/app_properties.conf"
        owner: app1
        group: app1
        mode: 0644
...

Any help and assistance would be highly appreciated.
Or, any idea that will work from the objective.

Thank you in advance.

Cheers....

J Hawkesworth

unread,
Jul 27, 2020, 6:16:52 PM7/27/20
to Ansible Project
I would try and organize variables so they are associated with the host in inventory, rather than setting a lot of playbook vars

# ansible inventory (notice both hosts in a group called 'appservers' )

[appservers]

app_server_node1 appprops_file
=node1-appprops.config
 app_server_node2 appprops_file
=node2-appprops.config


Playbook YML File (running by different user)
---
- name: Test Template

  hosts
: appservers

  tasks
:
   
- name: Copying the properties file
      copy
:
        src
: '/ConfigurationFolder/WrapperConfig/temp/{{appprops_file}}'

        dest
: '/appserver/instance/conf/app_properties.conf'
        owner
: app1
       
group: app1
        mode
: 0644

Ansible will put /ConfigurationFolder/WrapperConfig/temp/node1-appprops.config on node1 and /ConfigurationFolder/WrapperConfig/temp/node2-appprops.config onto node2.  No need to use template as you can use the appprops_file variable which is different for each host.

Hope this helps,

Jon
# ansible inventory (notice both hosts in a group called 'appservers' )
[appservers]
app_server_node1 appprops_file=node1-appprops.config
app_server_node2 appprops_file=node2-appprops.config

wpgpo...@gmail.com

unread,
Jul 29, 2020, 10:44:52 PM7/29/20
to Ansible Project
Hello Jon,

Thank you so much for the idea.

Cheers....
Reply all
Reply to author
Forward
0 new messages