找到包含目录中特定内容的文件,然后仅使用该文件运行命令

问题描述 投票:0回答:2
- name: find config files
  find:
    paths: "/directory/files/"
    patterns: "*.yaml,*.yml"
  register: yaml_files

- set_fact:
    yaml_list: "{{ yaml_files.files | map(attribute='path') | list}}"


- name: Create namespace first
  command: "{{ bin_dir }}/kubectl apply -f {{ item }}"
  when: contents.find('kind\:\ Namespace')
  vars:
    contents: "{{ lookup('file', '{{ item }}') }}"
  with_items:
    "{{ yaml_list }}"

我只想在文件包含“kind:Namespace”时运行命令,但这将在找到所有文件的情况下运行。

kubernetes ansible
2个回答
0
投票

试试search

- name: Create namespace first
  command: "{{ bin_dir }}/kubectl apply -f {{ item }}"
  loop: "{{ yaml_list }}"
  when: lookup('file', item) is search('kind\:\ Namespace')

(未测试)


0
投票
  - name: find config files
    command: 'find /directory/files/ -type f \( -iname \*.yml -o -iname \*.yaml \)'
    register: yaml_files

  - name: Create namespace first
    command: 'kubectl apply -f {{ item }}'
    with_items:
      - "{{ yaml_files.stdout_lines }}"
    when: lookup('file', item) is search('kind\:\ Namespace')

它应该适合你

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.