【问题标题】:Ansible: convert a list with unicode to a list of strings (and compare them)Ansible:将带有 unicode 的列表转换为字符串列表(并比较它们)
【发布时间】:2020-01-29 20:31:43
【问题描述】:

我创建了一个包含所有 ansible 组主机的列表。 但是当我打印列表时,它包含 unicode 字符。 u'text' 而不是 "text" 有没有有效的方法来删除/转换它。 我在网上查看了其他示例,但没有成功。

带有 unicode 字符的当前(错误)输出:

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' .....

这应该具有与以下列表相同的格式。因为我想将它们与差异进行比较。 列表本身的值是不同的,但格式应如下:

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", ...

目前我正在使用以下代码来获取列表:

- 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 获取的。 另请注意,由于某种原因, [括号 之前似乎有一个 " 。 如下所示:

"msg": " [u'all',....

这不在第一个列表中。知道为什么会这样。我认为这会在以后比较它们时出现问题。 我创建列表的方式是相同的。

【问题讨论】:

    标签: list ansible diff python-unicode ansible-awx


    【解决方案1】:

    这最终解决了我的大部分问题。

    - 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", 
             .... 
    

    【讨论】:

      猜你喜欢
      • 2015-09-18
      • 2014-09-30
      • 1970-01-01
      • 2021-12-17
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 2021-03-09
      相关资源
      最近更新 更多