zoukankan      html  css  js  c++  java
  • ansible find模块

    ​ 根据条件查找文件,返回文件列表。文档中已经有很详细的解释,可以用ansible-doc find查看文档。下面对常用的参数作一些解释,和主要详解后面的案例(找出文件,并对找出的文件做一些操作)。

    find模块参数

    • paths:必选参数,指定查找的目录,可以指定多个路径,路径之间用逗号隔开。别名:namepath(即用name、path和paths相同)
    • recurse:递归。默认情况下只会在指定的目录中查找,也就是说如果指定目录中还有目录,ansible不递归查找;如果要递归查找,recurse设置为yes
    • hidden:默认不查找隐藏文件,如果要查找隐藏文件,hidden设置为yes
    • file_type:文件类型,默认只查找文件(file)。文件类型可以指定为:anydirectoryfilelink
    • contains:根据正则表达式匹配文件的内容,只有file_type=file的时候才有效
    • patterns:指定需要查找的文件名称,支持shell通配符和正则表达式,默认使用shell通配符,如果要用正则表达式,use_regex参数需设置为yes
    • excludes:不包括。返回列表中不包括这些值
    • use_regex:是否用正则表达式,默认no,表示containspatternsexcludes使用shell通配符;如果指定为yes,则containspatternsexcludes使用正则表达式

    案例

    查找符合条件的目录,然后遍历目录,查找符合条件的文件。

    执行逻辑

    1. 目录下包括foo开头和test开头的目录,首先查找test开头的目录,把结果存储在register: find_dirctory中。
    2. 遍历test开头的目录(用with_list遍历),在目录下查找文件名.py结尾,文件内容包含qwertyuiop的文件。
    3. 打印结果。符合条件的结果有bar1_1.pybar1_2.pybar2_1.py

    查找目录结构

    $ tree ~/test/
    
    ├── foo1
    │   ├── bar1
    │   └── bar2
    ├── index.html
    ├── test1
    │   ├── bar1_1.py
    │   ├── bar1_2.py
    │   ├── file1_1
    │   ├── foo1_1.txt
    │   ├── foo1_2.txt
    │   └── foo1_3.txt
    └── test2
        ├── bar2_1.py
        ├── bar2_2.py
        ├── bar2_3.py
        └── file2_1
    
    8 directories, 11 files
    

    ansible-play脚本结构

    $ tree
    .
    ├── inventories
    │   └── localhost
    ├── main.yml
    └── roles
        └── find
            └── tasks
                └── main.yml
    
    4 directories, 3 files
    

    inventories/localhost

    [servers]
    127.0.0.1
    
    [servers:vars]
    # 远程用户名
    become_user = user_name
    # 查找目录
    directory_path = "~/test"
    
    

    main.yml

    ---
    
    - name: find module test
      hosts: servers
      become_method: sudo
      become: yes 
      become_user: "{{ become_user }}"
      gather_facts: no
      roles:
        - find
    

    roles/find/tasks/main.yml

    ---
    
    - name: find directory
      find:
        paths: "{{ directory_path }}"
        file_type: "directory"
        recurse: no
        patterns: "test*"
      register: find_dirctory
    
    - name: find file
      find:
        paths: "{{ item.path }}"
        file_type: "file"
        recurse: yes
        patterns: "*.py"
        contains: ".*qwertyuiop.*"
      with_list: "{{ find_dirctory.files }}"
      register: find_file
    
    - name: print find file
      debug:
        msg:
          "
          {% for i in item.files %}
            {{ i.path }}
          {% endfor %}
          "
      with_list: "{{ find_file.results }}"
    

    运行

    $ ansible-playbook -i inventories/localhost main.yml
    

    运行结果

    "msg": "  /home/user_name/test/test2/bar2_1.py  "
    
    "msg": "  /home/user_name/test/test1/bar1_2.py  /home/user_name/test/test1/bar1_1.py  "
    

    可以找到bar1_1.pybar1_2.pybar2_1.py三个符合条件的文件。

  • 相关阅读:
    可视化开发_AppInventor2似乎被抛弃了
    PHP内核学习(一)SAPI
    代码整洁之道(一)理论篇
    Silence.js高效开发移动Web前端类库
    梦游前端,JavaScript兼容性
    20分钟入门正则表达式
    原生Javascript 省市区下拉列表插件
    Tortoise-SVN 出现“unable to connect to a repository at url no element found”解决办法
    PHP实现好友生日邮件提醒
    第一份工作
  • 原文地址:https://www.cnblogs.com/shengmading/p/14524861.html
Copyright © 2011-2022 走看看