簡體   English   中英

如何使用Ansible“吸引”多個文件

[英]How to “slurp” multiple files with Ansible

使用Ansible,我需要從多個文件中提取內容。 在一個文件中,我使用了slurp並注冊了一個變量。

- name: extract nameserver from .conf file
  slurp:
    src: /opt/test/roles/bind_testing/templates/zones/example_file.it.j2
  delegate_to: 127.0.0.1
  register: file

- debug:
   msg: "{{ file['content'] | b64decode }}"

但是現在我有多個文件,因此我需要從每個文件中提取內容,一個一個地注冊它們,以便以后使用sed,merging_list等操作對其進行處理...

我怎么能做到這一點呢?

我試圖通過with_fileglob指令使用slurp,但是我無法注冊文件...

- name: extract nameserver from .conf file
  slurp:
    src: "{{ item }}"
  with_fileglob:
    - "/opt/test/roles/bind9_domain/templates/zones/*"
  delegate_to: 127.0.0.1
  register: file

您應該只使用loop選項,該選項配置為slurp的文件列表。 檢查以下示例:

---
- hosts: local
  connection: local
  gather_facts: no
  tasks:
    - name: Find out what the remote machine's mounts are
      slurp:
        src: '{{ item }}'
      register: files
      loop:
        - ./files/example.json
        - ./files/op_content.json

    - debug:
        msg: "{{ item['content'] | b64decode }}"
      loop: '{{ files["results"] }}'

我要對每個文件進行slurping ,然后遍歷results以獲取其內容。

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM