无法从 Ansible 变量中搜索子字符串
Unable to search sub-string from Ansible variable
如果变量包含字符串 7.0.0.GA
,我将尝试在 Ansible 中创建一个目录
下面是我的剧本:
- name: Determine the version of Tom
raw: "cat {{ homefound.path | dirname }}/version.txt | grep -i version"
register: Tomver
- debug:
msg: "Tom VERSION IS: {{ Tomver.stdout }}"
- name: Create patch folder /app/Tom_Patches/7.0 on target servers
file:
path: /app/Tom_Patches/7.0
state: directory
mode: '0755'
when: Tomver.stdout is match('7.0.0.GA*')
在输出中我可以看到变量有字符串 7.0.0.GA
但仍然 when condition
失败并跳过。
输出:
TASK [debug] ***********************************************************************************************************************************************************
ok: [10.9.156.126] => {
"msg": "Tom VERSION IS: Red Hat Tom Enterprise Application Platform - Version 7.0.0.GA\r\n"
}
TASK [Create patch folder /app/Tom_Patches/7.0 on target servers] ****************************************************************************************************
skipping: [10.9.156.126]
我什至尝试了以下条件检查,但也失败了:
when: Tomver.stdout | join('') | search('7.0.0.GA')
我更喜欢使用 raw
模块而不是 command
或 shell
模块以避免 python 依赖。
有人可以建议吗?
你可以试试in
when: "'7.0.0.GA' in res.stdout"
如果变量包含字符串 7.0.0.GA
下面是我的剧本:
- name: Determine the version of Tom
raw: "cat {{ homefound.path | dirname }}/version.txt | grep -i version"
register: Tomver
- debug:
msg: "Tom VERSION IS: {{ Tomver.stdout }}"
- name: Create patch folder /app/Tom_Patches/7.0 on target servers
file:
path: /app/Tom_Patches/7.0
state: directory
mode: '0755'
when: Tomver.stdout is match('7.0.0.GA*')
在输出中我可以看到变量有字符串 7.0.0.GA
但仍然 when condition
失败并跳过。
输出:
TASK [debug] ***********************************************************************************************************************************************************
ok: [10.9.156.126] => {
"msg": "Tom VERSION IS: Red Hat Tom Enterprise Application Platform - Version 7.0.0.GA\r\n"
}
TASK [Create patch folder /app/Tom_Patches/7.0 on target servers] ****************************************************************************************************
skipping: [10.9.156.126]
我什至尝试了以下条件检查,但也失败了:
when: Tomver.stdout | join('') | search('7.0.0.GA')
我更喜欢使用 raw
模块而不是 command
或 shell
模块以避免 python 依赖。
有人可以建议吗?
你可以试试in
when: "'7.0.0.GA' in res.stdout"