如何使用(delegate_to)修复成为root用户的问题?

时间:2019-07-29 13:48:27

标签: ansible

我正试图成为我的任务之一的root用户。但是,当我看到该任务的delegate_to字段并输入正确的root密码时,它总是给我致命错误Incorrect su password

我已经尝试过弄混delegate_facts: true。但是,我没有运气。

代码:

- 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

所需的结果应为“ root”的输出。相反,这是我得到的输出: fatal: [10.x.x.2]: FAILED! => {"msg": "Incorrect su password"}

1 个答案:

答案 0 :(得分:1)

(已通过ansible 2.7.9测试)

set_fact 应该不起作用。如果 set_fact

声明了 ansible_become_password
  set_fact:
     ansible_become_password: "{{ tempPassword }}"

戏剧应该失败

FAILED! => {"msg": "Timeout (12s) waiting for privilege escalation prompt: "}
在播放的 vars 部分中

声明 ansible_become_password

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

,或者在任务中

- 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

引用Connecting to hosts: behavioral inventory parameters

  

ansible_become_password   与ansible_sudo_password或ansible_su_password等效,允许您设置特权升级密码(永远不要以纯文本形式存储此变量;始终使用保管库。请参阅变量和保险库)