# Passing variables to a template / jinja2 ??

**URL:** <https://forum.ansible.com/t/passing-variables-to-a-template-jinja2/28907>\
**Category:** Ansible Project\
**Created:** [October 31, 2018, 11:03am UTC](https://forum.ansible.com/t/passing-variables-to-a-template-jinja2/28907 "2018-10-31T11:03:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nadim\_mansour](https://avatars.discourse-cdn.com/v4/letter/n/a698b9/32.png) [@nadim\_mansour](https://forum.ansible.com/u/nadim_mansour)\
**Post date:** [October 31, 2018, 11:03am UTC](https://forum.ansible.com/t/passing-variables-to-a-template-jinja2/28907/1 "2018-10-31T11:03:37Z")

</div>

I have an http.conf where some values will be variables passed by the user

where should I put the variables and how the file should look can someone advice ?

---

<div class="post-metadata">

**Author:** ![Foss\_Geek](https://avatars.discourse-cdn.com/v4/letter/f/df705f/32.png) [@Foss\_Geek](https://forum.ansible.com/u/Foss_Geek)\
**Post date:** [October 31, 2018, 12:43pm UTC](https://forum.ansible.com/t/passing-variables-to-a-template-jinja2/28907/2 "2018-10-31T12:43:05Z")

</div>

1). Send all your user input to ansible playbook command using —extra-vars. You can also pass as json and yaml file.

Example: # ansible-playbook site.yml —extra-vars “http\_server\_root=/var/www/html http\_listen=8080 http\_user=apache http\_group=apache”

[https://docs.ansible.com/ansible/2.7/user\_guide/playbooks\_variables.html#passing-variables-on-the-command-line](https://docs.ansible.com/ansible/2.7/user_guide/playbooks_variables.html#passing-variables-on-the-command-line)

2). Create a template for your http.conf file with variable which you are passing via —extra-vars.

http.conf.j2

ServerRoot {{ http\_server\_root }}

Listen {{ http\_listen }}

User {{ http\_user }}

Group {{ http\_group }}

3). Use template module to copy the config to target.

- name: Install main configuration file

template:

src: httpd.conf.j2

dest: /etc/httpd/conf/httpd.conf

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 1, 2018, 9:30pm UTC](https://forum.ansible.com/t/passing-variables-to-a-template-jinja2/28907/3 "2018-11-01T21:30:51Z")

</div>

you can also use vars\_prompt to create the variables  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_prompts.html](https://docs.ansible.com/ansible/latest/user_guide/playbooks_prompts.html)
