Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible - how to register output from "FIND" module and use in other

Tags:

ansible

I need to find a files in unknown directory place and remove them. Tried to use "find" module, register its output, and pass it to "file".

Even if I see path registered, I can not use it later:

< TASK [print find_result] >
ok: [1.2.3.4] => {
    "find_result": {
        "changed": false, 
        "examined": 3119, 
        "files": [
            {
                "atime": 1483973253.7295375, 
           ...
                "mode": "0600", 
                "mtime": 1483973253.7295375, 
                "nlink": 1, 
                "path": "/tmp/delme", 

My playbook is:

- hosts: "{{ target }}"
  become: no
  vars:
    find_what: "delme*"
    find_where: "/tmp"

  tasks:
  - name: finding files
    find:
      paths:            "{{ find_where }}"
      patterns:         "{{ find_what }}"
      recurse:          "yes"
      file_type:        "file"
    register: find_result

# \/ for debugging
  - name: print find_result
    debug: var=find_result

  - name: remove files
    file:
        path= "{{ item.path }}"
        state=absent
    with_items: "{{ find_result.files }}"
like image 983
Marcin P Avatar asked Jan 10 '17 09:01

Marcin P


People also ask

How do I register variables in Ansible?

Registering variables To see the values for a particular task, run your playbook with -v . Registered variables are stored in memory. You cannot cache registered variables for use in future plays. Registered variables are only valid on the host for the rest of the current playbook run.


Video Answer


1 Answers

There's a syntax flaw in file task – space after =.

Try:

- name: remove files
  file:
    path: "{{ item.path }}"
    state: absent
  with_items: "{{ find_result.files }}"
like image 193
Konstantin Suvorov Avatar answered Dec 09 '22 23:12

Konstantin Suvorov