Ansible 运行存档模块的所有第一级文件夹

Ansible 运行存档模块的所有第一级文件夹,ansible,Ansible,我正在使用存档模块压缩一些文件夹和存档。 我需要列出我的zip文件中的所有一级文件夹 是否有任何Jinja或python函数可以使用archive模块的注册输出来执行此操作 到目前为止,我的测试是: - name: zip folder archive: path: - /tmp/test/* dest: /tmp/zipfile.zip format: zip mode: "0755" force_archive: true reg

我正在使用
存档
模块压缩一些文件夹和存档。
我需要列出我的zip文件中的所有一级文件夹

是否有任何Jinja或python函数可以使用
archive
模块的注册输出来执行此操作

到目前为止,我的测试是:

- name: zip folder
  archive:
    path: 
      - /tmp/test/*
    dest: /tmp/zipfile.zip
    format: zip
    mode: "0755"
    force_archive: true
  register: module_result
打印
模块\u结果
,我得到:

ok: [host1] => 


msg:
    ansible_facts:
      discovered_interpreter_python: /usr/bin/python
    archived:
    - /tmp/test/cfg/1.txt
    - /tmp/test/cfg/2.txt
    - /tmp/test/cfg/3.txt
    - /tmp/test/folder1/folder2/4.txt
    - /tmp/test/folder1/folder2/5.txt
使用其他任务显示我的尝试:

- name: list folders
  debug:
    msg: "{{ item.lstrip(module_result.arcroot).split('/')[0] }}"
    # msg: "{{ item }}"
  with_items: "{{module_result.archived}}"
我试图剪切url的第一部分,在模块输出的每个项目上使用
module\u result.arcroot
,然后分割路径的其余部分,但我得到了错误的结果。最糟糕的是,我得到了太多次相同的文件夹名

结果是:

cfg
cfg
cfg
folder1
folder1
我的预期结果是:

cfg
folder1

这可能吗?

如果您的目标是在第一级文件夹上实现唯一性,那么Jinja过滤器就是您要寻找的

由于此过滤器作用于列表,因此您需要首先通过模块将结果注册为列表

然后只需使用Jinja过滤器:

- debug:
    msg: "{{ folders | unique }}"
全面工作手册:

---
- hosts: localhost
  connection: local

  vars:
    faked_module_result_archived:
      - /tmp/test/cfg/1.txt
      - /tmp/test/cfg/2.txt
      - /tmp/test/cfg/3.txt
      - /tmp/test/folder1/folder2/4.txt
      - /tmp/test/folder1/folder2/5.txt
    faked_module_result_arcroot: /tmp/test/

  tasks:
    - name: Making an list out of the files first folder
      set_fact: 
        folders: "{{ folders | default([]) + [(item.lstrip(faked_module_result_arcroot).split('/')[0])] }}"
      with_items: "{{ faked_module_result_archived }}"

    - debug:
        msg: "{{ folders | unique }}"
将输出

PLAY [localhost] ************************************************************************************************************************************************************

TASK [Gathering Facts] ******************************************************************************************************************************************************
ok: [localhost]

TASK [Making an list out of the files first folder] ************************************************************************************************************************
ok: [localhost] => (item=/tmp/test/cfg/1.txt)
ok: [localhost] => (item=/tmp/test/cfg/2.txt)
ok: [localhost] => (item=/tmp/test/cfg/3.txt)
ok: [localhost] => (item=/tmp/test/folder1/folder2/4.txt)
ok: [localhost] => (item=/tmp/test/folder1/folder2/5.txt)

TASK [debug] ****************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": [
        "cfg",
        "folder1"
    ]
}

PLAY RECAP ******************************************************************************************************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

你的例子很好!我可以得到一些定制mi脚本的提示和技巧,但是由于某些原因,当我运行改编时,我得到了一个奇怪的lstrip行为

这是我的密码:

- name: zip a folder
  archive:
    path: 
    - /tmp/test/*
    dest: /tmp/{{ path_example | basename }}-files.zip
    exclude_path:
    - /tmp/test/another_folder
    format: zip
    mode: "0755"
    force_archive: true
   register: module_result

- name: First, lstrip result
    set_fact: 
      lstrip_result: "{{ lstrip_result | default([]) [(item.lstrip(module_result.arcroot))] }}"
    with_items: "{{ module_result.archived }}"
运行“存档”任务后:

运行“第一次,lstrip结果”任务后:

这是我的问题,使用folder cfg一切正常,但使用folder test,lstrip函数比预期的多剥离2个字符,结果是:

st/folder...
而不是:

test/folder...
有人知道这个奇怪的骗局吗

编辑

lstrip文件说:

定义和用法 lstrip()方法删除所有前导字符(空格是要删除的默认前导字符)

这就是为什么我的输出是坏条带化的,因为在字符串中有一些字符

我将lstrip替换为replace

- name: First, lstrip result
    set_fact: 
      lstrip_result: "{{ lstrip_result | default([]) [(item.lstrip(module_result.arcroot))] }}"
    with_items: "{{ module_result.archived }}"

你的结果怎么会错?第一个结果=对于文件1.txt;第二个代表2.txt,第三个代表3.txt,第四个代表4.txt,第五个代表5.txt。所以结果是对的,不是吗?
st/folder...
test/folder...
- name: First, lstrip result
    set_fact: 
      lstrip_result: "{{ lstrip_result | default([]) [(item.lstrip(module_result.arcroot))] }}"
    with_items: "{{ module_result.archived }}"