将文件复制到远程服务器但是在属于其他用户的目录中?

问题描述 投票:-2回答:1

我正在开发一个ansible playbook,它将我的tasks.tar.gz文件复制到远程服务器并将其解压缩到远程服务器上的特定目录中。

我在machineA上运行我的playbook作为用户david所以我将david用户的公钥从machineA添加到所有远程服务器authorized_key文件,以便我可以在不输入密码的情况下ssh,因为我想运行我的ansible playbook无密码。

---
- hosts: ALL
  serial: 3
  tasks:
      - name: copy and untar latest tasks.tar.gz file
        unarchive: src=tasks.tar.gz dest=/data/files/tasks/

      - name: sleep for few seconds
        pause: seconds=20

我现在遇到的问题是因为远程服务器上的这个“/ data / files / tasks /”目录属于其他用户(goldy)所以它无法复制和解压缩tasks.tar.gz文件,因为我正在运行我的playbook作为david用户我想。

ansible-playbook -e 'host_key_checking=False' test2.yml

我想以用户david无密码运行我的ansible playbook,但它应该能够将文件复制到属于用户goldy的目录中的所有远程服务器。我试过和becomebecome_user一起玩,但它对我不起作用。还有什么我需要做的吗?

  - name: copy and untar latest tasks.tar.gz file
    unarchive: src=tasks.tar.gz dest=/data/files/tasks/
    become: true
    become_user: goldy
    become_method: sudo

这是我得到的错误:

"msg": "Failed to set permissions on the temporary files Ansible needs to create when becoming an unprivileged user (rc: 1, err: chown: changing ownership of ‘/tmp/ansible-tmp-1518323151.21-195554555527544/’: Operation not permitted\nchown: changing ownership of ‘/tmp/ansible-tmp-1518323151.21-195554555527544/stat.py’: Operation not permitted\n}).
linux ansible
1个回答
1
投票

由于您提示为连接用户david配置了sudo,您可以做的最简单的事情是使用提升的权限来复制文件,并通过goldy模块的owner参数将其所有权设置为unarchive

- name: copy and untar latest tasks.tar.gz file
  unarchive:
    src: tasks.tar.gz
    dest: /data/files/tasks/
    owner: goldy
  become: true

对于如何配置sudoers以允许代表root以外的用户执行命令的问题,您需要了解sudosudoers实际上是如何工作的(请参阅the manual)。

© www.soinside.com 2019 - 2024. All rights reserved.