【问题标题】:How to fix permission denied error when trying to install packages using Ansible?尝试使用 Ansible 安装软件包时如何修复权限被拒绝错误?
【发布时间】:2018-03-22 00:06:25
【问题描述】:

我正在尝试编写一个简单的 Ansible Playbook,请查看下面的 sn-ps。使用 Ansible 2.4.0.0、Ubuntu 17.04、Python 2.7.13。 这是我第一次使用 Ansible 和 Playbooks,所以请不要太苛刻。我做错了什么?

playbook.yml

---
- name: install packages
  hosts: dbservers
  become: yes
  become_method: sudo
  become_user: user

  tasks:
  - name: Update repositories cache and install "python-minimal" package
  apt:
    name: python-minimal
    update_cache: yes

主机文件

 ---
 [dbservers]
 db ansible_host=127.0.0.1 ansible_port=22 ansible_user=user ansible_ssh_pass=pass ansible_become_pass=pass ansible_become_user=user

命令:ansible-playbook -i hosts playbook.yml -vvv

上面的命令返回以下错误:

The full traceback is:
  File "/tmp/ansible_yozgsn/ansible_module_apt.py", line 287, in <module>
    import apt

fatal: [db]: FAILED! => {
    "changed": false, 
    "cmd": "apt-get update", 
    "failed": true, 
    "invocation": {
        "module_args": {
            "allow_unauthenticated": false, 
            "autoclean": false, 
            "autoremove": false, 
            "cache_valid_time": 0, 
            "deb": null, 
            "default_release": null, 
            "dpkg_options": "force-confdef,force-confold", 
            "force": false, 
            "force_apt_get": false, 
            "install_recommends": null, 
            "name": "python-minimal", 
            "only_upgrade": false, 
            "package": [
                "python-minimal"
            ], 
            "purge": false, 
            "state": "present", 
            "update_cache": true, 
            "upgrade": null
        }
    }, 
    "msg": "W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)\nE: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)\nE: Unable to lock directory /var/lib/apt/lists/\nW: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)\nW: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)", 
    "rc": 100, 
    "stderr": "W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)\nE: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)\nE: Unable to lock directory /var/lib/apt/lists/\nW: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)\nW: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)\n", 
    "stderr_lines": [
        "W: chmod 0700 of directory /var/lib/apt/lists/partial failed - SetupAPTPartialDirectory (1: Operation not permitted)", 
        "E: Could not open lock file /var/lib/apt/lists/lock - open (13: Permission denied)", 
        "E: Unable to lock directory /var/lib/apt/lists/", 
        "W: Problem unlinking the file /var/cache/apt/pkgcache.bin - RemoveCaches (13: Permission denied)", 
        "W: Problem unlinking the file /var/cache/apt/srcpkgcache.bin - RemoveCaches (13: Permission denied)"
    ], 
    "stdout": "Reading package lists...\n", 
    "stdout_lines": [
        "Reading package lists..."
    ]
}

编辑:如果我通过 SSH 连接到同一台机器,我可以手动更新 apt-cache 并使用同一用户(使用 sudo)安装软件包。如果我在 Playbook 中运行命令“whoami”,它会返回预期结果(用户名)。

【问题讨论】:

    标签: ansible ansible-inventory


    【解决方案1】:

    如果您的用户具有 sudo 访问权限,请使用 become: -

    tasks:
      - name: Update repositories cache and install "python-minimal" package
        become: yes
        apt:
          name: python-minimal
          update_cache: yes
    

    【讨论】:

    • 如果我使用 become: - 那么我得到语法错误。我还尝试从剧本和主机(在 CLI 中使用)中删除所有成为变量,但它没有帮助。
    • 您的用户有 sudo 权限吗?您可以成功登录并手动执行此操作吗?
    • 请编辑您的原始帖子以显示这一点。你必须使用sudo?目录权限是什么?
    【解决方案2】:

    我认为您混淆了become_userremote_userremote_user 是 Ansible 将用于 ssh 到服务器的用户,become_user 是 Ansible 将在服务器上切换并运行任务的用户。您可以在Ansible's docs 中找到有关become_userremote_user 的更多信息。

    所以这里发生的事情是您的剧本正试图成为“用户”用户并安装软件包。它不是以根用户身份安装软件包,这正是您所需要的。要解决此问题,您可以从 playbook 中删除 become_user 参数(become_user 默认为 root),或者您可以将 become_user 参数添加到您的任务中。

    - name: Update repositories cache and install "python-minimal" package
      apt:
        name: python-minimal
        update_cache: yes
      become_user: root
    

    【讨论】:

    • 我无权访问 root 用户,我有 sudoers 用户帐户。这是我问题的根源吗? (没有双关语):)
    • 如果您sudo -l,您有权运行哪些命令?我希望我们不必调整 ansible.cfg
    • 您需要以 root 身份安装软件包。您可能已经有权执行此操作 - 我不确定 sudoers 用户帐户的含义。 sudo -l 会告诉我们您是否可以。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 2016-06-27
    • 1970-01-01
    • 2014-11-07
    • 2017-02-28
    • 2017-03-12
    • 2016-03-13
    相关资源
    最近更新 更多