doctore
doctore

Reputation: 334

Ansibe Jinja2 Filter

I want to apply filter to the following log file. But I keep missing something

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

Here is my playbook 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

I get an error message saying

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

Here is actually the result I want after applying filter

- 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

I want to get only the list of all the 'msg'

Upvotes: 2

Views: 157

Answers (1)

Vladimir Botka
Vladimir Botka

Reputation: 67984

Use the filter from_yaml

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

Let's take a simplified file to show the problem, e.g.

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

When you read the file into the variable the result is AnsibleUnsafeText, not a list

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

gives

  msg: AnsibleUnsafeText
  filter.0: '-'

The first item of the text is the dash '-'.


Use filter from_yaml to get the list

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

gives

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

Upvotes: 4

Related Questions