私は、Ansible 1.6.6を使用してマシンをプロビジョニングしています。
私のプレイブックには、Jinja2テンプレートから宛先ファイルを作成するテンプレートタスクがあります。
tasks:
- template: src=somefile.j2 dest=/etc/somefile.conf
すでに somefile.conf
が存在する場合、それを置き換えたくありません。Ansibleで可能でしょうか?もし可能であれば、どのようにすればよいですか?
テンプレートモジュールの force パラメータを使用すればよいのです。
tasks:
- template: src=somefile.j2 dest=/etc/somefile.conf force=no
またはタスクのネーミング ;-)
tasks:
- name: Create file from template if it doesn't exist already.
template:
src: somefile.j2
dest:/etc/somefile.conf
force: no
Ansibleテンプレートモジュール]1のドキュメントより。
force: デフォルトはyesで、次のような場合にリモートファイルを置き換えます。
内容がソースと異なる場合。Noの場合、ファイルは 転送先が存在しない場合、転送されます。
他の回答は、書き込まれた後に force パラメータが追加されたため、 stat
を使用しています。
まず、保存先のファイルが存在するかどうかを確認し、その結果の出力に基づいて判断することができます。
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