ansible - 将字符串附加到 Ansible 中的列表
问题描述
我正在尝试将字符串附加到 ansible 的列表中,所以基本上我将构建一个有效负载以删除 F5 GTM 网络设备中的一些拓扑记录。
我能够创建一个列表,其中包括相应拓扑记录的所有输出。对于输出的每一行,我需要附加一个字符串“删除”。
- name: Lookup Topology Records
bigip_command:
user: admin
password: password
server: gtm.abc.com
commands: "list gtm topology | grep -i '{{ item }}'"
warn: no
validate_certs: no
register: topology_info
delegate_to: localhost
loop: "{{ gtm_pool }}"
- debug: var=topology_info
- name: Sanitize the Topology records of the Pool
set_fact:
clean_topology_info: "{{ clean_topology_info | default ([]) + item.stdout_lines }}"
loop: "{{ topology_info.results }}"
- debug: var=clean_topology_info
- name: Sanitized Topology Info
vars:
topology_item: "{{ item }}"
set_fact:
sanitized_topology_info: "{{ sanitized_topology_info | default ([]) + topology_item }}"
loop: "{{ clean_topology_info }}"
- name: Build payload to delete the Topology Record
set_fact:
topology_payload: "{{ topology_payload | default([]) + ['delete'] + item }}"
loop: "{{ clean_topology_info }}"
- debug: var=topology_payload
------------------------------------------------------------
Debug outputs(stdout_lines) as below :-
"gtm_pool": [
"test-poo1",
"test-pool2"
]
debug of "topology_info" :-
"stdout_lines": [
[
"gtm topology ldns: subnet 10.10.10.0/24 server: pool /Common/test-pool1 {",
"gtm topology ldns: subnet 10.8.22.0/24 server: pool /Common/test-pool1 {"
]
]
"stdout_lines": [
[
"gtm topology ldns: subnet 0.0.0.0/0 server: pool /Common/test-pool2 {"
]
debug of "clean_topology_info":-
"clean_topology_info": [
[
"gtm topology ldns: subnet 10.10.10.0/24 server: pool /Common/test-pool1 {",
"gtm topology ldns: subnet 10.8.22.0/24 server: pool /Common/test-pool1 {",
],
[
"gtm topology ldns: subnet 0.0.0.0/0 server: pool /Common/test-pool2 {",
]
]
debug of "sanitized_topology_info":-
"sanitized_topology_info": [
"gtm topology ldns: subnet 10.10.10.0/24 server: pool /Common/test-pool1 {",
"gtm topology ldns: subnet 10.8.22.0/24 server: pool /Common/test-pool1 {",
"gtm topology ldns: subnet 0.0.0.0/0 server: pool /Common/test-pool2 {"
]
debug of "topology_payload":-
"topology_payload": [
"delete",
"gtm topology ldns: subnet 10.10.10.0/24 server: pool /Common/test-pool1 {",
"gtm topology ldns: subnet 10.8.22.0/24 server: pool /Common/test-pool1 {",
"delete",
"gtm topology ldns: subnet 0.0.0.0/0 server: pool /Common/test-pool2 {"
]
Expected output of topology_payload should be like :-
Basically i need to append a string 'delete' infront of the each output.
"topology_payload": [
"delete gtm topology ldns: subnet 10.10.10.0/24 server: pool /Common/test-pool1 {",
"delete gtm topology ldns: subnet 10.8.22.0/24 server: pool /Common/test-pool1 {",
"delete gtm topology ldns: subnet 0.0.0.0/0 server: pool /Common/test-pool2 {"
]
topology_payload 的预期输出应该是这样的:-
基本上我需要在每个输出前面附加一个字符串“删除”。
“topology_payload”:[“删除 gtm 拓扑 ldns:子网 10.10.10.0/24 服务器:池 /Common/test-pool1 {”,“删除 gtm 拓扑 ldns:子网 10.8.22.0/24 服务器:池 /Common/test-pool1 {",
"删除 gtm 拓扑 ldns: 子网 0.0.0.0/0 服务器: pool /Common/test-pool2 {" ]
解决方案
这是你要找的吗?
vars:
info: [ 'a', 'b', 'c' ]
tasks:
- set_fact:
payload: "{{ payload|default([]) + [ 'delete ' ~ item ] }}"
loop: "{{ info }}"
- debug:
var: payload
给出:
"payload": [
"delete a",
"delete b",
"delete c"
]
追加删除所有盯着的东西
gtm topology
vars:
info: [ 'a', 'gtm topology', 'c' ]
tasks:
- set_fact:
payload: "{{ payload|default([]) + [ 'delete ' ~ item ] }}"
loop: "{{ info }}"
when: item is search('^gtm topology')
- debug:
var: payload
给出:
payload:
- delete gtm topology
除了上述条件和
输出到 test-pool1
def list_search(l, x):
r = re.compile(x)
return list(filter(r.match, l))
def list_index(l, x, *i):
if len(i) == 0:
return l.index(x) if x in l else -1
elif len(i) == 1:
return l.index(x, i[0]) if x in l[i[0]:] else -1
else:
return l.index(x, i[0], i[1]) if x in l[i[0]:i[1]] else -1
下面的戏
vars:
info: [ 'a', 'gtm topology 1', 'c', 'test-pool1', 'gtm topology 2', 'd' ]
stop_regex: '.*pool1.*'
tasks:
- set_fact: # Find elements that match stop_regex
stop_elements: "{{ info|list_search(stop_regex) }}"
- set_fact: # Find index of the fist element that match stop_regex
stop_index: "{{ info|list_index(stop_elements.0) }}"
- set_fact: # Select and transform elements of the list
payload: "{{ payload|default([]) + [ 'delete ' ~ item ] }}"
loop: "{{ info[0:stop_index|int] }}"
when: item is search('^gtm topology')
- debug:
var: payload
给出:
payload:
- delete gtm topology 1
推荐阅读
- javascript - 如何从 javascript 中的创建订阅 API 调用中检索 Stripe 订阅 ID
- python - Selenium:通过'option selected value'选择,默认时不存在
- docker - Docker compose 找不到本地包
- android - 将android生成的文件保存在运行模拟器的电脑上
- ios13 - iOS13不支持后台更新通知?
- php - PHP preg_match 正则表达式重定向网址
- vim - 为什么 UltiSnips 列出的可用片段不起作用?
- r - 在不同系列的线性模型中循环多个变量组合的问题
- php - PHP 创建循环来处理未知数量的传入 POST 数据
- javascript - jQuery没有检测到点击表中的链接