# How to use external variable inside an ansible inventory file.

**URL:** <https://forum.ansible.com/t/how-to-use-external-variable-inside-an-ansible-inventory-file/20017>\
**Category:** Ansible Project\
**Created:** [December 15, 2015, 7:30am UTC](https://forum.ansible.com/t/how-to-use-external-variable-inside-an-ansible-inventory-file/20017 "2015-12-15T07:30:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anoop.kolari](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anoop.kolari](https://forum.ansible.com/u/anoop.kolari)\
**Post date:** [December 15, 2015, 7:30am UTC](https://forum.ansible.com/t/how-to-use-external-variable-inside-an-ansible-inventory-file/20017/1 "2015-12-15T07:30:24Z")

</div>

Hi,

I have an inventory file ‘inventory/localhost’ and contains;

[localhost]  
“{{MYSITE\_NAME}}-local.com” ansible\_ssh\_port=2222

Lets say ‘MYSITE\_NAME’ is a variable defined in some file. I need to access this inside my inventory file. Please suggest some solution.

---

<div class="post-metadata">

**Author:** ![anoop.kolari](https://avatars.discourse-cdn.com/v4/letter/a/59ef9b/32.png) [@anoop.kolari](https://forum.ansible.com/u/anoop.kolari)\
**Post date:** [December 16, 2015, 7:10am UTC](https://forum.ansible.com/t/how-to-use-external-variable-inside-an-ansible-inventory-file/20017/2 "2015-12-16T07:10:16Z")

</div>

Any suggestion guys??

---

<div class="post-metadata">

**Author:** ![pixel\_fairy](https://avatars.discourse-cdn.com/v4/letter/p/6f9a4e/32.png) [@pixel\_fairy](https://forum.ansible.com/u/pixel_fairy)\
**Post date:** [December 27, 2015, 12:43am UTC](https://forum.ansible.com/t/how-to-use-external-variable-inside-an-ansible-inventory-file/20017/3 "2015-12-27T00:43:57Z")

</div>

theres probably an easier way, but since your setting up for a dynamic inventory, you might as well use it.

[http://docs.ansible.com/ansible/developing\_inventory.html](http://docs.ansible.com/ansible/developing_inventory.html)

heres an example in python for lxc containers.

`  
#!/usr/bin/env python

import yaml  
import json

with open(‘containers.yml’) as fp:  
c = yaml.load(fp)  
containers = c[‘containers’]

inventory = {  
‘containers’: { ‘hosts’: },  
‘\_meta’: { ‘hostvars’: {} }  
}

for k, v in containers.items():  
inventory[‘containers’][‘hosts’].append(k)  
if ‘groups’ in v:  
for group in v[‘groups’]:  
if group not in inventory:  
inventory[group] = { ‘hosts’: }  
inventory[group][‘hosts’].append(k)  
inventory[‘\_meta’][‘hostvars’][k] = {}  
inventory[‘\_meta’][‘hostvars’][k][‘ansible\_ssh\_host’] = v[‘address’]

print(json.dumps(inventory,indent=2))

`

and the containers,

`  
containers:  
vpn:  
address: 192.168.114.4  
forwards:

- { transport: udp, host: 1194, container: 1194 }  
reverseproxy:  
address: 192.168.114.10  
forwards:
- { transport: tcp, host: 80, container: 80 }
- { transport: tcp, host: 443, container: 443 }  
web1:  
hostname: web1.example.lan  
address: 192.168.114.11  
cnames:
- moreweb.example.lan
- another.example.lan  
groups:
- web  
web2:  
hostname: web2.example.lan  
address: 192.168.114.12  
groups:
- web

`
