1

我在 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/

我在剧本中做错了什么?

提前谢谢

4

2 回答 2

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

希望能帮助到你。

于 2014-03-06T03:57:33.483 回答
0

hazan,可能是您缺少在目标主机中制作 sudo 的用户:如果您没有在 playbook 中声明它,则需要在 ansible-playbook 命令中添加额外的变量,例如:

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

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

于 2014-07-11T14:03:46.620 回答