# how to set an environment variable in ansible ad-hoc command?

**URL:** <https://forum.ansible.com/t/how-to-set-an-environment-variable-in-ansible-ad-hoc-command/19052>\
**Category:** Ansible Project\
**Created:** [August 19, 2015, 9:10am UTC](https://forum.ansible.com/t/how-to-set-an-environment-variable-in-ansible-ad-hoc-command/19052 "2015-08-19T09:10:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yves\_Serrano](https://avatars.discourse-cdn.com/v4/letter/y/da6949/32.png) [@Yves\_Serrano](https://forum.ansible.com/u/Yves_Serrano)\
**Post date:** [August 19, 2015, 9:10am UTC](https://forum.ansible.com/t/how-to-set-an-environment-variable-in-ansible-ad-hoc-command/19052/1 "2015-08-19T09:10:31Z")

</div>

Hi there

Is there in ansible a way so set an linux environment variable for a ad-hoc mode and store it permanently in some place?  
I tried host\_vars, but that doesn’t worked.

I want to run this command:  
ansible all -m shell -a "DEBIAN\_FRONTEND=noninteractive apt-get upgrade”

without the need to explicit define DEBIAN\_FRONTEND and make DEBIAN\_FRONTEND=noninteractive the default for the shell module.

I don’t want to use a playbook, because normally I run ansible all -m shell -a "apt-get upgrade -s” to see what would be changed, and than  
afterwards to run it without “-s”. I often forgot to set DEBIAN\_FRONTEND=noninteractive in the ad-hoc command call.

I don’t want that this the default for the normal environment, when I ssh outside of ansible.

A possible workaround would be if I could recognize an ansible ssh connection from the inside of the host and than set the environment variable accordingly.  
Any idea who to do it?  
Or is there a proper ansible way of doing this?

thanks  
yves

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [August 20, 2015, 2:05am UTC](https://forum.ansible.com/t/how-to-set-an-environment-variable-in-ansible-ad-hoc-command/19052/2 "2015-08-20T02:05:04Z")

</div>

look at .ssh/rc file for setting up vars, but i would also recommend  
just using 'ansible -m apt -a "upgrade=yes" '

one of these days i'm just going to patch apt-get to automatically go  
non-interactive when stdin is not a tty ....
