我正在Ansible 1.6.6用来配置我的机器。
我的剧本中有一个模板任务,它从 Jinja2 模板创建目标文件:
tasks:
- template: src=somefile.j2 dest=/etc/somefile.conf
Run Code Online (Sandbox Code Playgroud)
somefile.conf如果它已经存在,我不想替换。Ansible 有可能吗?如果是这样,如何?
Tef*_*tin 68
您可以使用 stat 检查文件是否存在,然后仅当文件不存在时才使用模板。
tasks:
- stat: path=/etc/somefile.conf
register: st
- template: src=somefile.j2 dest=/etc/somefile.conf
when: not st.stat.exists
Run Code Online (Sandbox Code Playgroud)
小智 59
您可以只使用模板模块的force参数:
tasks:
- template: src=somefile.j2 dest=/etc/somefile.conf force=no
Run Code Online (Sandbox Code Playgroud)
或命名任务;-)
tasks:
- name: Create file from template if it doesn't exist already.
template:
src: somefile.j2
dest:/etc/somefile.conf
force: no
Run Code Online (Sandbox Code Playgroud)
来自Ansible 模板模块文档:
force:默认为yes,当内容与源不同时替换远程文件。如果否,则仅当目标不存在时才会传输文件。
使用其他答案stat是因为在写入后添加了force参数。
arb*_*zar 11
您可以先检查目标文件是否存在,然后根据其结果的输出做出决定。
tasks:
- name: Check that the somefile.conf exists
stat:
path: /etc/somefile.conf
register: stat_result
- name: Copy the template, if it doesnt exist already
template:
src: somefile.j2
dest: /etc/somefile.conf
when: stat_result.stat.exists == False
Run Code Online (Sandbox Code Playgroud)