0

For some reasons, I'm not allowed to use jsqon_query with Ansible, I'm trying to reach stdout element in a results list in a variable resulting from a shell call.

The JSON variable is saved this way :

"request": {
    "changed": true, 
    "msg": "All items completed", 
    "results": [
        {
            "_ansible_ignore_errors": null, 
            "_ansible_item_result": true, 
            "_ansible_no_log": false, 
            "_ansible_parsed": true, 
            "changed": true, 
            "cmd": "echo \"****:********\" | grep -o -P '^*****:[^\\n]*$' | awk '{split($0,a,\":\"); print a[2]}'", 
            "delta": "0:00:00.003660", 
            "end": "2018-10-31 17:26:17.697864", 
            "failed": false, 
            "invocation": {
                "module_args": {
                    "_raw_params": "echo \"**************\" | grep -o -P '^************:[^\\n]*$' | awk '{split($0,a,\":\"); print a[2]}'", 
                    "_uses_shell": true, 
                    "chdir": null, 
                    "creates": null, 
                    "executable": null, 
                    "removes": null, 
                    "stdin": null, 
                    "warn": true
                }
            }, 
            "item": "**********:************", 
            "rc": 0, 
            "start": "2018-10-31 17:26:17.694204", 
            "stderr": "", 
            "stderr_lines": [], 
            "stdout": "**********", 
            "stdout_lines": [
                "*********"
            ]
        }
    ]
}

}

I'm trying to browse my stdout element this way :

- name: Tarball copy
  copy: src= "{{ '%s/%s' | format( TARBALL_DIR , request.results[0].stdout ) }}" dest= "/tmp/tarball/"

I tryied also :

- name: Tarball copy
  copy: src= "{{ '%s/%s' | format( TARBALL_DIR , request.results[.stdout] ) }}" dest= "/tmp/tarball/"

- name: Tarball copy
  copy: src= "{{ '%s/%s' | format( TARBALL_DIR , item.stdout ) }}" dest= "/tmp/tarball/"
  with_items: "{{ request.results }}"

I've no idea why I'm always getting the same error : - template error while templating string: unexpected '.'. String: {{ request.results[.stdout] }} (when trying with [.stdout) - The task includes an option with an undefined variable (when putting [0] index)

Waldo
  • 189
  • 1
  • 8
  • 1
    The first one looks like it should work, except that you need to single quote TARBALL_DIR like so: `'TARBALL_DIR'`. – Jack Oct 31 '18 at 17:04
  • Even if ```TARBALL_DIR``` is a host var ? – Waldo Nov 02 '18 at 08:00
  • No, not if that is a variable. Can you post the error you get from that? – Jack Nov 02 '18 at 14:06
  • That was the following one : ```- template error while templating string: unexpected '.'. String: {{ request.results[.stdout] }}``` – Waldo Nov 02 '18 at 14:16
  • 1
    That's the second one. What was the output from ` request.results[0].stdout `? – Jack Nov 02 '18 at 14:49
  • Sorry yes, I meant the second message `- The task includes an option with an undefined variable` – Waldo Nov 02 '18 at 14:51

1 Answers1

0

I've finally solved my problem using :

- name: Tarball copy
  copy: 
    src: "{{ '%s/%s' | format( TARBALL_DIR , request.results[0].stdout ) }}" 
    dest: "/tmp/tarball/"

It seems that src and dest couldn't accept space after the equal char.

Waldo
  • 189
  • 1
  • 8