Strip doube quotes from list

I need help stripping the " " from around the joined list.

vars:
 sox_files:
        - '/etc/login.defs'
        - '/etc/default/useradd'
        - '/etc/passwd'
        - '/etc/shadow'
        - '/etc/group'
        - '/etc/hosts'
        - '/etc/audit/auditd.conf'
    
     - name: Join the list of strings
       debug:
        msg: "{{ sox_files | join(' ') }}"
       tags: bug

     - name: Compress into archive
       ansible.builtin.shell:
          cmd: |
            zip /tmp/sox/audit.zip "{{ sox_files | join(' ') }}"

When I run the playbook the zip command fails because the list of files is encapsulated in quotes.

TASK [Join the list of strings] *************************************************************************************************************************************************************
ok: [testhost] => {
“msg”: “/etc/login.defs /etc/default/useradd /etc/passwd /etc/shadow /etc/group /etc/hosts /etc/audit/auditd.conf”
}

TASK [Compress into archive] ****************************************************************************************************************************************************************
fatal: [testhost]: FAILED! => {“changed”: true, “cmd”: “zip /tmp/sox/audit.zip "/etc/login.defs /etc/default/useradd /etc/passwd /etc/shadow /etc/group /etc/hosts /etc/audit/auditd.conf"\n”, “delta”: “0:00:00.005151”, “end”: “2024-10-24 18:00:48.138459”, “msg”: “non-zero return code”, “rc”: 12, “start”: “2024-10-24 18:00:48.133308”, “stderr”: “”, “stderr_lines”: , “stdout”: “\tzip warning: name not matched: /etc/login.defs /etc/default/useradd /etc/passwd /etc/shadow /etc/group /etc/hosts /etc/audit/auditd.conf\n\nzip error: Nothing to do! (/tmp/sox/audit.zip)”, “stdout_lines”: [“\tzip warning: name not matched: /etc/login.defs /etc/default/useradd /etc/passwd /etc/shadow /etc/group /etc/hosts /etc/audit/auditd.conf”, “”, “zip error: Nothing to do! (/tmp/sox/audit.zip)”]}

Hii

| sturnisvulgaris Michael
October 24 |

  • | - |

I need help stripping the " " from around the joined list.

vars:
 sox_files:
        - '/etc/login.defs'
        - '/etc/default/useradd'
        - '/etc/passwd'
        - '/etc/shadow'
        - '/etc/group'
        - '/etc/hosts'
        - '/etc/audit/auditd.conf'
    
     - name: Join the list of strings
       debug:
        msg: "{{ sox_files | join(' ') }}"
       tags: bug

     - name: Compress into archive
       ansible.builtin.shell:
          cmd: |
            zip /tmp/sox/audit.zip "{{ sox_files | join(' ') }}"

When I run the playbook the zip command fails because the list of files is encapsulated in quotes

That is because you asked it to encapsulate it in quotes :slight_smile:
If you remove them, it will work.
But, there are still two issues then. One is that arguments without any escaping/quote is fragile, so it will break with filename that have spaces, quotes, etc.
The other problem is that you’re using a shell module for something that a real ansible module can do.
This is simpler to use and less error prone:

  • name: compress with ansible
    community.general.archive:
    dest: /tmp/sox2.zip
    path: “{{ sox_files }}”
    format: zip
2 Likes

Thank you. I had to install the community modules first.

1 Like