# How to prepare Ansible inventory, playbook and configuration file used by Ansible module using Ansible/Jinja2?

**URL:** <https://forum.ansible.com/t/how-to-prepare-ansible-inventory-playbook-and-configuration-file-used-by-ansible-module-using-ansible-jinja2/24661>\
**Category:** Ansible Project\
**Created:** [June 6, 2017, 2:31pm UTC](https://forum.ansible.com/t/how-to-prepare-ansible-inventory-playbook-and-configuration-file-used-by-ansible-module-using-ansible-jinja2/24661 "2017-06-06T14:31:49Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@Martin](https://forum.ansible.com/u/Martin)\
**Post date:** [June 6, 2017, 2:31pm UTC](https://forum.ansible.com/t/how-to-prepare-ansible-inventory-playbook-and-configuration-file-used-by-ansible-module-using-ansible-jinja2/24661/1 "2017-06-06T14:31:49Z")

</div>

Hi,

I have a plain-text file “routers” in versioning system which contains host-names separated by newlines:

$ cat routers  
r1  
r2  
r3  
$

I also have routers configuration templates in versioning system as a plain-text files. For example:

$ cat system:syslog  
system {  
syslog {  
host 10.10.10.153 {  
any notice;  
daemon info;  
}  
}  
}  
$

I can’t change the format of those two types of files because they are used by other systems. Now what I need, is to create Ansible inventory file from this “routers” file, configuration file for Ansible junos\_config([https://docs.ansible.com/ansible/junos\_config\_module.html](https://docs.ansible.com/ansible/junos_config_module.html)) module from routers configuration template and finally Ansible playbook in YAML. At the moment I use a bash script for this. Simplified, it takes four arguments:

1. “routers” file
2. configuration template file(for example “system:syslog” described above)
3. ticket number in ticketing system(integer)
4. comment string(this is used in router to describe the change)

Based on the example above, “routers” file is converted to inventory file with following content:

junipers ansible\_connection=local  
[junipers]  
r1  
r2  
r3

“system:syslog” file is converted to a file name “\<ticket\_nr\>.cfg” with following content:

groups {  
replace: system:syslog {  
system {  
syslog {  
host 10.10.10.153 {  
any notice;  
daemon info;  
}  
}  
}  
}  
}

Basically, bash script does something like this:

cat \<\< EOF  
groups {  
replace: ${templatefile##\*/} {  
EOF  
sed ‘s/^/ /’ “$templatefile”  
cat \<\< EOF  
}  
}  
EOF

And finally playbook file is generated:

- hosts: junipers  
remote\_user: admin  
tasks:
- name: Change routers conf.  
ignore\_errors: yes  
junos\_config:  
comment: “”  
timeout: 60  
port: 22  
src: \<ticket\_nr\>.cfg  
src\_format: text  
replace: yes

“” and “\<ticket\_nr\>” are arguments given to the bash script. In addition, this bash script checks out the latest revision of mentioned “routers” file and configuration template.

Now is there a better way to do all this without bash? While this works, I’m feeling that I’m doing it wrong and I’m relying heavily on bash. Is it possible to do all this in Ansible/Jinja2?

thanks,  
Martin
