Search code examples
ansibleansible-2.xansible-template

Ansible set_fact output


I need the string or destination path as the output and use it in other task but the output displayed is dfifferent. Here is the register output

ok: [localhost] => {
    "msg": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": true,
                "checksum": "b045e5836bbd01d9c6dd2b7426afb5d1c8957b30",
                "dest": "/home/ec2-user/bb/Data-1.1.tar.gz",
                "failed": false,
                "invocation": {
                    "module_args": {
                        "_original_basename": null,
                        "attributes": null,
                        "backup": false,
                        "checksum": null,
                        "content": null,
                        "delimiter": null,
                        "dest": "/home/ec2-user/bb/Data-1.1.tar.gz",
                        "directory_mode": null,
                        "follow": false,
                        "force": true,
                        "group": null,
                        "local_follow": null,
                        "mode": null,
                        "owner": null,
                        "regexp": null,
                        "remote_src": true,
                        "selevel": null,
                        "serole": null,
                        "setype": null,
                        "seuser": null,
                        "src": "/home/ec2-user/aa/Data-1.1.tar.gz",
                        "unsafe_writes": null,
                        "validate": null
                    }
                },
                "item": "/home/ec2-user/aa/Data-1.1.tar.gz",
                "md5sum": "df9309334454cc3ceac9a6ac8fea8989",
                "src": "/home/ec2-user/aa/Data-1.1.tar.gz"

I used the below the task to display the destination path

     - set_fact:
          filefact: "{{ output.results | map(attribute='item') | string }}"
     - debug:
          msg: "{{ filefact }}"

The output displayed is

ok: [localhost] => {
    "msg": "<generator object do_map at 0x7f59539f01e0>"

Solution

  • i'd write :

     - set_fact:
          filefact: "{{ filefact | default([]) + [output.results.item] }}"
     - debug:
          msg: "{{ filefact }}"
    

    the right syntax , following your output should be:

    - set_fact:
        filefact: "{{ filefact | default([]) + [output.results[0].item] }}"
    

    or

    - set_fact:
        filefact: "{{ filefact | default([]) + [item.item] }}"
      loop: "{{ output.results }}"