Ansible:将带有 unicode 的列表转换为字符串列表(并比较它们) [英] Ansible: convert a list with unicode to a list of strings (and compare them)
问题描述
我创建了一个包含所有 ansible 组主机的列表.但是当我打印列表时,它包含 unicode 字符.u'text' 而不是 "text"有什么有效的方法可以删除/转换它.我在网上查看了其他示例,但没有成功.
I created a list with all the ansible group hosts. But when I print the list it contains unicode characters. u'text' instead of "text" Is there any efficient way to remove/convert this. I looked at other examples online but didn't succeed.
带有 unicode 字符的当前(错误)输出:
Current (wrong) output with unicode characters:
ok: [server.name] => {
"msg": " [u'all', u'coaster', u'aes', u'curo, u'dert', u'tomcatdeploy', u'implus-app', u'domain-top', u'tp-general', u'cdaes01', u'dicco-acc' .....
这应该具有与以下列表相同的格式.因为我想将它们与 diff 进行比较.列表中的值本身不同,但格式应为:
This should have an identical format like the following list. Because I want to compare them with diff. The values in the list itself are different, but should be formatted like:
ok: [server.name] => {
"msg": [
"tools",
"tools-api",
"adr-app",
"adr-app-e2j",
"aec",
"aec-copy",
"aes",
"aes1",
"aes2",
"aes3",
"ais",
"apiman",
"apiman-gateway-poc",
"apiman-manager",
"apiman-manager-poc",
"apollo-beheer-aangiftes", ...
目前我正在使用以下代码来获取列表:
Currently I'm using following code to fetch the list:
- name: register groups
set_fact:
inventory_groups: [] # empty list
inventory_group_keys: "{{ groups.keys() |to_yaml}}" #|list
- name: clean white spaces from list
set_fact:
inventory_groups: "{{ inventory_groups}} + ['{{item.strip()}}']"
with_items: "{{inventory_group_keys[1:-2].split(',')}}"
- name: print inventory_groups from local host for debugging
debug:
msg: " {{ inventory_groups }}"
第二个列表是使用 AWX api 获取的.另请注意,由于某种原因, [ 括号 之前似乎有一个 " .就像下面突出显示的一样:
The second list is fetched with an AWX api. Also note that for some reason there seems to be a " before the [ bracket . Like highlighted below:
"msg": " [u'all',....
哪个不在第一个列表中.知道这是为什么.我认为这会在以后比较它们时出现问题.我创建列表的方式是相同的.
Which isn't in the first list. Any idea why this is. I assume this will give problems when comparing them later on. The way that I create the lists is identical.
推荐答案
这最终解决了我的大部分问题.
This ended up solving most of my issue.
- name: print list
debug:
msg: "{{ groups | list }}"
其中给出了以下列表:
ok: [server.name] => {
"msg": [
"all",
"coster",
"ius1",
"curo",
"derti",
"tomcatdeploy",
"implus-app",
"domain",
"tpgeneral",
"cdaes",
"diccop-acc",
"cdaes",
....
这篇关于Ansible:将带有 unicode 的列表转换为字符串列表(并比较它们)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!