Ansible 如何使用regex_replace有条件地替换列表中的文本?
我的剧本中有一个变量,它来自于一个列表。在某些情况下,此变量包含一个“-”以分隔两个值。比如说,Ansible 如何使用regex_replace有条件地替换列表中的文本?,ansible,yaml,jinja2,ansible-inventory,Ansible,Yaml,Jinja2,Ansible Inventory,我的剧本中有一个变量,它来自于一个列表。在某些情况下,此变量包含一个“-”以分隔两个值。比如说, Numbers: - 2211 - 2211-2212 在这种情况下,我希望根据一个条件将“-”替换为“\”:如果数字长度为4个字符,请执行此操作。否则,请将“-”替换为“\u1”,然后执行此操作 我已经试着在我的剧本中摆弄jinja2和regex,但到目前为止没有运气。这是我试过的 number: {% if length(item) == 4 %} {{ item | regex_re
Numbers:
- 2211
- 2211-2212
在这种情况下,我希望根据一个条件将“-”替换为“\”:如果数字长度为4个字符,请执行此操作。否则,请将“-”替换为“\u1”,然后执行此操作
我已经试着在我的剧本中摆弄jinja2和regex,但到目前为止没有运气。这是我试过的
number: {% if length(item) == 4 %} {{ item | regex_replace("^(.*)$", "Number_\1") | string }} {% else %} {{ item | regex_replace("^(.*)$", "Number_\1") |replace("-", "_") | string }}
我想要的结果
Number is four characters long:
number: Number_2211
Number is more then 4 characters long:
number: Number_2211_2212
我收到的一些错误消息是
ERROR! Syntax Error while loading YAML.
did not find expected key
在Ansible剧本中有没有实现这一点的方法?
提前谢谢 不太清楚您是如何使用这些数据的。Ansible并不擅长就地修改复杂的数据结构,但当您访问数据时,它有很多转换数据的方法。例如,这个剧本:
---
- hosts: localhost
gather_facts: false
vars:
numbers:
- "2211"
- "2211-2212"
tasks:
- debug:
msg: "number: {{ item.replace('-', '_') }}"
loop: "{{ numbers }}"
将输出:
TASK [debug] **********************************************************************************
ok: [localhost] => (item=2211) => {
"msg": "number: 2211"
}
ok: [localhost] => (item=2211-2212) => {
"msg": "number: 2211_2212"
}
如果您确实需要使转换以长度为条件(不清楚您是否需要),可以执行以下操作:
- debug:
msg: "{{ item.replace('-', '_') if item|length > 4 else item }}"
loop: "{{ numbers }}"
更新
我看你选择了另一个答案。这里给出的解决方案似乎更简单(没有“过滤器、正则表达式和等式检查的不可理解序列”),并产生几乎相同的输出:
TASK [debug] **********************************************************************************
ok: [localhost] => (item=445533) => {
"msg": "445533"
}
ok: [localhost] => (item=112234-538) => {
"msg": "112234_538"
}
ok: [localhost] => (item=11) => {
"msg": "11"
}
ok: [localhost] => (item=1111) => {
"msg": "1111"
}
ok: [localhost] => (item=1111-1111) => {
"msg": "1111_1111"
}
ok: [localhost] => (item=11-11) => {
"msg": "11_11"
}
考虑到
11-11
,不清楚您希望11\u 11
还是11-11
作为输出。如果你认为是前者,那么这个答案就更正确。你可以使用一系列令人费解的过滤器、正则表达式和相等性检查来完成这项工作
#!/usr/bin/env ansible-playbook
- name: Lets munge some data
hosts: localhost
gather_facts: false
become: false
vars:
array:
- 445533
- 112234-538
- 11
- 1111
- 1111-1111
- 11-11
tasks:
- name: Replace hypens when starting with 4 numbers
debug:
msg: "{{ ((item | string)[0:4] | regex_search('[0-9]{4}') | string != 'None')
| ternary((item | regex_replace('-', '_')), item) }}"
loop: "{{ array }}"
PLAY [Lets munge some data] *****************************************************************************************************************************************************************************************************
TASK [Replace hypens when starting with 4 numbers] ******************************************************************************************************************************************************************************
ok: [localhost] => (item=445533) => {
"msg": "445533"
}
ok: [localhost] => (item=112234-538) => {
"msg": "112234_538"
}
ok: [localhost] => (item=11) => {
"msg": "11"
}
ok: [localhost] => (item=1111) => {
"msg": "1111"
}
ok: [localhost] => (item=1111-1111) => {
"msg": "1111_1111"
}
ok: [localhost] => (item=11-11) => {
"msg": "11-11"
}
PLAY RECAP **********************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
谢谢你的回答,这帮我搞定了!
#!/usr/bin/env ansible-playbook
- name: Lets munge some data
hosts: localhost
gather_facts: false
become: false
vars:
array:
- 445533
- 112234-538
- 11
- 1111
- 1111-1111
- 11-11
tasks:
- name: Replace hypens when starting with 4 numbers
debug:
msg: "{{ ((item | string)[0:4] | regex_search('[0-9]{4}') | string != 'None')
| ternary((item | regex_replace('-', '_')), item) }}"
loop: "{{ array }}"
PLAY [Lets munge some data] *****************************************************************************************************************************************************************************************************
TASK [Replace hypens when starting with 4 numbers] ******************************************************************************************************************************************************************************
ok: [localhost] => (item=445533) => {
"msg": "445533"
}
ok: [localhost] => (item=112234-538) => {
"msg": "112234_538"
}
ok: [localhost] => (item=11) => {
"msg": "11"
}
ok: [localhost] => (item=1111) => {
"msg": "1111"
}
ok: [localhost] => (item=1111-1111) => {
"msg": "1111_1111"
}
ok: [localhost] => (item=11-11) => {
"msg": "11-11"
}
PLAY RECAP **********************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0