--- - name: Check for rc.local file stat: path: /etc/rc.local register: rcfile - name: Create rc.local if needed lineinfile: path: /etc/rc.local line: "#!/bin/sh -e" create: true insertbefore: BOF mode: "u=rwx,g=rx,o=rx" when: not rcfile.stat.exists - name: Write rc.local file blockinfile: path: /etc/rc.local content: "{{ lookup('template', 'templates/rc.local.j2') }}" state: present notify: reboot server