如何通过 ansible 执行带有替代方案的替代方案循环(文件夹列表)

如何通过 ansible 执行带有替代方案的替代方案循环(文件夹列表)

请求 Ansible 专家帮忙解决。Ansible 是否可行

  1. Jdk下载并解压至/apps/tools/jdk_1.8.121

  2. 二进制文件 -/apps/tools/jdk_1.8.121/bin

需要

每个二进制文件/应用程序/工具/jdk_1.8.121/bin/java、java、javadoc、

...... 等约 40 项,按循环进入替代程序

alternatives --install /usr/bin/java java /apps/tools/jdk_1.8.121/bin/java 1000

alternatives --install /usr/bin/{{ var }} {{var}} /apps/tools/jdk_1.8.121/bin/{{var}} 1000

答案1

这很简单,你只需要记录结果find模块并循环遍历with_items

- hosts: hosts
  tasks:
  - name: Find binaries
    find:
      paths: /apps/tools/jdk_1.8.121/bin
    register: bins
  - name: Install alternatives
    command: "update-alternatives --install /usr/bin/{{ item.path|basename }} {{ item.path|basename }} {{ item.path }} 1000"
    with_items: "{{ bins.files }}"

或者,使用alternatives模块:

  - name: Install alternatives
    alternatives:
      name: "{{ item.path|basename }}"
      link: "/usr/bin/{{ item.path|basename }}"
      path: "{{ item.path }}"
      priority: 1000
    with_items: "{{ bins.files }}"

使用 Ansible 2.5 或更新版本,建议使用loop代替with_items

  - name: Install alternatives
    alternatives:
      name: "{{ item.path|basename }}"
      link: "/usr/bin/{{ item.path|basename }}"
      path: "{{ item.path }}"
      priority: 1000
    loop: "{{ bins.files|flatten(levels=1) }}"

相关内容