Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible gets localhost USER wrong?

Tags:

ansible

I'm trying to setup a register i.e check if file exists before executing a task in ansible

- name: check CRAN R script exists
  stat:
    path: "{{ playbook_dir }}/scripts/R/cran.R"
  delegate_to: localhost
  register: cran

  tags: r_extras

This task is part of the playbook as runs as sudo

- name: installing common apt dependencies, pip python packages as well as java
  hosts: base
  remote_user: "{{ sudo_guy }}"
  roles:
            # need to have non previlidged installer guy
            - make_sw_guy
            # apt-get installation
            - common

  become: true

when I run playbook like this

ansible-playbook -vvvv -i my_hosts all.yml --tags r_extras

I get this error

fatal: [bio_training -> localhost]: FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_name": "stat"
    }, 
    "module_stderr": "sudo: a password is required\n", 
    "module_stdout": "", 
    "msg": "MODULE FAILURE"
}

Because Ansible thinks I'm root .. which seems a bit silly to me, but I might be missing the point?

How to check that local file exists? AND/OR how to get $USER variable of the localhost


I can "fix" this problem by adding become_user: $USER but I don't know how to make this generic..

like image 769
serine Avatar asked Mar 15 '26 07:03

serine


1 Answers

become directive set for the role is valid for all tasks, including those with delegate_to.

Effectively, you attempt to run the task locally with sudo command in front and have a requirement for providing the password set in /etc/sudoers.

There's nothing silly in what Ansible does here and it does not "think" you are root, actually if it "thought" you were root, it wouldn't ask for the password.

You can add become: false to the task.

like image 65
techraf Avatar answered Mar 18 '26 00:03

techraf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!