Home > OS >  Unable to produce a newline for useful output with ansible shell output going to lineinfile module
Unable to produce a newline for useful output with ansible shell output going to lineinfile module

Time:07-16

Tasks below, When I run the find command on the shell, I get output expected. When I debug it after registering the variable, I also get the output one per line.

When I run these tasks I wrote, I get a mess of output that just won't work no matter where I put the newline.

      ansible.builtin.shell:
        cmd: find / -xdev \( -perm -4000 -o -perm -2000 \) -type f | awk '{print "-a always,exit -F path=" $1 " -F perm=x -F auid>=1000 -F auid!=4294967295 -k privileged" }'
      register: ps

    - name: Put them into the rules file
      ansible.builtin.lineinfile:
        path: "/etc/audit/rules.d/audit.rules"
        insertafter: EOF
        line: "{{ ps.stdout_lines }}" 

I've tried adding a newline to the end of the find both inside and outside of the quote but it's getting mashed up somehow.

What is a fix? and why is this not working as is ?

CodePudding user response:

stdout is raw output while stdout_lines is an array that "{ ... }" converts to a string (without newlines).

CodePudding user response:

- name: find privileged files
      ansible.builtin.shell:
        cmd: find / -xdev \( -perm -4000 -o -perm -2000 \) -type f | awk '{print "-a always,exit -F path=" $1 " -F perm=x -F auid>=1000 -F auid!=4294967295 -k privileged" }'
      register: ps

    - name: Put them into the rules file
      ansible.builtin.blockinfile:
        path: "/etc/audit/rules.d/audit.rules"
        insertafter: EOF
        state: present
        block: "{{ ps.stdout }}"

Thank you to everyone who replied, I figured out the issue. Posted modified code above.

  • Related