2

我有简单的 ansible 剧本

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

运行剧本时出现以下错误。

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
4

1 回答 1

3

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

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

于 2015-10-17T22:00:28.993 回答