【问题标题】:How to make Ansible with_fileglob include hidden files?如何使 Ansible with_fileglob 包含隐藏文件?
【发布时间】:2017-09-05 12:55:38
【问题描述】:

我在我的 Ansible 脚本中使用以下任务将所有文件从本地数据文件夹复制到服务器:

- name: copy basic files to folder
  copy:
    src: "{{ item }}"
    dest: ~/data/
    mode: 755
    owner: "www-data"
    group: "www-data"
  with_fileglob:
    - ../files/data/*

这很好用,除了它跳过隐藏文件(例如.htaccess)。

有人知道我如何让with_fileglob 也包含隐藏文件吗?

【问题讨论】:

    标签: ansible hidden-files file-globs


    【解决方案1】:

    好的,我自己找到了答案。我发现with_fileglob 只是调用了python 的glob.glob() 函数。所以经过一番折腾后,我发现只需要添加一个带有.* 的文件团:

    - name: copy basic files to folder
      copy:
        src: "{{ item }}"
        dest: ~/data/
        mode: 755
        owner: "www-data"
        group: "www-data"
      with_fileglob:
        - ../files/data/*
        - ../files/data/.*
    

    【讨论】:

    • 我快了 18 秒 ;)
    • 你是对的!你得到了学分!谢谢,祝你有个美好的一天!
    【解决方案2】:

    Ansible uses Python 的glob

    如果目录包含以. 开头的文件,则默认不匹配。

    >>> import glob
    >>> glob.glob('*.gif')
    ['card.gif']
    >>> glob.glob('.c*')
    ['.card.gif']
    

    .*明确添加到模式列表中。

    【讨论】:

    • 我自己也找到了答案。但我会接受你的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    相关资源
    最近更新 更多