在ansible playbook中使用sudo权限执行任务

时间:2015-10-17 21:48:15

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

我有简单的ansible playbook

- 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

1 个答案:

答案 0 :(得分:3)

我会检查或修改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