linux - Ansible:将文件复制到另一个用户的主目录

标签 linux ubuntu ansible sysadmin

我是 Ansible 的新手,所以这可能是个愚蠢的问题。感谢您的耐心等待。

我的子节点上有两个用户:ubuntuansible

我的控制节点上有一个用户:ubuntu

我在我的子节点上创建了 ansible 用户来测试多个用户/隔离 ansible。也许这不是一个好主意?

我正在尝试将测试文件从我的控制节点复制到我的子节点。我以 ansible 用户身份连接(因为我在 sudoers 文件中授予他们无密码 sudo,我不想为 ubuntu 用户执行此操作)。但是我无法将文件复制到 ubuntu 用户的主文件夹中。我能够复制到 ansible 用户的主文件夹中。

我想做的事情可行吗?我找不到太多这方面的读物,所以我猜我正在以错误的方式处理这个问题......有没有更好的方法来做到这一点?

这是我的剧本:

---
- name: script transfer practice
  hosts: devdebugs
  remote_user: ansible

  tasks:
  - name: Copy file with owner and permissions
    ansible.builtin.copy:
      src: /home/ubuntu/files/test.txt
      dest: /home/ubuntu/test.txt
      owner: ubuntu
      group: ubuntu
      mode: '0600'
...

注意:它适用于 dest /home/ansible/test.txt。它不适用于 dest /home/ubuntu/test.txt

最佳答案

I created the ansible user on my child node to test out multiple users/isolate ansible. Maybe this is not a good idea?

为您的部署指定一个在目标主机上具有完全升级权限的特定用户是运行 ansible 的最常见设置。

Is what I'm trying to do possible?

绝对不是。如果您已经正确地为您的 ansible 用户设置了升级权限,那么您在任务或游戏中所缺少的就是 become: true。在播放级别,它将影响该播放的所有任务:

---
- name: script transfer practice
  hosts: devdebugs
  remote_user: ansible
  become: true

  # here goes the rest of your play....

在任务级别,它只会影响给定的任务。

  - name: Copy file with owner and permissions
    ansible.builtin.copy:
      src: /home/ubuntu/files/test.txt
      dest: /home/ubuntu/test.txt
      owner: ubuntu
      group: ubuntu
      mode: '0600'
    become: true

正如@SipSeb 在评论中所报告的那样,您还可以在运行时使用 ansible(-playbook) 上的 -b/--become 标志为整个剧本设置 become 标志) 命令行。

I couldn't find much reading on this

可能是因为您是 ansible 的新手,不知道要寻找什么。对于这个特定主题,一个好的起点是 understanding ansible privilege escalation

关于linux - Ansible:将文件复制到另一个用户的主目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67424572/

相关文章:

linux - Bash根据另一个文件中的行重命名多个文件

windows - 无法 telnet/ping 虚拟机上安装的服务器

QT Creator,编译和部署一个c或c++到远程设备(BeagleBone或R pi)

maven - 使用 ansible playbook 在 docker 容器内运行 mvn clean install

python - 使用 OpenSSL 静态编译 Python 3.6

linux - linux 下 make 上的 tab 补全错误

c++ - 使用 libsocketcan 从用户空间启动/停止 CAN 板

linux - 用于 ubuntu 的稳定和简单的 amqp 服务器

ssh - 是否有任何条件或事实可以检查主机是否可以使用SSH成功​​登录另一台服务器?

docker - 带有远程Docker守护程序的Packer Docker Builder