【问题标题】:Is there with_fileglob that works remotely in ansible?是否有 with_fileglob 可以在 ansible 中远程工作?
【发布时间】:2020-08-15 03:48:00
【问题描述】:

是否有with_fileglob 在 ansible 中远程工作?

主要是我确实想使用与with_fileglob 类似的东西,但这会在远程/目标机器上使用文件,而不是在运行 ansible 的机器上。

【问题讨论】:

    标签: ansible ansible-playbook


    【解决方案1】:

    使用find module 过滤文件,然后处理结果列表:

    - name: Get files on remote machine
      find:
        paths: /path/on/remote
      register: my_find
    
    - debug:
        var: item.path
      with_items: "{{ my_find.files }}"
    

    【讨论】:

    • 也适用于win_find。感谢with_items: "{{ my_find.files }}"部分,不是很明显,我一开始尝试使用with_items: my_find.files
    【解决方案2】:

    不幸的是,所有with_* 循环机制都是本地查找,因此在 Ansible 中没有真正干净的方法可以做到这一点。设计的远程操作必须包含在任务中,因为它需要处理连接和库存等。

    您可以做的是通过向主机发送shell然后注册输出并循环输出的stdout_lines部分来生成您的fileglob。

    所以一个简单的例子可能是这样的:

    - name    : get files in /path/
      shell   : ls /path/*
      register: path_files
    
    - name: fetch these back to the local Ansible host for backup purposes
      fetch:
        src : /path/"{{item}}"
        dest: /path/to/backups/
      with_items: "{{ path_files.stdout_lines }}"
    

    这将连接到远程主机(例如,host.example.com),获取/path/ 下的所有文件名,然后将它们复制回 Ansible 主机的路径:/path/host.example.com/

    【讨论】:

    • 根据您的经验,当您执行此操作时,Windows 和 Linux 之间的行尾是否存在问题?例如。一些with_items 会从dir /b 复制标准输出为空白吗?
    【解决方案3】:

    使用ls /path/* 对我不起作用,所以这里有一个使用find 和一些简单的正则表达式来删除所有 nginx 托管虚拟主机的示例:

    - name: get all managed vhosts
      shell: find /etc/nginx/sites-enabled/ -type f -name \*-managed.conf
      register: nginx_managed_virtual_hosts
    
    - name: delete all managed nginx virtual hosts
      file:
        path: "{{ item }}"
        state: absent
      with_items: "{{ nginx_managed_virtual_hosts.stdout_lines }}"
    

    您可以使用它来查找具有特定扩展名或任何其他组合的所有文件。例如,简单地获取目录中的所有文件:find /etc/nginx/sites-enabled/ -type f

    【讨论】:

      【解决方案4】:

      这是一种方法,您可以循环遍历所有找到的内容。在我的示例中,我必须查找所有 pip 实例以清除 awscli 以准备安装 awscli v2.0。我对 lineinfile 做了类似的操作,以去除 /etc/skel dotfiles 中的变量

      - name: search for pip
        find:
          paths: [ /usr/local/bin, /usr/bin ]
          file_type: any
          pattern: pip*
        register: foundpip
      
      - name: Parse out pip paths (say that 3 times fast)
        set_fact:
          pips: "{{ foundpip | json_query('files[*].path') }}"
      
      - name: List all the found versions of pip
        debug:
          msg: "{{ pips }}"
      
      #upgrading pip often leaves broken symlinks or older wrappers behind which doesn't affect pip but breaks playbooks so ignore!
      - name: remove awscli with found versions of pip
        pip:
          name: awscli
          state: absent
          executable: "{{ item }}"
        loop: "{{ pips }}"
        ignore_errors: yes
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-30
        • 1970-01-01
        • 1970-01-01
        • 2020-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多