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

查看:103
本文介绍了使用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.

推荐答案

在订单列表"(MS2Packages)循环中,对包名称列表(ProviderDebList)应用regex_search过滤器的想法是实际上是一个好人.您错过的是,必须将regex_search过滤器与 (将过滤器应用于列表的每个项目).

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).

这是可行的解决方案:

- 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天全站免登陆