Ansible 从列表中删除空行 [英] Ansible remove blank lines from list

查看:28
本文介绍了Ansible 从列表中删除空行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个剧本可以检索 AD 组的成员并为我提供他们的用户 ID 列表,如下所示:

So I've got a playbook that retrieves the members of an AD group and gives me a list of their user IDs like this:

  tasks:
- name: Get group members
  set_fact:
    member: "{{ item }}"
  register: members
  with_ldap:
    - context: group_members
    - Jira_Administrators_GG

- name: Get userids
  set_fact:
    userid: "{{ lookup('ldap', '{{ item.item }}', context='users') }}"
  register: userids
  with_items: "{{ members.results }}"

- name: Create list of userids
  set_fact:
    userid_list: "{{ userids.results | map(attribute='ansible_facts.userid') | list }}"

问题是我在结果列表中得到了几个空行:

Trouble is I end up with a few blank lines in the resultant list:

- name: Show userids
  debug:
    msg: "{{ hostvars['localhost']['userid_list'] }}"

输出:

TASK [Show userids] **************************************

ok: [xxxxxxxxx01] => {
    "msg": [
        "xxxxxxxxx55",
        "xxxxxxxxx58",
        "xxxxxxxxx71",
        [],
        "xxxxxxxxx46",
        [],
        "xxxxxxxxx27",
        [],
        "xxxxxxxxx63",
        "xxxxxxxxx27",
        [],
        "xxxxxxxxx04",
        "xxxxxxxxx87"
    ]
}

有谁知道如何从列表中删除空行?我看过 http://jinja.pocoo.org/docs/2.9/templates/#builtin-filters 并在 map 之间尝试了几个像 replace()rejectattr()> 和 list 但无论出于何种原因我都没有做对.

Does anyone know how to remove the blank lines from the list? I've had a look through http://jinja.pocoo.org/docs/2.9/templates/#builtin-filters and tried a couple like replace() and rejectattr() in between the map and list but for whatever reason I'm not getting it right.

编辑 - 一些无效的尝试...

EDIT - Some attempts that didn't work...

我显然没有得到正确的语法:

I'm clearly not getting the syntax right with this one:

- name: Create list of userids
  set_fact:
    userid_list: "{{ userids.results | rejectattr('ansible_facts.userid', 'equalto', '') | map(attribute='ansible_facts.userid') | list }}"

...因为输出是:

    An exception occurred during task execution. To see the full traceback, use -vvv. The error was: TemplateRuntimeError: no test named ''
fatal: [localhost]: FAILED! => {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}

然后我尝试在 map 之前移动 rejectattr().这更好,它实际上运行但似乎对结果列表没有任何影响:

I then tried moving rejectattr() before map. This was better, it actually runs but doesn't seem to make any difference to the resultant list:

- name: Create list of userids
  set_fact:
    userid_list: "{{ userids.results | rejectattr('ansible_facts.userid', 'equalto', '') | map(attribute='ansible_facts.userid') | list }}"

我尝试使用 reject() 而不是 rejectattr() 像这样:

I tried with reject() instead of rejectattr() like this:

- name: Create list of userids
  set_fact:
     userid_list: "{{ userids.results | reject('equalto', '') | map(attribute='ansible_facts.userid') | list }}"

...就像这样:

- name: Create list of userids
  set_fact:
     userid_list: "{{ userids.results | map(attribute='ansible_facts.userid') | reject('equalto', '') | list }}"

我尝试了一些排列,我也尝试与 '[]' 而不是 '' 进行比较,因为我的空行在结果列表中显示为这样.我想我只是不明白如何正确应用过滤器.

I've tried a few permutations, I also tried comparing to '[]' instead of '' because my empty lines show like that in the resultant list. I guess I just don't understand how to apply the filters correctly.

编辑 - 变量内容

现在如果我这样做

- debug:
    var: userids

我得到了这个(截断以显示 2 个填充了 userid 的结果和 1 个 userid 为空的结果):

I get this (truncated to show 2 results that have userid populated and 1 with userid blank):

ok: [localhost] => {
    "userids": {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "ansible_facts": {
                    "userid": "xxxxxxxxx55"
                },
                "changed": false,
                "item": {
                    "_ansible_item_result": true,
                    "_ansible_no_log": false,
                    "ansible_facts": {
                        "member": "CN=Liam Fitzpatrick,OU=My User OU,DC=my,DC=domain,DC=com"
                    },
                    "changed": false,
                    "item": "CN=Liam Fitzpatrick,OU=My User OU,DC=my,DC=domain,DC=com"
                }
            },
            {
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "ansible_facts": {
                    "userid": "xxxxxxxxx58"
                },
                "changed": false,
                "item": {
                    "_ansible_item_result": true,
                    "_ansible_no_log": false,
                    "ansible_facts": {
                        "member": "CN=Mr Jones,OU=My User OU,DC=my,DC=domain,DC=com"
                    },
                    "changed": false,
                    "item": "CN=Mr Jones,OU=My User OU,DC=my,DC=domain,DC=com"
                }
            },
            {
                "_ansible_item_result": true,
                "_ansible_no_log": false,
                "ansible_facts": {
                    "userid": []
                },
                "changed": false,
                "item": {
                    "_ansible_item_result": true,
                    "_ansible_no_log": false,
                    "ansible_facts": {
                        "member": "CN=Mr Smith,OU=My User OU,DC=my,DC=domain,DC=com"
                    },
                    "changed": false,
                    "item": "CN=Mr Smith,OU=My User OU,DC=my,DC=domain,DC=com"
                }
            }

推荐答案

你有一个字符串列表(例如 "xxxxxxxxx63")和空列表([])– 不是空字符串.

You have a list of strings (e.g. "xxxxxxxxx63") and empty lists ([]) – not empty strings.

您可以使用 Jinja2 select/reject 过滤器,例如:

You can use Jinja2 select/reject filters, for example:

- name: Create list of userids
  set_fact:
    userid_list: "{{ userids.results | map(attribute='ansible_facts.userid') | select('string') | list }}"

附言并避免嵌套的大括号,使用:

P.S. and avoid nested curly brackets, use:

"{{ lookup('ldap', item.item, context='users') }}"

这篇关于Ansible 从列表中删除空行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆