# get value from command line in playbook

**URL:** <https://forum.ansible.com/t/get-value-from-command-line-in-playbook/24373>\
**Category:** Ansible Project\
**Created:** [May 2, 2017, 9:52am UTC](https://forum.ansible.com/t/get-value-from-command-line-in-playbook/24373 "2017-05-02T09:52:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anand\_Dasari](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@Anand\_Dasari](https://forum.ansible.com/u/Anand_Dasari)\
**Post date:** [May 2, 2017, 9:52am UTC](https://forum.ansible.com/t/get-value-from-command-line-in-playbook/24373/1 "2017-05-02T09:52:32Z")

</div>

Hi

is there anyway i can get value from command line arguments

for eg., my command line is

ansible-playbook -i inv/sit1 playbooks/sit1/test.yml

in test.yml i want to capture the value from inv/sit1 as sit1,

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [May 2, 2017, 7:12pm UTC](https://forum.ansible.com/t/get-value-from-command-line-in-playbook/24373/2 "2017-05-02T19:12:16Z")

</div>

There probably is a way to hack it

(e.g. inventory\_file minus inventory\_dir , see

&nbsp;&nbsp;&nbsp;&nbsp;[http://stackoverflow.com/questions/18839509/where-can-i-get-a-list-of-ansible-pre-defined-variables](http://stackoverflow.com/questions/18839509/where-can-i-get-a-list-of-ansible-pre-defined-variables)

)

but I've done this myself by just setting a common variable in  
inv/\*/group\_vars/all  
and referencing that.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [May 2, 2017, 8:57pm UTC](https://forum.ansible.com/t/get-value-from-command-line-in-playbook/24373/3 "2017-05-02T20:57:51Z")

</div>

Add a variable in your group vars for the environment you are targeting, e.g. Have a structure like

group\_vars/  
&nbsp;&nbsp;&nbsp;&nbsp;prd.yml - variables for your production environment  
&nbsp;&nbsp;&nbsp;&nbsp;uat.yml - variables for your uat environment  
&nbsp;&nbsp;&nbsp;&nbsp;sit.yml - variables for your sit environment  
&nbsp;&nbsp;&nbsp;&nbsp;app.yml - application variables that cover your application valid across all environments  
inventory.ini - your static inventory file that groups the hosts together

Use your inventory to define the group the host sits under so in the example above it will be a member of the app and one environment group (sit). This way you can access any variables that are set in that environment that you have defined which in your case would contain the environment name.
