# Ansible trying to use root user to ssh instead of host\_var user?

**URL:** <https://forum.ansible.com/t/ansible-trying-to-use-root-user-to-ssh-instead-of-host-var-user/3354>\
**Category:** Get Help\
**Tags:** playbook, ubuntu, ansible-core\
**Created:** [January 17, 2024, 6:19pm UTC](https://forum.ansible.com/t/ansible-trying-to-use-root-user-to-ssh-instead-of-host-var-user/3354 "2024-01-17T18:19:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon1835205](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@anon1835205](https://forum.ansible.com/u/anon1835205)\
**Post date:** [January 17, 2024, 6:19pm UTC](https://forum.ansible.com/t/ansible-trying-to-use-root-user-to-ssh-instead-of-host-var-user/3354/1 "2024-01-17T18:19:11Z")

</div>

Hello,  
Im trying to learn/setup ansible for my homelab.  
Ansible uses the ansible\_user var fine for the initial ssh to gather facts, but when starting the update-ubuntu.yml, it tries to use root no matter what I try.  
FYI, root is locked so its impossible to ssh to it. My intended behaviour is for it to login as my normal user and then sudo to root privileges. Not login as root from the get go for some reason.  
Does someone have any pointers for me on this?

Thank you in advance 🙂

-Using Ansible 2.12.10  
-I would like to add logs but they show usernames and ips and I would rather not share these? I assume there is no way to have them be anonymized.

Log:

```auto
sudo ansible-playbook main-playbook.yml

PLAY [k-server] ********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
ok: [my-server]

PLAY [Update ubuntu] ***************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
fatal: [my-server]: UNREACHABLE! => changed=false
  msg: 'Failed to connect to the host via ssh: root@my-server: Permission denied (publickey,password).'
  unreachable: true

PLAY RECAP *************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
my-server : ok=1 changed=0 unreachable=1 failed=0 skipped=0 rescued=0 ignored=0

```

main\_playbook.yml

```auto
---
- hosts: my-server
- import_playbook: playbooks/update-ubuntu.yml

```

playbooks/update-ubuntu.yml

```auto
---
- hosts: ubuntu
  name: Update ubuntu
  tasks:
    - name: Update all packages
      #become: true
      apt:
        update_cache: yes
        upgrade: 'yes'

```

hosts

```auto
[servers]
my-server
nas

[servers:vars]
ansible_ssh_port=2222

[ubuntu]
my-server

```

host\_vars/my\_server

```auto
ansible_host: <<my_server_ip>>

#ssh credentials
ansible_user: <<my_username>>
ansible_password: <<EXPUNGED>>

```

---

<div class="post-metadata">

**Author:** ![anon1835205](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@anon1835205](https://forum.ansible.com/u/anon1835205)\
**Post date:** [January 17, 2024, 6:29pm UTC](https://forum.ansible.com/t/ansible-trying-to-use-root-user-to-ssh-instead-of-host-var-user/3354/2 "2024-01-17T18:29:29Z")

</div>

I just noticed it also tries to login at “my-server” instead of the ip given for my\_server with ansible\_host in inventory/my\_server. So it really seems to not use the host vars for some reason. I tried changing “hosts: ubuntu” to “hosts: my\_server” in the update-ubuntu.yml but sadly that didnt change anything.

---

<div class="post-metadata">

**Author:** ![anon1835205](https://avatars.discourse-cdn.com/v4/letter/a/7feea3/32.png) [@anon1835205](https://forum.ansible.com/u/anon1835205)\
**Post date:** [January 17, 2024, 6:36pm UTC](https://forum.ansible.com/t/ansible-trying-to-use-root-user-to-ssh-instead-of-host-var-user/3354/3 "2024-01-17T18:36:06Z")

</div>

After moving /host\_vars/my\_server to /inventory/host\_vars/my\_server it now works. I dont quite understand why though, because the ansible best practice page states to place the host\_vars at the top-level, not sindie /inventory.

Could someone explain this to me?

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [January 18, 2024, 2:37pm UTC](https://forum.ansible.com/t/ansible-trying-to-use-root-user-to-ssh-instead-of-host-var-user/3354/4 "2024-01-18T14:37:05Z")

</div>

One issue is the file in `host_vars` needs to have the same name as the host `my-server`, instead of `my_server`.

`host_vars`/`group_vars` are always looked up relative to the current playbook and inventory. So if the playbooks are at the top level, it would work.

With the given layout (after fixing the filename) the variables in host\_vars/my-server would be available in main\_playbook.yml, but not available to the playbooks in the `playbooks/` directory.
