Ansible:findall 正则表达式中变量的正确语法是什么 [英] Ansible: What is the correct syntax for a variable within a findall regex
问题描述
我使用的是 Ansible 2.9 版.我想做一个 GET,它返回一个信息块,正则表达式来自该信息的 ID,该 ID 对应于我目前正在迭代的任何主机,然后再使用该 ID 执行操作.
I am using Ansible version 2.9. I want to do a GET, which returns a block of info, regex an ID from that info that corresponds to whatever host I am iterating through at the moment, and then later do things with that ID.
I have the regex working ( https://regex101.com/r/UL7V6r/2 ). And I confirmed this regex does work in the playbook when I input it exactly like in the sample. But, I need the host name at the end of that regex to be a variable matching the host I am iterating through, not a static host name. Here is the playbook, focused on just getting the regex to work:
gather_facts: no
vars:
the_name: "{{ inventory_hostname_short }}"
tasks:
- uri:
url: http://controller-sample-a1p.someplace.com/restapi/nodes
method: GET
return_content: yes
register: nodeId
delegate_to: 127.0.0.1
- debug: msg="{{ nodeId.content | regex_findall("(\w+-\w+-\w+-\w+-\w+)..<address>\d+\.\d+\.\d+\.\d+:?\d+?<\/address><name>{{ the_name }}") }}"
delegate_to: 127.0.0.1
- debug:
var: the_name
焦点在该正则表达式的末尾,变量为the_name".正如你所看到的,我在这里尝试使用大括号.我已经尝试了很多不带括号、单引号、双引号等的变体.
The focus is on the end of that regex, with the variable of "the_name". As you can see I am trying curly brackets here. I've tried lots of variations insulting no brackets, single quotes, double quotes, etc.
这出戏的结果是:
ok: [sampleFQDN2.place.com -> 127.0.0.1] => {
"msg": []
}
TASK [debug] *************************************************************************************************************************
ok: [sampleFQDN2.place.com] => {
"the_name": "sampleFQDN2"
}
预期结果是调试消息一显示FJLEEJ24-3190-49F1-965D-823F63904136"(根据链接).
Expected result would be for debug msg one to show "FJLEEJ24-3190-49F1-965D-823F63904136" (per the link).
所以这向我表明该变量是正确的(第二次调试).并且正则表达式是正确的,因为如果我将sampleFQDN2"的变量输出并直接插入,第一个调试会输出正确的响应.这让我相信我只是没有找到将变量粘贴到 regex_findall 中的正确语法.
So this suggests to me that the variable is correct (second debug). And the regex is correct because if I take the variable output of "sampleFQDN2" and plug it in directly, that first debug spits out the right response. Which leads me to believe I just have not found the right syntax to stick a variable in a regex_findall.
有人知道这个的语法吗?或者即使有可能?
Does anyone know the syntax for this? Or even if it is possible?
推荐答案
你永远不会嵌套 Jinja {{...}}
模板标记.在 Jinja 模板中,如果你想组合一个文本字符串和一个变量,你可以使用 ~
字符串连接运算符,如下所示:
You never nest Jinja {{...}}
template markers. Within a Jinja template, if you want to combine a literal string and a variable, you can use the ~
string concatenation operator, like this:
- debug: msg="{{ nodeId.content | regex_findall("(\w+-\w+-\w+-\w+-\w+)..<address>\d+\.\d+\.\d+\.\d+:?\d+?<\/address><name>" ~ the_name) }}"
~
操作符类似于 +
,除了它将参数转换为字符串,以便您可以将数值连接到字符串(编写 "this " + 1
会失败,但是 "this " ~ 1
会起作用).
The ~
operator is like +
, except it will convert arguments into
strings so that you can concatenate numeric values to a string
(writing "this " + 1
would fail, but "this " ~ 1
would work).
或者您可以使用 Python 字符串格式化语法,如下所示:
Or you can use Python string formatting syntax, like this:
- debug: msg="{{ nodeId.content | regex_findall("(\w+-\w+-\w+-\w+-\w+)..<address>\d+\.\d+\.\d+\.\d+:?\d+?<\/address><name>%s" % (the_name)) }}"
此语法使用 printf
样式的格式化标记(%s
用于字符串,%d
用于整数等).或者像这样:
This syntax uses printf
-style formatting tokens (%s
for strings,
%d
for integers, etc). Or like this:
- debug: msg="{{ nodeId.content | regex_findall("(\w+-\w+-\w+-\w+-\w+)..<address>\d+\.\d+\.\d+\.\d+:?\d+?<\/address><name>{}".format(the_name)) }}"
这里是一个记录 Python 的站点字符串格式的一些细节.
Here is one site that documents Python string formatting in some detail.
这篇关于Ansible:findall 正则表达式中变量的正确语法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!