首页 > 解决方案 > ansible_user 和 become_user 不工作

问题描述

我正在尝试通过用户“naresh”连接到目标服务器并通过用户“vijay”安装 apache2,“vijay”用户是无密码的 sudo 用户,但我的剧本仍然失败。

ansible-playbook ds.yml  -u naresh

猫ds.yml

---
- hosts: all
  gather_facts: no
  tasks:

  - name: install apache2
    command: apt install apache2 -y
    register: abcd
    become: yes
    become_user: vijay


  - debug: var=abcd

我得到以下错误


fatal: [52.87.204.142]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"}, "changed": true, "cmd": ["apt", "install", "apache2", "-y"], "delta": "0:00:00.007384", "end": "2019-11-06 14:25:58.346627", "msg": "non-zero return code", "rc": 100, "start": "2019-11-06 14:25:58.339243", "stderr": "\nWARNING: apt does not have a stable CLI interface. Use with caution in scripts.\n\nE: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)\nE: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?", "stderr_lines": ["", "WARNING: apt does not have a stable CLI interface. Use with caution in scripts.", "", "E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)", "E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?"], "stdout": "", "stdout_lines": []}

标签: ansible

解决方案


从您的错误消息中简单摘录:

无法获取 dpkg 前端锁 (/var/lib/dpkg/lock-frontend),你是 root 吗?

您作为第一个用户(naresh,没有管理员权限)连接成为第二个用户(vijay,也没有管理员权限)。这不起作用,ansible 不会发出任何其他升级。

你需要:

  1. 与具有完全 sudo 权限的用户联系(例如,在您的情况下看起来像是“vijay”)
  2. 成为具有管理权限的用户(即在这种情况下为 root,become如果您未指定,则使用时默认为 root become_user

绝对是的!正如@Ash 所指出的,使用该apt模块。


推荐阅读