senthil Kumar B.K
senthil Kumar B.K

Reputation: 35

Ansible- How to include /import playbook based on when condition result

I have folder in which i have many sub folder and those sub folder will have yaml file, by using the below code , I have got all yaml file and register those file,

  tasks:
- name: Find .yml files
  find:
    paths: /temp/
    patterns: '*.yml,*.yaml'
    recurse: yes
  register: yaml_path_files

One of the yaml file content is below,

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO
  
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO

i want to check if the yaml contains kind is ConfigMap and that yaml file needs to be passed to role/another playbook as parmeter,i tried with below code, it is not working,

    - debug:
    var: item.path
  when: lookup('file',item.path ) | from_yaml_all | list | selectattr('kind', '==', 'ConfigMap') | list | length > 0
  with_items: "{{ yaml_path_files.files }}"

i am facing the below error,

fatal: [localhost]: FAILED! => {"msg": "The conditional check 'lookup('file',item.path ) | from_yaml | list | selectattr('kind', '==', 'ConfigMap') | list | length > 0' failed. The error was: error while evaluating conditional (lookup('file',{{ filename }} ) | from_yaml | list | selectattr('kind', '==', 'ConfigMap') | list | length > 0): 'dict object' has no attribute 'kind'\n\nThe error appears to be in '/cygdrive/c/senthil/Ansible/Ansibleusecase4/EKSartifactvalidationV2.yml': line 14, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n # with_items: "{{ yaml_path_files.files }}"\n - debug:\n ^ here\n"}

kindly help me to achive it in ansible

Upvotes: 1

Views: 1403

Answers (1)

Zeitounator
Zeitounator

Reputation: 44615

Preliminary note: Your question is missing a complete playbook to be 100% sure of the problem. Edit your question with more info, possibly with an MCVE, if this is not your exact issue.

Your find tasks looks for files on your remote target. The file lookup, as all other lookups (see notes in the lookup plugins documentation), reads files locally on your ansible controller. In other words, you're looking for a file which does not exists.

You have to fetch or slurp the files from the remote target to get their content.

Here is a quickly written untested sample to put you on track. Please note on the last task that item.item.path is not a typo. item.item references the preceding item loop when registering the new var yaml_files_content (i.e. each yaml_path_files.files[*] from the preceding task, debug the full item to see for yourself)

- hosts: all
  
  tasks:
    - name: Find .yml files
      find:
        paths: /temp/
        patterns: '*.yml,*.yaml'
        recurse: yes
      register: yaml_path_files

    - name: Slurp file contents from remote
      slurp:
        src: "{{ item.path }}"
      loop: "{{ yaml_path_files.files }}"
      register: yaml_files_contents
   
    - name: Show path for files containing ConfigMaps
      debug:
        var: item.item.path
      loop: "{{ yaml_files_contents.results }}"
      when: item.content | b64decode | from_yaml_all | list | selectattr('kind', '==', 'ConfigMap') | list | length > 0

Upvotes: 1

Related Questions