Ansible – Accessing Debug Values in Ansible

ansibleansible-playbook

My ansible code looks like this,

  ¦ - name: Copying Archived File
  ¦ ¦ unarchive:
  ¦ ¦ ¦ src: "/home/user/{{ item }}"
  ¦ ¦ ¦ dest: "/home/user/someLocation"
  ¦ ¦ ¦ list_files: yes
  ¦ ¦ register: archive_contents
  ¦ ¦ loop: "{{ files_to_unarchive }}"

  ¦ - name: Print Directory Name
  ¦ ¦ debug:
  ¦ ¦ ¦ msg: "{{ archive_contents }}"

The output I am getting is,

ok: [serverName] => {
    "msg": {
        "changed": false, 
        "msg": "All items completed", 
        "results": [
            {
                "ansible_loop_var": "item", 
                "changed": false, 
                "dest": "/home/user/someLocation", 
                "failed": false, 
                "files": [
                    "6880880/", 
                    "6880880/README.txt", 
                    "6880880/opatch_generic.jar", 
                    "6880880/version.txt"
                ], 
                "gid": 110, 
                "group": "oracle", 
                "handler": "ZipArchive", 
                "invocation": {
                    "module_args": {
                        "attributes": null, 
                        "backup": null, 
                        "content": null, 
                        "creates": null, 
                        "delimiter": null, 
                        "dest": "/home/user/someLocation", 
                        "directory_mode": null, 
                        "exclude": [], 
                        "extra_opts": [], 
                        "follow": false, 
                        "force": null, 
                        "group": null, 
                        "keep_newer": false, 
                        "list_files": true, 
                        "mode": null, 
                        "owner": null, 
                        "regexp": null, 
                        "remote_src": false, 
                        "selevel": null, 
                        "serole": null, 
                        "setype": null, 
                        "seuser": null, 
                        "src": "/var/tmp/ansible-tmp-1660003769.3-20299-226224112946024/source", 
                        "unsafe_writes": false, 
                        "validate_certs": true
                    }
                }, 
                "item": "XYZ.zip", 
                "mode": "0755", 
                "owner": "user", 
                "size": 21, 
                "src": "/var/tmp/ansible-tmp-1660003769.3-20299-226224112946024/source", 
                "state": "directory", 
                "uid": 110
            }
        ]
    }
}

I need to use the first value (directory name) from files:6880880 in another task.
I tried msg: "{{ archive_contents['results'] }}" and "{{ archive_contents['results']['files'][0] }}" but getting error. I am fairly new to ansible. I think I am missing something.

Error when using archive_contents['results']['files'][0] ==>
fatal: [serverName]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'files'\n\nThe error appears to be in '/home/user/ansibleScript.yml': line 46, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n – name: Print Directory Name\n ^ here\n"}

Can you please suggest how can I get the desired value?

Best Answer

The attribute results is a list. You have to reference the item to get its attributes. The simplest option is the path to the first item

dir0: "{{ archive_contents.results.0.files.0 }}"

gives

dir0: 6880880/

Generally, there might be more items in the list results. Get them all

dirs: "{{ archive_contents.results|map(attribute='files.0')|list }}"

gives a list

dirs:
  - 6880880/

Get the first item

dirs.0: 6880880/