我有一个简单易懂的剧本
- 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
我会检查或修改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
指令。