Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Ansible passing variables to j2 templates

$
0
0

With ansible I am using j2 templates to replace files, and pass through environment vars unique to each environment and some shared. Neither the variables for global or local work. I have the following set-up and structure.

The directory structure

/project/project/playbooks/project/playbooks/do-something.yaml/project/playbooks/templates/project/playbooks/templates/myfile.json.j2/project/vars/project/vars/config_vars.yaml/project/inventory.ini

Inventory file

inventory.ini[myhosts]host1 ansible_host=192.168.1.10

Config Vars file

vars/config_vars.yamlall:  vars:    my_value_one: "value1"  children:    myhosts:      hosts:        host1:          my_value_two: "value2"

Playbook file

playbooks/do-something.yaml---- name: Configure Nodes  hosts: myhosts  become: yes  vars_files:    - ../vars/config_vars.yml tasks:    - name: Replace file with j2 template      template:        src: templates/myfile.json.j2        dest: /home/{{ ansible_user }}/my-folder/myfile.json

j2 template file

templates/myfile.json.j2{"value_one": "{{ my_value_one }}","value_two": "{{ my_value_two }}",}

Ansible Version: 2.16.7Jinja: 3.1.4

I have tried the following:-

  • Changing directory structure
  • Playing with inventory, changes structure, using yaml file
  • Using include vars task
  • I am aware I can do host1 ansible_host=192.168.1.10 my_value_one:"value" in the inventory file. However this for some reason outputs random numbers.

Viewing all articles
Browse latest Browse all 12111

Trending Articles