Ansible regex_findall 多个字符串 [英] Ansible regex_findall multiple strings

查看:45
本文介绍了Ansible regex_findall 多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cisco IOS 路由器,执行dir",我想获取名称中带有.bin"的所有文件名.

Cisco IOS routers, doing a "dir", and I want to grab all file names with ".bin" in the name.

示例字符串:flash0目录://p>

Example string: Directory of flash0:/

1  -rw-    95890300  May 24 2015 11:27:22 +00:00  c2900-universalk9-mz.SPA.153-3.M5.bin
2  -rw-    68569216   Feb 8 2019 20:15:26 +00:00  c3900e-universalk9-mz.SPA.151-4.M10.bin
3  -rw-       46880  Oct 25 2017 19:08:56 +00:00  pdcamadeusrtra-cfg
4  -rw-         600   Feb 1 2019 19:36:44 +00:00  vlan.dat

总共 260153344 字节(95637504 字节空闲)

260153344 bytes total (95637504 bytes free)

我已经知道如何提取bin",但我不知道如何提取整个文件名(以c"开头,以bin"结尾),因为我想然后使用这些值并删除不需要的文件.

I've figured out how to pull "bin", but I can't figure out how to pull the whole filename (starting with " c", ending in "bin"), because I want to then use the values and delete unwanted files.

我是编程新手,所以正则表达式示例有点令人困惑.

I'm new to programming, so the regex examples are a little confusing.

推荐答案

感谢 Code Maniac!您的代码找到了一个实例,而我需要找到所有实例.使用你给我的东西加上其他一些例子,我发现这个工作:

Thank you Code Maniac! Your code finds one instance, and I needed to find all. Using what you gave me plus messing around with some other examples, I found this to work:

binfiles="{{ dir_response.stdout[0] | regex_findall('\b(?=(c.*.bin))\b') }}"

binfiles="{{ dir_response.stdout[0] | regex_findall('\b(?=(c.*.bin))\b') }}"

现在我明白了:任务 [set_fact] **************************************************************************************************************任务路径:/export/home/e130885/playbooks/ios-switch-upgrade/ios_clean_flash.yml:162019 年 2 月 12 日星期二 08:29:58 -0600 (0:00:00.350) 0:00:03.028 ******好的:[10.35.91.200] => 改变=假ansible_facts:二进制文件:- c2900-universalk9-mz.SPA.153-3.M5.bin- c3900e-universalk9-mz.SPA.151-4.M10.bin- c2800nm-adventerprisek9-mz.151-4.M12a.bin

Now I get this: TASK [set_fact] ******************************************************************************************************** task path: /export/home/e130885/playbooks/ios-switch-upgrade/ios_clean_flash.yml:16 Tuesday 12 February 2019 08:29:58 -0600 (0:00:00.350) 0:00:03.028 ****** ok: [10.35.91.200] => changed=false ansible_facts: binfiles: - c2900-universalk9-mz.SPA.153-3.M5.bin - c3900e-universalk9-mz.SPA.151-4.M10.bin - c2800nm-adventerprisek9-mz.151-4.M12a.bin

接下来的任务是弄清楚如何使用每个元素.谢谢!

Onto the next task of figuring out how to use each element. Thank you!

这篇关于Ansible regex_findall 多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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