--- - name: Check for rc.local file ansible.builtin.stat: path: /etc/rc.local register: rcfile - name: Create rc.local if needed ansible.builtin.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 ansible.builtin.blockinfile: path: /etc/rc.local content: "{{ lookup('template', 'templates/rc.local.j2') }}" state: present notify: reboot server