Search code examples
ansiblefindmsg

How to msg only the file path in ansible?


This is my ansible:

  - name: finding files
     find:
       paths: /etc/nginx
       patterns: '{{ my_vhost }}'
       recurse: "yes"
       file_type: "file"
     delegate_to: '{{ my_server }}'
     register: find_result

   - name: output the path of the conf file
     debug: msg="{{ find_result.files }}"

and the output of msg is:

    "msg": [
        {
            "atime": 1567585207.0371234, 
            "ctime": 1567585219.9410768, 
            "dev": 64768, 
            "gid": 1001, 
            "inode": 4425684, 
            "isblk": false, 
            "ischr": false, 
            "isdir": false, 
            "isfifo": false, 
            "isgid": false, 
            "islnk": false, 
            "isreg": true, 
            "issock": false, 
            "isuid": false, 
            "mode": "0644", 
            "mtime": 1567585219.9410768, 
            "nlink": 1, 
            "path": "/etc/nginx/sites-enabled/specified-file", 
            "rgrp": true, 
            "roth": true, 
            "rusr": true, 
            "size": 546, 
            "uid": 1001, 
            "wgrp": false, 
            "woth": false, 
            "wusr": true, 
            "xgrp": false, 
            "xoth": false, 
            "xusr": false
        }
    ]
}

and I only want this line to be outputed:

 "path": "/etc/nginx/sites-enabled/specified-file",

I don't really care about the msg, specifically I want to use just this path "/etc/nginx/sites-enabled/specified-file" for later usage. There will always be only one result of the file path in my system.


Solution

  • @CzipO2, You can use the below tasks which also sets the file path in a variable which can be used later in the playbook,

    - set_fact:
        filepath: "{{ find_result.files | map(attribute='path') | list | first}}"
    
    - name: output the path of the conf file
      debug: 
        msg: "{{ filepath }}"
    

    I don't really care about the msg, specifically, I want to use just this path "/etc/nginx/sites-enabled/specified-file" for later usage.

    The filepath fact can be used in the playbook for later use.