Ansible从列表

时间:2017-10-19 09:59:58

标签: ansible jinja2

所以我有一个可以检索AD组成员的剧本,并给我一个他们的用户ID列表:

  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 }}"

麻烦是我最终在结果列表中留下了几个空行:

- 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并在replace()rejectattr()之间尝试了maplist之类的情侣,但无论出于何种原因,我都不是做对了。

编辑 - 有些尝试不起作用......

我显然没有正确使用这个语法:

- 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": ""}

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

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

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

- 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 }}"

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

编辑 - 可变内容

现在,如果我这样做

- debug:
    var: userids

我得到了这个(截断显示2个已填充用户ID的结果和1个用户ID为空的结果):

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"
                }
            }

1 个答案:

答案 0 :(得分:1)

您有一个字符串列表(例如"xxxxxxxxx63")和空列表([]) - 不是空字符串。

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

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

P.S。并避免使用嵌套的花括号,使用:

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