使用 Ansible 按第二个列表的特定顺序映射 Debian 软件包列表 [英] Mapping a list of Debian packages in a specific order of 2nd list with Ansible

查看:19
本文介绍了使用 Ansible 按第二个列表的特定顺序映射 Debian 软件包列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件列表映射到文件名列表.目标是按特定顺序安装 Debian 文件(基于名称列表).我可以使用 shell 命令检索文件列表并将它们注册到列表中.目标是按照我预定义的名称列表的顺序生成文件名列表.然后按顺序安装它们.

I'm trying to map a list of files to a list of filenames. The goal is to install Debian files in specific order (base off the list of names). I can retrieve the list of files with a shell command and register them to a list. The goal is to generate a list of filenames in the order of my predefined name list. Then install them in that order.

ms2Num.stdout_lines 是来自 shell 命令的文件列表:

ms2Num.stdout_lines is the list of files from the shell command:

# use List -1 to find the file names for the deb files.| grep
- name: Find the needed deb files
  shell: "ls -1 {{ DestDir | join }}/ms2install/ms2install/ | grep {{ ms2Num.stdout_lines[0] | join }}"
  register: ProviderDebList

此任务生成一个列表ProviderDebList.stdout_lines.这是文件列表:

This task generates a list ProviderDebList.stdout_lines. Here is the list of files:

    "stdout_lines": [
        "ms2-apache_1.6.1.8~20160324_amd64.deb", 
        "ms2-ctps_1.6.1.8~20160324_amd64.deb", 
        "ms2-desert_1.6.1.8~20160324_amd64.deb", 
        "ms2-provider_1.6.1.8~20160324_amd64.deb", 
        "ms2-w3gui_1.6.1.8+1~20160324_amd64.deb"
    ]

映射任务

- name: Display files in order from MS2-list
  debug:
    msg: "File name: {{ ms2Num.stdout_lines | regex_search( item | string ) | string }}"
  loop: "{{ MS2Packages }}"

运行我得到的映射任务:但我收到一个错误:

Running the mapping task I get: But I get an error:

fatal: [10.0.2.25]: FAILED! => {
    "msg": "Unexpected templating type error occurred on (File name: {{ ms2Num.stdout_lines | regex_search( item | string ) | string }}): expected string or buffer"

我对 Ansible 过滤器的了解相当基本,所以这些错误仍然很难解析.我知道我错过了一些东西,但是什么?

My knowledge of the Ansible's filters fairly basic so these errors are still a pain to parse. I know that I'm missing something, but what?

目标是按照 MS2Packages 的顺序生成文件名列表.我想获取我的名字列表并映射文件名以对其进行排序.

The goal is to generate a list of filenames in the order of the MS2Packages. I want to take my name list and map the filenames order it.

以下是安装顺序的列表:

Here is the list to base the installation order to:

MS2Packages:
  - ms2-desert
  - ms2-ctps
  - ms2-apache
  - ms2-w3gui
  - ms2-provider
    ]

结果列表应该是:

    "stdout_lines": [
        "ms2-desert_1.6.1.8~20160324_amd64.deb", 
        "ms2-ctps_1.6.1.8~20160324_amd64.deb", 
        "ms2-apache_1.6.1.8~20160324_amd64.deb", 
        "ms2-w3gui_1.6.1.8+1~20160324_amd64.deb"
        "ms2-provider_1.6.1.8~20160324_amd64.deb", 
    ]

一些较晚的文件使用较早的文件作为依赖项,因此我需要按特定顺序安装它们.

Some of the later files use the earlier ones as dependencies so I need to install them in a specific order.

# print the files names in order of the deb list
- name: Create the list files in order from MS2-list
  set_fact:
    OrderProviderList: "{{ OrderProviderList | default([]) + ProviderDebList.stdout_lines | map('regex_search', '.*' + order + '.*') | select('string') | list }}"
  loop: "{{ MS2Packages }}"
  loop_control:
    loop_var: order

我现在可以遍历这个列表并安装所需的包.

I can now loop through this list and install the needed packages.

推荐答案

你的想法是在你的包名列表 (ProviderDebList) 上应用 regex_search 过滤器订单列表"(MS2Packages)实际上是一个不错的选择.您错过的是您必须使用 map(将过滤器应用于列表中的每个项目).

Your idea to apply regex_search filter on your list of packages names (ProviderDebList) in a loop of "order list" (MS2Packages) is actually a good one. What you miss is that you have to apply the regex_search filter with map (which will apply the filter on each item of your list).

这里是可行的解决方案:

So here is the working solution:

- name: List sorting
  hosts: localhost
  gather_facts: no
  vars:
    MS2Packages:
      - ms2-desert
      - ms2-ctps
      - ms2-apache
      - ms2-w3gui
      - ms2-provider
    ProviderDebList:
      - ms2-apache_1.6.1.8~20160324_amd64.deb
      - ms2-ctps_1.6.1.8~20160324_amd64.deb
      - ms2-desert_1.6.1.8~20160324_amd64.deb
      - ms2-provider_1.6.1.8~20160324_amd64.deb
      - ms2-w3gui_1.6.1.8+1~20160324_amd64.deb

  tasks:
    - name: Print package in the right order
      debug:
        msg: " - {{ ProviderDebList | map('regex_search', '.*'+order+'.*') | select('string') | list }}"
      loop: "{{ MS2Packages }}"
      loop_control:
        loop_var: order

这篇关于使用 Ansible 按第二个列表的特定顺序映射 Debian 软件包列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆