Automation Ansible:-在目录中列出子目录&;使用每个目录名作为变量。

Automation Ansible:-在目录中列出子目录&;使用每个目录名作为变量。,automation,ansible,devops,Automation,Ansible,Devops,我想列出父目录中的所有子目录,然后在ansible中使用每个目录名作为单独的变量,以便进一步操作。请记住,通常不可能使用文件名作为ansible变量。文件名可以包含在变量名中非法的字符 还要记住,文件可以与已定义的变量同名。最好为变量添加前缀,以避免冲突 以下示例筛选有效的文件名并在变量名前加前缀 --- - hosts: test1 tasks: - find: path: /etc file_type: file use_reg

我想列出父目录中的所有子目录,然后在ansible中使用每个目录名作为单独的变量,以便进一步操作。

请记住,通常不可能使用文件名作为ansible变量。文件名可以包含在变量名中非法的字符

还要记住,文件可以与已定义的变量同名。最好为变量添加前缀,以避免冲突

以下示例筛选有效的文件名并在变量名前加前缀

---
- hosts: test1

  tasks:

    - find:
        path: /etc
        file_type: file
        use_regex: yes
        patterns: "^[a-zA-Z0-9_]+$"
        recurse: no
      register: files

    - set_fact:
        "my_{{ item | basename }}": "{{ item }}"
      with_items: "{{ files.files | map(attribute='path') | list }}"

    - debug: var=my_passwd
    - debug: var=my_hosts
“我想……”这不是怎么问问题的!