robert.kane
robert.kane

Reputation: 129

How to fix becoming root using (delegate_to)?

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"}

Upvotes: 3

Views: 2419

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 68044

(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)

Upvotes: 2

Related Questions