2

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.

CzipO2
  • 403
  • 1
  • 6
  • 15

2 Answers2

3

@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.

Shubham Vaishnav
  • 1,637
  • 6
  • 18
1

Please try as below

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

Smily
  • 2,308
  • 2
  • 17
  • 41
  • Updated to print only the path – Smily Sep 04 '19 at 10:10
  • This seems to work. But do you know how I would use this with lineinfile module? for the "path:"? So that the lineinfile uses the path of the msg (find_result variable). – CzipO2 Sep 04 '19 at 10:13