提取没有扩展名的文件名 - Ansible [英] Extract file names without extension - Ansible

查看:42
本文介绍了提取没有扩展名的文件名 - Ansible的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ansible 中有一个 variable 文件,如下所示

I have a variable file in ansible like below

check:
       - file1.tar.gz
       - file2.tar.gz

tasks 中迭代它时,我正在使用 {{item}}

while iterating it in tasks i am using {{item}}

with_items:- "{{check}}",

有没有办法在迭代时提取没有扩展名的文件名?即我需要 file1.tar.gz 中的 file1file2.tar.gz

Is there a way to extract the filenames without extension while iterating? i.e i need file1 from file1.tar.gz and file2 from file2.tar.gz

推荐答案

Ansible has as splitext filter 但不幸的是它只在最后一个点之后拆分扩展名.

Ansible has as splitext filter but unfortunately it only splits the extension after the last dot.

可靠地实现 IMO 要求的唯一解决方案是使用 split() python 方法 可用于 string 对象或 regex_replace 过滤器

The only solution to reliably achieve your requirement IMO is to extract the characters before the first dot using either the split() python method available on string objects or the regex_replace filter

正则表达式解决方案对于您当前的需求来说有点过头了.同时它非常灵活,因为您可以轻松地使其适应更复杂的情况(匹配名称中的语义版本,寻找特定模式).此外,由于它是一个过滤器(相对于 .split() 的 Python 本机方法),您可以使用它:

The regexp solution is a bit of an overkill for your current requirement. Meanwhile it is very flexible as you can easily adapt it to more complex situations (matching a semantic version in the name, look for a particular pattern). Moreover, since it is a filter (vs a python native method for .split()), you can either use it:

以下是以下剧本中每个解决方案的示例:

Here is an example for each solution in the below playbook:

---
- name: Extract file name without extension(s)
  hosts: localhost
  gather_facts: false

  vars:
    check:
      - file1.tar
      - file2.tar.gz
      - file3.tar.bz2.back
      - a_weird_file.name.with.too.many.dots

    file_regex: >-
      ^([^\.]*).*

  tasks:
    - name: use the split() function
      debug:
        msg: >-
          {{ item.split('.') | first }}
      loop: "{{ check }}"

    - name: Apply regex filter while looping
      debug:
        msg: >-
          {{ item | regex_replace(file_regex, '\1') }}
      loop: "{{ check }}"

    - name: Apply regex filter on list before loop
      debug:
        var: item
      loop: >-
        {{ check | map('regex_replace', file_regex, '\1') | list }}

这是结果.

注意:就我自己的知识而言,我使用了 profile_task 运行剧本时的回调插件.你会看到在这个小文件列表中,每种方法都有相同的性能

Note: for my own knowledge, I used the profile_task callback plugin when running the playbook. You will see that on this small list of files, each method has an equivalent performance

$ ansible-playbook playbook.yml 

PLAY [Extract file name without extension(s)] *************************************************

TASK [use the split() function] ************************************************************
Friday 02 April 2021  18:53:14 +0200 (0:00:00.019)       0:00:00.019 ********** 
ok: [localhost] => (item=file1.tar) => {
    "msg": "file1"
}
ok: [localhost] => (item=file2.tar.gz) => {
    "msg": "file2"
}
ok: [localhost] => (item=file3.tar.bz2.back) => {
    "msg": "file3"
}
ok: [localhost] => (item=a_weird_file.name.with.too.many.dots) => {
    "msg": "a_weird_file"
}

TASK [Apply regex filter while looping] ****************************************************
Friday 02 April 2021  18:53:14 +0200 (0:00:00.056)       0:00:00.075 ********** 
ok: [localhost] => (item=file1.tar) => {
    "msg": "file1"
}
ok: [localhost] => (item=file2.tar.gz) => {
    "msg": "file2"
}
ok: [localhost] => (item=file3.tar.bz2.back) => {
    "msg": "file3"
}
ok: [localhost] => (item=a_weird_file.name.with.too.many.dots) => {
    "msg": "a_weird_file"
}

TASK [Apply regex filter on list before loop] **********************************************
Friday 02 April 2021  18:53:14 +0200 (0:00:00.056)       0:00:00.132 ********** 
ok: [localhost] => (item=file1) => {
    "ansible_loop_var": "item",
    "item": "file1"
}
ok: [localhost] => (item=file2) => {
    "ansible_loop_var": "item",
    "item": "file2"
}
ok: [localhost] => (item=file3) => {
    "ansible_loop_var": "item",
    "item": "file3"
}
ok: [localhost] => (item=a_weird_file) => {
    "ansible_loop_var": "item",
    "item": "a_weird_file"
}

PLAY RECAP *********************************************************************************
localhost                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Friday 02 April 2021  18:53:14 +0200 (0:00:00.057)       0:00:00.189 ********** 
=============================================================================== 
Apply regex filter on list before loop --------------------------------- 0.06s
Apply regex filter while looping --------------------------------------- 0.06s
use the split() function ----------------------------------------------- 0.06s 

这篇关于提取没有扩展名的文件名 - Ansible的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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