【问题标题】:Ansible jinja2 compare strings non case sensitiveAnsible jinja2 比较字符串不区分大小写
【发布时间】:2020-04-24 05:23:48
【问题描述】:

我有以下复杂的字典(这只是一个示例)。我正在尝试获取属于 server1 的所有 id 的列表。 server1 的名称大小写不一。

我尝试了 jinja2 过滤器,例如 matchsearchequalto,但它们都没有返回预期结果。还尝试了 JSON 查询,但仍然缺少如何将全部小写或大写进行比较。

    ---
    - name: TEST
      hosts: localhost
      gather_facts: no
    
      vars:
        datacenters: {
          cabinets: {
            servers: [
              {
                name: Server1,
                id: 1
              },
              {
                name: SERVER1,
                id: 2
              },
              {
                name: Server2,
                id: 3
              },
              {
                name: server1,
                id: 4
              },
             ]
          }
        }
    
      tasks:
        - name: get ids for Server 1 
          set_fact:
            ids: "{{ datacenters.cabinets.servers
              | selectattr('name','match','Server1')
              | map(attribute='id')
              | list }}"
    
        - debug:
              var: ids
    
        - debug: msg="{{ datacenters | json_query(\"cabinets.servers[?name == 'Server1'].id\") }}"

【问题讨论】:

    标签: string ansible comparison jinja2 json-query


    【解决方案1】:

    这可以通过 ansible 的 when 和 lower 过滤器来实现。下面的剧本适合我。

    剧本:

    - name: Demo of restore plan
      hosts: localhost
      gather_facts: False
      vars:
        datacenters: {
            cabinets: {
              servers: [
                {
                  name: Server1,
                  id: 1
                },
                {
                  name: SERVER1,
                  id: 2
                },
                {
                  name: Server2,
                  id: 3
                },
                {
                  name: server1,
                  id: 4
                },
              ]
            }
          }
      tasks:
        - debug:
            msg: "{{ item.name }}"
          with_items:
            - "{{ datacenters.cabinets.servers }}"
          when: item.name|lower == "server1"
    

    输出:

    [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
    
    
    PLAY [Demo of restore plan] ************************************************************************************************************************************************
    
    TASK [debug] ***************************************************************************************************************************************************************
    ok: [localhost] => (item={'name': 'Server1', 'id': 1}) => {
        "msg": "Server1"
    }
    ok: [localhost] => (item={'name': 'SERVER1', 'id': 2}) => {
        "msg": "SERVER1"
    }
    skipping: [localhost] => (item={'name': 'Server2', 'id': 3})
    ok: [localhost] => (item={'name': 'server1', 'id': 4}) => {
        "msg": "server1"
    }
    
    PLAY RECAP *****************************************************************************************************************************************************************
    localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    

    Ansible 仅列出 server1 行并忽略 server2

    希望这会有所帮助

    【讨论】:

    • 这是正确的答案。你知道是否有一种方法可以在不使用循环而是使用 jinja2 语法的情况下实现相同的目标?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多