在ansible下使用sudo权限进行复制

时间:2014-03-02 12:17:36

标签: ansible ansible-playbook

我在Ansible中有以下剧本:

---
- hosts: ESNodes
  sudo: yes
  tasks:
  - name: Copy Elasticsearch
    copy: src=~/elasticsearch-1.0.0.tar.gz dest=/opt/elasticsearch-1.0.0.tar.gz
  - name: Untar Elasticsearch
    action: shell cd /opt/ && tar xvfz elasticsearch-1.0.0.tar.gz

按如下方式运行剧本时:

bash-4.1$ ansible-playbook es_playbook.yml  -K 
sudo password: 

PLAY [ESNodes] **************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [isk-vsrv643]

TASK: [Copy Elasticsearch] **************************************************** 
failed: [isk-vsrv643] => {"failed": true}
msg: Could not replace file: /usr2/ihazan/.ansible/tmp/ansible-1393762341.08-257257560838959/source to /opt/elasticsearch-1.0.0.tar.gz: [Errno 13] Permission denied: '/usr2/ihazan/.ansible/tmp/ansible-1393762341.08-257257560838959/source'

FATAL: all hosts have already failed -- aborting

PLAY RECAP ******************************************************************** 
           to retry, use: --limit @/usr2/ihazan/es_playbook.retry

isk-vsrv643                : ok=1    changed=0    unreachable=0    failed=1   

它因权限问题而失败。

在远程主机上我无法做到:

-bash-4.1$ cp elasticsearch-1.0.0.tar.gz /opt/
cp: cannot create regular file `/opt/elasticsearch-1.0.0.tar.gz': Permission denied

但我能做到:

-bash-4.1$ sudo cp elasticsearch-1.0.0.tar.gz /opt/

我在剧本中出错了什么?

提前谢谢

2 个答案:

答案 0 :(得分:1)

Ansible可能甚至没有登录。你错过了user指令:

---
- hosts: ESNodes
  user: whatever_user_that_you_use_to_login_and_has_sudo_access
  sudo: yes

  tasks:
  - name: Copy Elasticsearch
    copy: src=~/elasticsearch-1.0.0.tar.gz dest=/opt/elasticsearch-1.0.0.tar.gz
  - name: Untar Elasticsearch
    action: shell cd /opt/ && tar xvfz elasticsearch-1.0.0.tar.gz

希望它有所帮助。

答案 1 :(得分:0)

hazan,可能是你错过了在目的地主机中制作sudo的用户:如果你没有在playbooks中声明它,你需要在ansible-playbook命令中添加额外的vars,例如:

ansible-playbook -i hosts playbook.yml --extra-vars "user=your_user"

然后,ansible将使用目标主机中的“your_user”来运行任务。