我喜欢在ansible中使用template (或copy)函数,如下所示:
- name: Template a file to /etc/files.conf
template:
src: /mytemplates/foo.j2
dest: /etc/file.conf问题是,我没有远程文件/etc/file.conf的直接写权限,我只能通过sudoedit修改它
是否可以通过template命令管道通过sudoedit修改或复制这样的文件?
发布于 2020-05-08 08:59:36
事实上,我找到了一个解决办法:
# sudoedit.yml
---
- name: Create temp file
tempfile:
suffix: ".{{ sudoedit.suffix }}"
register: tempfile
check_mode: no
changed_when: false
- name: "Check if {{ sudoedit.suffix }} exists"
stat:
path: "{{ sudoedit.dest }}"
register: dest
- name: "Copy content of {{ sudoedit.suffix }} into temp file"
copy:
src: "{{ sudoedit.dest }}"
dest: "{{ tempfile.path }}"
remote_src: yes
diff: no
check_mode: no
changed_when: false
when: dest.stat.exists
- name: "Copy file {{ sudoedit.suffix }}"
copy:
src : "{{ sudoedit.src }}"
dest: "{{ tempfile.path }}"
register: sudoresult
- name: "Modify file {{ sudoedit.suffix }} with sudoedit"
shell:
cmd: sudoedit -n "{{ sudoedit.dest }}"
stdin: ":%d|:r {{ tempfile.path }}|:1d|:wq"
executable: /bin/bash
environment:
SUDO_EDITOR: /usr/bin/vi
when: sudoresult.changed
changed_when: false
- name: Delete the temp file
file:
path: "{{ tempfile.path }}"
state: absent
changed_when: false
when: tempfile.path is defined
...然后像这样使用它:
- name: Set NFS exports
include_tasks: sudoedit.yml
vars:
sudoedit:
src: source/etc/exports
dest: /etc/exports
suffix: exports发布于 2019-12-12 20:01:25
问:“是否可以在授权的文件上使用ansible template/copy ?我只能通过sudo编辑修改它。是否可以通过sudo编辑和修改或复制文件来引导模板命令?”
答:没有。这不可能。特权提升必须是通用的。对受限制的文件使用template、copy或任何其他不可用模块,但command、shell和类似的模块除外,它们可以使用sudoedit命令。
“不能将权限提升权限限制在某些命令上.”
https://stackoverflow.com/questions/59311616
复制相似问题