【问题标题】:Executing the tasks with sudo privileges in ansible playbook在 ansible playbook 中使用 sudo 权限执行任务
【发布时间】:2015-10-17 21:48:15
【问题描述】:

我有简单的 ansible 剧本

- hosts: all
  remote_user: myusername
  become: yes
  become_user: myusername
  become_method: sudo
  tasks:
    - name: Install tmux
      apt: name=tmux state=present

运行 playbook 时出现以下错误。

TASK: [Install tmux] ********************************************************** 
failed: [104.239.140.237] => {"failed": true}
stderr: E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)
E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

我参考http://docs.ansible.com/ansible/become.html提升用户的权限。

用户 'myusername' 属于 sudo 组。

$ sudo -l -U myusername
User myusername may run the following commands on this host:
    (ALL : ALL) ALL

我能够在控制台上使用以下命令成功安装 tmux。在剧本中做同样的事情时,我不确定我错过了什么。

$ sudo apt-get install tmux
Reading package lists... Done
Building dependency tree       
Reading state information... Done

【问题讨论】:

  • 我认为您运行sudo -l -U myusername 的主机是远程ansible 目标,而不是您正在运行ansible-playbook 的主机?还是都是同一个主机?
  • 它是远程节点。

标签: ubuntu-12.04 ansible sudo ansible-playbook privilege-elevation


【解决方案1】:

我会检查或修改 NOPASSWD 的 sudoers 文件,你的剧本对我有用,我看到的唯一区别是:

User myusername may run the following commands on this host:
    (ALL : ALL) ALL
    (ALL) NOPASSWD: ALL

这已由 Ansible 文档确认,其中指出:

–become,-b
run operations with become (no password implied)

如果您无法为此更改服务器端配置,您仍然可以使用sudo 指令。

http://docs.ansible.com/ansible/become.html

【讨论】:

    猜你喜欢
    • 2018-05-19
    • 2017-09-16
    • 2023-02-25
    • 2016-06-18
    • 2020-08-22
    • 2015-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多