【问题标题】:Ansibe Jinja2 FilterAnsible Jinja2 过滤器
【发布时间】:2021-05-28 15:09:02
【问题描述】:

我想对以下日志文​​件应用过滤器。但我总是错过一些东西

task.yml

-   ansible_loop_var: item
    changed: false
    failed: true
    invocation:
        module_args:
            policy_package: Package1
            version: null
            wait_for_task: true
            wait_for_task_timeout: 30
    item: PackageItem1
    msg: Task Verify policy operation with task id 01234567-89ab-cdef-a422-xxxxxxxxx
        failed. Look at the logs for more details
-   ansible_loop_var: item
    changed: false
    failed: true
    invocation:
        module_args:
            policy_package: Package2
            version: null
            wait_for_task: true
            wait_for_task_timeout: 30
    item: PackageItem2
    msg: Task Verify policy operation with task id 01234567-89ab-cdef-a6c4-xxxxxxxx
        failed. Look at the logs for more details

这是我的剧本 filter.yml

---
- name: sftp-domain
  hosts: check_point
  connection: httpapi
  gather_facts: False
  vars_files:
    - 'credentials/my_var.yml'
    - 'credentials/login.yml'

  tasks:
  - name: set-details
    set_fact:
      filter: "{{ lookup('file', 'tmp/task.yml') }}"

  - name: create list to loop through
    set_fact:
      new_list: "{{ filter | map(attribute='msg') | flatten }}"

  - name: copy-file-to-log
    local_action:
      module: copy
      content: "{{  new_list | to_nice_yaml }}"
      dest: tmp/task2.yml

我收到一条错误消息说

PLAY [sftp-domain] *******************************************************************************************************************************************

TASK [set-details] *******************************************************************************************************************************************
ok: [checkpoint]

TASK [create list to loop through] ***************************************************************************************************************************
fatal: [checkpoint]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'unicode object' has no attribute 'msg'\n\nThe error appears to be in '/home/tdeveu0/project/fwp_automation/filter.yml': line 15, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name: create list to loop through\n    ^ here\n"}

这其实是我应用过滤器后想要的结果

- msg: Task Verify policy operation with task id 01234567-89ab-cdef-a6c4-xxxxxxxx
        failed. Look at the logs for more details

- msg: Task Verify policy operation with task id 01234567-89ab-cdef-a422-xxxxxxxxx
        failed. Look at the logs for more details

我只想获取所有“味精”的列表

【问题讨论】:

    标签: list filter ansible jinja2


    【解决方案1】:

    使用过滤器from_yaml

      - name: set-details
        set_fact:
          filter: "{{ lookup('file', 'tmp/task.yml')|from_yaml }}"
    

    让我们用一个简化的文件来说明问题,例如

    shell> cat task.yml
    - a: 1
      b: 2
    - a: 3
      b: 4
    

    当您将文件读入变量时,结果是 AnsibleUnsafeText,而不是列表

        - set_fact:
            filter: "{{ lookup('file', 'task.yml') }}"
        - debug:
            msg: "{{ filter|type_debug }}"
        - debug:
            var: filter.0
    

    给予

      msg: AnsibleUnsafeText
      filter.0: '-'
    

    文本的第一项是破折号'-'。


    使用过滤器from_yaml获取列表

        - set_fact:
            filter: "{{ lookup('file', 'task.yml')|from_yaml }}"
        - debug:
            msg: "{{ filter|type_debug }}"
        - debug:
            var: filter.0
    

    给予

      msg: list
      filter.0:
        a: 1
        b: 2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-08
      • 2017-12-11
      • 1970-01-01
      相关资源
      最近更新 更多