ansible - 尝试使用 Ansible 安装软件包时如何修复权限被拒绝错误?

标签 ansible ansible-inventory

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

剧本.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”,它会返回预期结果(用户名)。

最佳答案

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

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

关于ansible - 尝试使用 Ansible 安装软件包时如何修复权限被拒绝错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46669172/

相关文章:

azure - azure 的 Ansible 库存插件抛出编码错误

ansible - 使用 with_items 连接字符串并分配给 Ansible 中的变量

ansible查找管道这个管道是什么意思?

ansible - 访问remote_user变量

python-2.7 - Ansible Azure 模块

ansible - 有没有办法在 Ansible Inventory 文件中验证一个组的主机数量?

ansible - 从输入文件/变量循环ansible ec2主机名

ansible - 如何使用 ansible 的剧本设置默认路径?

security - 如何阻止 Ansible 中的 extra_vars 覆盖变量?

ansible - 如何在 Ansible 中循环库存并分配值(value)