【问题标题】:Ansible: Cannot configure sudo command even become_user is a sudo userAnsible:即使 become_user 是 sudo 用户也无法配置 sudo 命令
【发布时间】:2018-06-09 14:56:07
【问题描述】:

testuser 是 sudo 用户,

sudo cat /etc/sudoers.d/90-cloud-init-testuser
testuser ALL=(ALL) NOPASSWD:ALL

我可以手动登录 testuser 并在没有密码的情况下运行:

sudo -H apt-get update
sudo -H apt-get upgrade

但如果我按照 ansible 代码运行,虽然我看到 whoami 命令返回 testuser,但代码会因致命错误而停止(请参阅下面的代码和错误)。

我必须将 become_user 设置为 root 才能运行(请参阅我注释掉的行)?注意我可以手动登录 testuser 并运行 sudo 命令,我不能使用 become_user=testuser 来安装 apt 吗?注意我认为remote_user没有关系,因为whoami命令只依赖become_user。实际上我觉得remote_user没用,它只是让我登录。如果become_user未设置。然后 whoami 成为 root,如果 become_user 设置为 testuser,则 whoami 成为 testuser。

- hosts: all
  remote_user: ubuntu
  become: yes
  become_user: testuser
  gather_facts: yes
  become_method: sudo
  tasks:
  - name: test which user I am
    shell: whomami
    register: hello
  - debug: msg="{{ hello.stdout }}"
  - name: Update and upgrade apt.
#    become_user: root
#    become: yes
    apt: update_cache=yes upgrade=dist cache_valid_time=3600

TASK [Update and upgrade apt.]     
********************************
fatal: [XX.XX.XX.XX]: FAILED! => {"changed": false, "msg": 

"'/usr/bin/apt-get dist-upgrade' failed: E: Could not open lock file     
/var/lib/dpkg/lock - open (13: Permission denied)\nE: Unable to lock  
the administration directory (/var/lib/dpkg/), are you root?\n", "rc": 
100, "stdout": "", "stdout_lines": []}

【问题讨论】:

    标签: ansible


    【解决方案1】:

    您需要连接一个具有 sudo 权限的帐户(在您的情况下为 testuser),然后以提升的权限运行 play/task(become: truebecome: root,这是默认的),所以:

    • 要么给ubuntu添加sudo权限,
    • 或与testuser联系。

    sudo 无法按照您在问题中暗示的方式工作。

    任何命令都在特定用户的上下文中运行——testuserubunturoot。没有像“sudo testuser”这样的命令。

    sudo 以不同的用户身份执行命令(默认为root)。执行sudo 的用户必须具有适当的权限。

    • 如果您以testuser 登录并执行sudo -H apt-get update,则(几乎*)与您以root 登录并运行apt-get update 相同。

    • 如果您以 ubuntu 登录并运行 sudo -u testuser apt-get update(这是问题中 Ansible 任务的 shell 对应项)——它(几乎*)与您登录时相同使用testuser 并运行apt-get update

      testuser 运行 apt-get update 会得到一个错误 - 这就是你得到的。


    *“几乎”,因为它取决于有关环境变量的设置——与这里的问题无关。

    【讨论】:

    • 嗨。 Techraf,最后我使用:remote_user:testuser(或ubuntu,只要我可以在没有密码的情况下对它们进行ssh,任何一个都可以),然后变成_user:testuser(如果我想以用户testuser身份运行命令)或become_user:root(如果我想运行 sudo 命令)所以对于上述情况,我使用了 remote_user: testuser 和 become_user: root。
    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    • 2019-01-18
    • 2015-02-07
    • 1970-01-01
    • 2019-01-14
    相关资源
    最近更新 更多