Ansible jinja2 过滤器拒绝

Ansible jinja2 过滤器拒绝

我需要从标准输出行中拒绝“*”。

"stdout_lines": [
    "rchinnn01",
    "rchinnn02",
    "*"
]

- set_fact:
  nfs_clients:  "{{ nfs_clients_out.stdout_lines | reject('search','*') | list }}"

剧本输出:

TASK [set_fact] *****************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: error: nothing to repeat
fatal: [rchinnn03]: FAILED! => {"msg": "Unexpected failure during module execution.", "stdout": ""}

答案1

正则表达式“[*]”有效。下面的剧

vars:
  my_lines:
    - "rchinnn01"
    - "rchinnn02"
    - "*"
tasks:
  - debug:
      msg: "{{ my_lines|reject('match', '[*]')|list }}"

给出(删节):

ok: [localhost] => {
    "msg": [
        "rchinnn01", 
        "rchinnn02"
    ]
}

答案2

我建议使用 equalto 而不是搜索:

- set_fact:
  nfs_clients:  "{{ nfs_clients_out.stdout_lines | reject('equalto','*') | list }}"

https://stackoverflow.com/questions/24041885/conditionally-join-a-list-of-strings-in-jinja

答案3

请按如下方式使用:

- set_fact:
     nfs_clients: "{{ nfs_clients_out.stdout_lines | reject('match','rchinnn*') | list }}"

请检查 jinja2 版本详细信息

相关内容