![Ansible 在现有用户的主目录中创建具有用户权限的文件](https://linux22.com/image/746182/Ansible%20%E5%9C%A8%E7%8E%B0%E6%9C%89%E7%94%A8%E6%88%B7%E7%9A%84%E4%B8%BB%E7%9B%AE%E5%BD%95%E4%B8%AD%E5%88%9B%E5%BB%BA%E5%85%B7%E6%9C%89%E7%94%A8%E6%88%B7%E6%9D%83%E9%99%90%E7%9A%84%E6%96%87%E4%BB%B6.png)
我有几个用户的 homedir,例如 /home/user1 /home/user2 /home/user(n-1)
我需要 ansible 来获取主目录列表并在每个主目录中以正确的权限创建文件。
现在我有了这个剧本
---
- hosts: pc070
tasks:
- name: Find home
find:
paths: /home
recurse: no
file_type: directory
register: homedir
- name: Create temp directory
file:
path: "{{ item.path }}/temp"
state: directory
with_items:
"{{ homedir.files }}"
但是我得到了 root 所有者的目录
答案1
嗯,很简单
---
- hosts: pc070
tasks:
- name: Find home
find:
paths: /home
recurse: no
file_type: directory
register: homedir
- name: Create ASS directory
file:
path: "{{ item.path }}/bigboobsinside"
state: directory
with_items:
"{{ homedir.files }}"
- name: Set permission
file:
path: "{{ item.path }}"
state: directory
recurse: yes
owner: "{{ item.path | basename }}"
group: "domain users"
with_items:
"{{ homedir.files }}"