linux - 在没有 sudo 权限的情况下以非 root 用户身份连接时使用 Ansible playbook 脚本更改 linux 密码

标签 linux bash passwords ansible passwd

我正在尝试通过 Ansible playbook 为 non-root Linux 用户更改密码。 为此,我尝试遵循 this link

按照说明,我可以通过在终端中键入以下代码成功更改非 root 用户的密码。

$ echo -e "your_current_pass\nlinuxpassword\nlinuxpassword" | passwd
Changing password for testuser.
(current) UNIX password: Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully

之后,我尝试使用如下所示的 Ansible 剧本自动化代码,

---
- hosts: all
  gather_facts: no

  tasks:
    - name: "Check if user exists"
      register: user1_exists
      raw: getent passwd {{ ansible_user }}
      ignore_errors: true

    - name: "Change {{ ansible_user }} password"
      raw: echo -e "my_current_pass\nmy_new_pass\nmy_new_pass" | passwd
      when: user1_exists|success

我正在使用 raw module Ansible 在这里,因为我的大多数机器都没有安装 Python。我没有 superuser (sudo) 权限,无法在 playbook 中使用 become: True

此处还使用基于密码的身份验证 在目标机器上运行 Ansible 剧本。不是基于 ssh 的身份验证。

但是当我执行 playbook 时出现此错误,

TASK [change user1 password] ***************************************************
fatal: [192.168.0.57]: FAILED! => {"changed": true, "failed": true, "rc": 10, 
"stderr": "Shared connection to 192.168.0.57 closed.\r\n", "stdout": "Changing 
password for testuser.\r\n(current) UNIX password: passwd: Authentication 
token manipulation error\r\npasswd: password unchanged\r\n", "stdout_lines": 
["Changing password for testuser.", "(current) UNIX password: passwd: 
Authentication token manipulation error", "passwd: password unchanged"]}

谁能告诉我我在这里犯的错误?

最佳答案

使用 built-in user module而不是 shell 命令。这需要在您的剧本中使用 become: True。请注意,用户模块的password 参数需要一个加密值。 password_hash jinja 过滤器将帮助您。

  - name: change user's password
    user:
      name: foo
      password: "{{ 'passwordsaresecret' | password_hash('sha512') }}"

关于linux - 在没有 sudo 权限的情况下以非 root 用户身份连接时使用 Ansible playbook 脚本更改 linux 密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45988965/

相关文章:

linux - 列出编译对象使用的指令集 - 适用于 ARM 和 X86

linux - 如何找到没有软链接(soft link)的目录?

regex - 在 bash 中使用正则表达式查找文件中的字符串

security - 在 Spring Security 3.1 中,StandardPasswordEncoder 最适合用于加盐密码的用途是什么?

security - 你能以多快的速度对 PBKDF2 进行暴力破解?

linux - 在 Linode 服务器上升级 openssl 但如何确定 'ENGINESDIR'

linux - 在 Openshift 中对进程进行沙箱处理

c - 使用 URL 发送登录详细信息

GNU/Linux 上的 Java 数据库连接

bash - bash中的关联数组不在循环内存储值