我正尝试在ansible中将字符串追加到列表中,因此基本上我将构建有效负载以删除F5 GTM网络设备中的少数拓扑记录。
我能够创建一个单独的列表,其中包括相应拓扑记录的所有输出。对于输出的每一行,我需要附加一个字符串“ delete”。
- 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的预期输出应类似于:-
基本上,我需要在每个输出的前面添加一个字符串'delete'。
“ topology_payload”:[
“删除gtm拓扑ldns:子网10.10.10.0/24服务器:pool / Common / test-pool1 {”,
“删除gtm拓扑ldns:子网10.8.22.0/24服务器:池/ Common / test-pool1 {”,
“删除gtm拓扑ldns:子网0.0.0.0/0服务器:池/ Common / test-pool2 {”
]
答案 0 :(得分:0)
您可以使用map filter将功能应用于列表中的每个元素。将其与regex_replace
结合使用,可以实现所需的结果:
---
- hosts: localhost
gather_facts: false
vars:
string: "prepend "
list: ["value1", "value2", "value3"]
tasks:
- name: "append string to each element in a list"
set_fact:
list: "{{ list | map('regex_replace', '(.*)', '{{ string }}\\1') | list }}"
- debug:
msg: "{{ list }}"
答案 1 :(得分:0)
您是looking for吗?
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
答案 2 :(得分:0)
您可以使用product filter为项目列表添加前缀或后缀:
---
- hosts: localhost
gather_facts: false
vars:
string: "prepend "
list: ["value1", "value2", "value3"]
tasks:
- name: "prefix"
set_fact:
prefix_list: "{{ "prefix_" | product(list) | map('join') }}"
- debug:
msg: "{{ prefix_list }}"
- name: "suffix"
set_fact:
suffix_list: "{{ list | product(["_suffix"]) | map('join') }}"
- debug:
msg: "{{ suffix_list }}"