6

I am using Ansible 2.1.0.0

I try to use become_user with a variable in a task, but I receive the following message:

fatal: [host]: FAILED! => {"failed": true, "msg": "'ansible_user' is undefined"}

The task executing this is

- name: Config git user name
  git_config: name=user.name scope=global value={{ ansible_host }}
  become: Yes
  become_user: "{{ansible_user}}"

And the playbook has the following line to define the remote user:

- name: Foo
  hosts: foo
  vars:
    http_port: 80
  remote_user: admin

I've seen this response which seems to be the same problem, but this does not work for me.

I have seen also a set_fact solution but I would like to use the remote_user var if possible so no extra lines must be added if a playbook already has the remote_user var set.

Does anyone know how to do this or what I am doing wrong?

Community
  • 1
  • 1
Hugo
  • 109
  • 1
  • 1
  • 8
  • 1
    Where does your ansible_user come from? I can see the remote_user but not ansible_user. try `become_user: "{{remote_user}}"` – michael_bitard Jun 23 '16 at 13:33

3 Answers3

2

I think I found it:

become_user: "{{ansible_ssh_user}}"

In fact the remote_user: admin is another way of defining the variable ansible_ssh_user, I dont know why remote_user is not accessible as a variable, but what I know is that when you set remote_user, it changes the variable ansible_ssh_user

Not sure if it's a clean solution though, but it works

michael_bitard
  • 3,613
  • 1
  • 24
  • 35
  • 1
    No, still failing although ansible_ssh_user is a valid var in my tasks. If the task uses this variable like that: `- file: dest=/var/www mode=0755 owner={{ansible_ssh_user}} state=directory` Works perfectly. However, if I use this var as become_user: "{{ansible_ssh_user}}" it fails with that message. In fact I already use ansible_ssh_user var for other tasks. – Hugo Jun 23 '16 at 14:55
  • After re-reading that, I think that you could use ansible_ssh_user everywhere **except** for become_user, because that's where you set "admin" – michael_bitard Jun 23 '16 at 15:23
2

What about that:

- name: Foo
  hosts: foo
  vars:
    http_port: 80
    my_user: admin
  remote_user: "{{my_user}}"

then:

- name: Config git user name
  git_config: name=user.name scope=global value={{ ansible_host }}
  become: Yes
  become_user: "{{my_user}}"
michael_bitard
  • 3,613
  • 1
  • 24
  • 35
  • This works great! As a side note I find strange to have to use an extra var / extra line to set the remote_user var, but for my current needs it fits perfectly. Thank you very much for your help. – Hugo Jun 23 '16 at 15:13
1

I had a similar problem thrying to use {{ ansible_ssh_user }}

fatal: [xxx]: FAILED! => {"msg": "The field 'become_user' has an
invalid value, which includes an undefined variable. The error was:
'ansible_user' is undefined"}

I fixed this error using this approach:

- name: Backups - Start backups service
  shell:
    cmd: systemctl --user enable backups.service && systemctl --user restart backups.service
    executable: /bin/bash
  become: true
  become_method: sudo
  become_user: "{{ lookup('env','USER') }}"

I hope this helps.

asterio gonzalez
  • 1,056
  • 12
  • 12