当在剧本中设置密码变量时,Ansible“成为”插件不起作用

Ansible “become” plugin doesn't work when password variable is set in playbook

我有一个没有 sudo 的远程用户和禁止 root 用户使用 ssh 的服务器。 所以我尝试使用下一种方法来提升权限:

- block:
  - name: Get hardware password
    shell: |
      slcli --format json hardware detail --passwords {{ hostname }}
    register: json_answer
    delegate_to: localhost
  - name: set hardwareInfo variable
    set_fact: 
      hardwareInfo: "{{ json_answer.stdout|from_json }}"
  - name: set password variable
    set_fact: 
      ansible_become_pass: "{{ hardwareInfo | to_json | from_json | json_query(password_query) }}"
    vars: 
      password_query: "users[?username==`root`].password"
  no_log: true

- name: Install repository deb
  shell: |
    dpkg -i {{ deb_repo_url }}
  become: yes
  become_method: su
  become_user: root

但是我得到错误:

{ "msg": "Incorrect su password", "_ansible_no_log": false }

我检查了 ansible_become_pass 变量,它有正确的密码。

发现我的变量格式错误(数组而不是字符串)。

{
    "changed": false,
    "ansible_facts": {
        "ansible_become_pass": [
            "my_password"
        ]
    },
    "_ansible_no_log": false
}

已将 set_fact 更改为此,现在可以使用了

- name: set password variable
  set_fact: 
    ansible_become_pass: "{{ hardwareInfo | to_json | from_json | json_query(password_query) | join('') }}"