Is it possible to use inline templates?

Yes, in that simple case it is possible with the lineinfile module.

- lineinfile:
  dest=/path/to/REVISION
  line="{{ git_commit }}"
  regexp=".*"
  create=yes

The lineinfile module usually is used to ensure that a specific line is contained inside a file. The create=yes option will crete the file if it does not exist. The regexp=.* option makes sure you do not add content to the file if git_commit changes, because it would by default simply make sure the new content is added to the file and not replace the previous content.

This only works since you only have one line in your file. If you'd had more lines this obviously would not work with this module.


Another option to the lineinfile module (as given by udondan's answer) would be to use the copy module and specify the content rather than a source local to the Ansible host.

An example task would look something like:

- name: Copy commit ref to file
  copy:
    content: "{{ git_commit }}"
    dest: /path/to/REVISION

I personally prefer this to lineinfile as for me lineinfile should be for making slight changes to files that are already there where as copy is for making sure a file is in a place and looking exactly like you want it to. It also has the benefit of coping with multiple lines.

In reality though I'd be tempted to make this a template task and just have a the template file be:

"{{ git_commit }}"

Which gets created by this task:

- name: Copy commit ref to file
  template:
    src: path/to/template
    dest: /path/to/REVISION

It's cleaner and it's using modules for exactly what they are meant for.

Tags:

Ansible