Mapping a list of Debian packages in a specific order of 2nd list with Ansible

吃可爱长大的小学妹 提交于 2020-07-09 19:56:07

问题


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

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"
    ]

Mapping task

- 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"

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?

Goal:

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
    ]

The resulting list should be:

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

Working task: (Solved)

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


回答1:


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



回答2:


The tasks below

- set_fact:
    my_pkg: "{{ my_pkg|default([]) +
                ms2Num.stdout_lines|
                select('search', item)|
                list }}"
  loop: "{{ MS2Packages }}"
- debug:
    var: my_pkg

give

"my_pkg": [
    "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"
]


来源:https://stackoverflow.com/questions/57587809/mapping-a-list-of-debian-packages-in-a-specific-order-of-2nd-list-with-ansible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!