ansible 将用户切换到 sudo 以外的用户

ansible switch user to other than sudo

在我的 Linux 机器上,系统中有三个用户级别。

- base_user
- higher_user
- root

我通过 SSH 远程连接到 base_user 级别的机器。 我的 ssh 密钥设置允许我自动以 base_user 身份登录。 然后我想更改为没有 sudo 特权的 higher_user 。 从 ansible 文档 (http://docs.ansible.com/ansible/latest/become.html) 变成 become_user 需要在 base_user 上执行 sudo 才能变成 higher_user。 我不想让 higher_user 或 base_user 拥有 sudo 权限。

是否有任何解决方法可以纯粹在 ansible 中执行以下操作?

1. login as base_user with ssh key setup
2. switch user to higher_user with password input

sudoers 文件中的这一行将允许 base_user 像 higher_user 一样使用 sudo 到 运行 命令。它不允许 base_user 到 运行 作为 root 或任何其他用户的命令。

base_user   ALL=(higher_user) ALL

从那里您可以在您的剧本中使用 becomebecome_user

- hosts: some_host
  become: True
  become_user: higher_user
  tasks:
...

当您 运行 ansible-playbook 时,您需要提供 base_user 的密码 --ask-become-pass (-K)。