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.yaml
all:
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.7
Jinja: 3.1.4
I have tried the following:-
- Changing directory structure
- Playing with inventory, changes structure, using yaml file
- Using include vars task
New contributor
The Node Guy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.