我正在尝试使用synchronize模块将文件从Ansible节点复制到远程节点。
我希望这些文件在远程节点上以UserB
的形式存在,但我无法直接访问UserB
。相反,UserA
具有切换到UserB
的sudo权限。所以我以UserA
登录。
我的环境文件说:
ansible_ssh_user=UserA
ansible_ssh_pass=<PassUserA>
ansible_become_method=sudo
ansible_become_user=UserB
ansible_become_pass=<PassUserA>
我的任务是:
- name: Copy and unarchive webapps node.
synchronize: src=/home/ansible/templates/app/Sprint6/webapps dest=/opt/msdp/ca/app checksum=yes
become: yes
但是当我运行剧本时,我收到一个错误:
fatal: [5.232.57.247]: FAILED! => {"changed": false, "cmd": "/usr/bin/rsync --delay-updates -F --compress --checksum --archive --rsh 'ssh -S none -o StrictHostKeyChecking=no -o ControlMaster=auto -o ControlPersist=60s' --rsync-path=\"sudo rsync\" --out-format='<<CHANGED>>%i %n%L' \"/home/ansible/templates/app/Sprint6/webapps\" \"UserA@5.232.57.247:/opt/msdp/ca/app\"", "failed": true, "msg": "sudo: sorry, you must have a tty to run sudo\nrsync: connection unexpectedly closed (0 bytes received so far) [sender]\nrsync error: error in rsync protocol data stream (code 12) at io.c(600) [sender=3.0.6]\n", "rc": 12}
在远程节点上,只有UserB
可以写在:/opt/msdp/ca/app
我缺少配置吗?
答案 0 :(得分:2)
synchronize
模块在控制机器上本地执行(不在目标节点上),默认情况下,它连接到那里使用的帐户(SSH凭证)的节点(当前已登录,或在~/.ssh/config
)。
首先,您需要通过为连接时要考虑的use_ssh_args
值添加ansible_ssh_*
参数来覆盖默认行为:
中指定的ssh_args
use_ssh_args
使用ansible.cfg
其次,因为任务是在本地运行的,所以become
指令仅对控制机器有效。 ansible_become_*
变量不适用于这种情况。
相反,您可以告诉模块运行sudo rsync
以通过rsync_path
参数提升目标节点的权限:
- name: Copy and unarchive webapps node.
synchronize:
src: /home/ansible/templates/app/Sprint6/webapps
dest: /opt/msdp/ca/app checksum=yes
use_ssh_args: yes
rsync_path: "sudo -u UserB rsync"
最后,上面假设sudoers设置允许无密码sudo。如果您确实需要以纯文本形式提供密码,则需要在以下方面提供其他解决方法:
rsync_path: "echo {{ UserA_password }} | sudo -S -u UserB rsync"
答案 1 :(得分:0)
检查目标系统的Defaults requiretty
中的/etc/sudoers
,并将该行替换为Defaults !requiretty
或为Ansible用户禁用requiretty
:
Defaults!/path/to/my/bin !requiretty
Defaults:myansibleuser !requiretty
选中此site以获取更多选项。