Ansible regex_findall多个字符串 [英] Ansible regex_findall multiple strings
问题描述
Cisco IOS路由器,做一个目录",我想获取名称中带有".bin"的所有文件名.
Cisco IOS routers, doing a "dir", and I want to grab all file names with ".bin" in the name.
示例字符串:flash0的目录:/
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.
我是编程的新手,因此regex示例有些混乱.
I'm new to programming, so the regex examples are a little confusing.
推荐答案
感谢代码疯子!您的代码找到一个实例,而我需要找到所有实例.使用您给我的内容以及其他示例,我发现它可以正常工作:
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] =>更改= falseansible_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屋!