我是 Ansible 的新手,所以这可能是个愚蠢的问题。感谢您的耐心等待。
我的子节点上有两个用户:ubuntu
和 ansible
我的控制节点上有一个用户: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/