# Define global variables in ansible group\_vars/all file

**URL:** <https://forum.ansible.com/t/define-global-variables-in-ansible-group-vars-all-file/31780>\
**Category:** Ansible Project\
**Created:** [December 6, 2019, 4:24pm UTC](https://forum.ansible.com/t/define-global-variables-in-ansible-group-vars-all-file/31780 "2019-12-06T16:24:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [December 6, 2019, 4:24pm UTC](https://forum.ansible.com/t/define-global-variables-in-ansible-group-vars-all-file/31780/1 "2019-12-06T16:24:10Z")

</div>

Hello,

I followed Ansible best practice to create my ansible automation directory structure like following:

group\_vars  
group\_vars/all  
host\_vars  
os.yml  
production  
roles  
roles/os-issue

Since I’m using ansible user as a remote user, and sudo method to escalate its privileges globally, I want to define this in group\_vars/all file. Here is what I define in group\_vars/all directory:

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [December 6, 2019, 4:30pm UTC](https://forum.ansible.com/t/define-global-variables-in-ansible-group-vars-all-file/31780/2 "2019-12-06T16:30:12Z")

</div>

It's not necessary to use the "vars:" directive in the files.  
See "Organizing host and group variables"  
[https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html#organizing-host-and-group-variables](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables)

# group\_vars/all  
remote\_user: ansible  
become: yes  
become\_method: sudo

Cheers,

&nbsp;&nbsp;-vlado

---

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [December 6, 2019, 4:44pm UTC](https://forum.ansible.com/t/define-global-variables-in-ansible-group-vars-all-file/31780/3 "2019-12-06T16:44:52Z")

</div>

Hello,

> It’s not necessary to use the “vars:” directive in the files.  
> See “Organizing host and group variables”  
> [https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html#organizing-host-and-group-variables](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables)
> 
> # group\_vars/all
> 
> remote\_user: ansible  
> become: yes  
> become\_method: sudo
> 
> Cheers,
> 
> -vlado

I changed per advice. Here is my changed file:

---

<div class="post-metadata">

**Author:** ![acozine1](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@acozine1](https://forum.ansible.com/u/acozine1)\
**Post date:** [December 6, 2019, 5:09pm UTC](https://forum.ansible.com/t/define-global-variables-in-ansible-group-vars-all-file/31780/4 "2019-12-06T17:09:38Z")

</div>

I don’t think the failure is related to using or not using “sudo”. The playbook failed because Ansible could not connect to the remote machine.

The error message:

fatal: [myserver]: UNREACHABLE! =\> {“changed”: false, “msg”: “ **Failed to connect to the host via ssh** : ============================================================\n|Permission denied (publickey,password,keyboard-  
interactive).”, “unreachable”: true}

tells you that Ansible cannot connect to ‘myserver’ over SSH. If you try to connect to the target machine using SSH and the user ‘ansible’ from the command line, does that work? Do you have to type in a password? If you’re using SSH keys, does the ‘ansible’ user have permission to access the correct key?

See [https://docs.ansible.com/ansible/latest/user\_guide/connection\_details.html#ssh-key-setup](https://docs.ansible.com/ansible/latest/user_guide/connection_details.html#ssh-key-setup) for information on setting up SSH keys.

Hope this helps point you in the right direction.

Alicia

---

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [December 6, 2019, 5:15pm UTC](https://forum.ansible.com/t/define-global-variables-in-ansible-group-vars-all-file/31780/5 "2019-12-06T17:15:42Z")

</div>

Hello Alicia,

I just ran ad-hoc command with -u ansible parameter like below:

$ ansible all -i production -u ansible -l mygroup -a “uptime”  
myserver | CHANGED | rc=0 \>\>  
12:13:22 up 11 days, 2:26, 2 users, load average: 0.00, 0.02, 0.05

ansible user is defined on myserver, and it is in sudoers file in wheel group without password required.

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [December 6, 2019, 5:16pm UTC](https://forum.ansible.com/t/define-global-variables-in-ansible-group-vars-all-file/31780/6 "2019-12-06T17:16:45Z")

</div>

`remote_user` is not an inventory var that ansible looks for to inform ansible how to connect. You would instead want to use `ansible_user`.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [December 6, 2019, 5:19pm UTC](https://forum.ansible.com/t/define-global-variables-in-ansible-group-vars-all-file/31780/7 "2019-12-06T17:19:23Z")

</div>

And if you add the ‘-b’ option to that?

---

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [December 6, 2019, 5:28pm UTC](https://forum.ansible.com/t/define-global-variables-in-ansible-group-vars-all-file/31780/8 "2019-12-06T17:28:02Z")

</div>

Hello Dick,

Yes, I added -b option. That worked fine.

$ ansible all -i production -u ansible -l mygroup -a “uptime” -b  
myserver | CHANGED | rc=0 \>\>

12:26:39 up 11 days, 2:40, 2 users, load average: 0.00, 0.01, 0.05

---

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [December 6, 2019, 5:58pm UTC](https://forum.ansible.com/t/define-global-variables-in-ansible-group-vars-all-file/31780/9 "2019-12-06T17:58:06Z")

</div>

Hello All,

I just figured out myself and I want to post it for sharing with other who would have similar issue like myself.

The issue is what variable names I use in group\_vars/all file.

remote\_user, become and become\_method, are not inventory-like recognized variables, they are recognized in playbook, but not in inventory, although those variables are not put in inventory file, I believe, they are inventory variables.

The correct names when putting in group\_vars/all file are:

---

<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:** [December 20, 2019, 5:01pm UTC](https://forum.ansible.com/t/define-global-variables-in-ansible-group-vars-all-file/31780/10 "2019-12-20T17:01:22Z")

</div>

ansible\_user is a variable, can be defined anywhere variables are defined  
remote\_user is a keyword, as such it can only be set on playbook  
objects (play/block/role/task)
