Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix becoming root using (delegate_to)?

Tags:

ansible

I am trying to become root user for one of my tasks. However, when I see the delegate_to field for the task and enter the correct root password, it keeps giving me the fatal error Incorrect su password.

I have already tried messing around with the delegate_facts: true. However, I had no luck with getting it to work.

Code:

- hosts: 10.x.x.1
- tasks:


- name: Set root password for host
  set_fact:
     ansible_become_password: "{{ tempPassword }}"

- name: whoami as root (su)
  command: whoami
  register: output_root_su
  delegate_to: "{{ delegate_host }}"
  become_user: root
  become_method: su
  become: yes

The desired result should give an output of "root". Instead this is the output I get: fatal: [10.x.x.2]: FAILED! => {"msg": "Incorrect su password"}

like image 570
robert.kane Avatar asked Dec 09 '25 09:12

robert.kane


1 Answers

(Tested with ansible 2.7.9)

set_fact should not work. If ansible_become_password is declared by set_fact

  set_fact:
     ansible_become_password: "{{ tempPassword }}"

the play should fail with

FAILED! => {"msg": "Timeout (12s) waiting for privilege escalation prompt: "}

Declare ansible_become_password either in the vars section of the play

- hosts: 10.x.x.1
  vars:
    ansible_become_password: "{{ tempPassword }}"
  tasks:

, or in the task

- hosts: 10.x.x.1
  tasks:
    - command: whoami
      register: result
      delegate_to: "{{ delegate_host }}"
      become: yes
      become_user: root
      become_method: su
      vars:
        ansible_become_password: "{{ tempPassword }}"
    - debug:
        var: result.stdout

Quoting from Connecting to hosts: behavioral inventory parameters

ansible_become_password Equivalent to ansible_sudo_password or ansible_su_password, allows you to set the privilege escalation password (never store this variable in plain text; always use a vault. See Variables and Vaults)

like image 108
Vladimir Botka Avatar answered Dec 11 '25 01:12

Vladimir Botka



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!