ansibleansible-2.x

`remote_user` is ignored in playbooks and roles


I have defined the following in my ansible.cfg

# default user to use for playbooks if user is not specified
# (/usr/bin/ansible will use current user as default)
remote_user = ansible

However I have a playbook bootstrap.yaml where I connect with root rather than ansible

---
- hosts: "{{ target }}"
  become: no
  gather_facts: false
  remote_user: root

  vars:
    os_family: "{{ osfamily }}}"

  roles:
  - role: papanito.bootstrap

However it seems that remote_user: root is ignored as I always get a connection error, because it uses the user ansible instead of root for the ssh connection

fatal: [node001]: UNREACHABLE! => {"changed": false,
"msg": "Failed to connect to the host via ssh:
 ansible@node001: Permission denied (publickey,password).", 
"unreachable": true}

The only workaround for this I could find is calling the playbook with -e ansible_user=root. But this is not convenient as I want to call multiple playbooks with the site.yaml, where the first playbook has to run with ansible_user root, whereas the others have to run with ansible

- import_playbook: playbooks/bootstrap.yml
- import_playbook: playbooks/networking.yml
- import_playbook: playbooks/monitoring.yml

Any suggestions what I am missing or how to fix it?


Solution

  • Q: "remote_user: root is ignored"

    A: The playbook works as expected

    - hosts: test_01
      gather_facts: false
      become: no
      remote_user: root
      tasks:
        - command: whoami
          register: result
        - debug:
            var: result.stdout
    

    gives

    "result.stdout": "root"
    

    But, the variable can be overridden in the inventory. For example with the inventory

    $ cat hosts
    all:
      hosts:
        test_01:
      vars:
        ansible_connection: ssh
        ansible_user: admin
    

    the result is

    "result.stdout": "admin"
    

    Double-check the inventory with the command

    $ ansible-inventory --list
    


    Notes