我有一个变量,我需要解析它以提取版本字符串,有办法做到这一点吗?下面是 ansible 变量的示例。
--xxx 1.2.3-102 --yyy 2.5.10-47 --zzz 10.4.2-193
更新:添加 ansible 任务格式
---
- hosts: localhost
tasks:
- name: Get Version
shell: echo '{{ version }}'
register: results
- set_fact:
value: "{{ results.stdout | regex_search(regexp,'') }}"
vars:
regexp: ''
- debug:
var: value
最佳答案
仅获取“--yyy”后的版本号,根据任务需要更改正则表达式:
- hosts: localhost
tasks:
- name: Get Version
shell: echo '--xxx 1.2.3-102 --yyy 2.5.10-47 --zzz 10.4.2-193'
register: results
- name: set regex
set_fact:
re: '--yyy\s+(?P<digit>\d+\.\d+\.\d+-\d+)'
- set_fact:
value: "{{ results.stdout | regex_search(re, '\\g<digit>') }}"
- debug:
var: value[0]
关于parsing - 有没有办法 grep 或解析 ansible 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45927743/