Parikshit Rastogi
Parikshit Rastogi

Reputation: 17

Playbook where item.stat.exist not working

I have created playbook which will run on a remote host and check whether the files exist or not. I want to extract the only files which are not present on the remote host. But my playbook giving all paths whether they are present or not.

Playbook:-

- name: Playbook for files not present on remote hosts
  hosts: source
  gather_facts: false
  vars:
     Filepath: /opt/webapps/obiee/oracle_common/inventory/ContentsXML/comps.xml
  tasks:
    - name: Getting files location path
      shell:  grep -i "COMP NAME" {{ Filepath }} |sed 's/^.*INST_LOC="//'|cut -f1 -d'"' | sed '/^$/d;s/[[:blank:]]//g' // extract files from comps.xml
      register: get_element_attribute

    - name: check path present or not
      stat:
        path: "{{ item }}"
      with_items:
         - "{{ get_element_attribute.stdout_lines }}"
      register: path_output

    - name: path exists or not
      set_fact:
         path_item: "{{ item }}" # here i am getting the output as expected that's files not present on remote host
      with_items: "{{ path_output.results }}"
      register: final_output
      when:  item.stat.exists == False

    - debug:
        var: final_output  #  giving both output i.e. files present and absent

    - name: Create a fact list
      set_fact:        
       paths: "{{ final_output.results |  map(attribute='item.item') | list }}" # i have add this condition  " item.stat.exists == False' inside this stmt

    - name:  Print Fact
      debug:
        var: paths

Upvotes: 0

Views: 350

Answers (2)

Parikshit Rastogi
Parikshit Rastogi

Reputation: 17

The issue resolved by using below command:

- name: Create a fact list
  set_fact:
    paths: "{{ final_output.results | selectattr('item.stat.exists', 'equalto', false) | map(attribute='item.item') |  list }}"
  register: config_facts

Upvotes: 1

phaebel
phaebel

Reputation: 66

The following query should get all the file names which don't exsist on the remote host and store them in the fact 'paths':

- name: Create a fact list
  set_fact:    
    paths: "{{ final_output | json_query(query)}}"
  vars:
    query: "results[?(@._ansible_item_label.stat.exists==`false`)]._ansible_item_label.item" 

Upvotes: 0

Related Questions