Ansibe Jinja2 Filter - list

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'

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

Related

How can I use regex in when condition

On an Ansible playbook, I'm trying to execute a shell command only if a service exist on the remote server.
I have 3 tasks :
service_facts
execution of shell command if tomcat is installed
display the output of the shell command if tomcat is installed
Here is my code :
- name: Get Infos
hosts: all
gather_facts: yes
become: false
remote_user: [MY_USER]
tasks:
- name: Get the list of services
service_facts:
- name: Get version of Tomcat if installed
become: true
shell: 'java -cp /opt/tomcat/lib/catalina.jar org.apache.catalina.util.ServerInfo | grep "Server version"'
register: tomcat_version
when: "'tomcat.service' in services"
- debug: msg="{{ tomcat_version.stdout_lines }}"
when: "'tomcat.service' in services"
The problem is on certains servers the service name is, for example, tomcat-8.1
How can i use regex in the when condition?
I tried regex(), regex_search(), either I'm doing it wrong or I don't know how to do it.
Have you any idea how to do it?
Thanks in advance!
Count matching items. For example
- service_facts:
- block:
- shell: smartctl --version | head -1
register: smart_version
- debug:
msg: "{{ smart_version.stdout_lines }}"
when: _srvcs|length > 0
vars:
_regex: '.*smart.*'
_srvcs: "{{ services|select('match', _regex) }}"
gives
msg:
- smartctl 7.1 2019-12-30 r5022 [x86_64-linux-5.4.0-73-generic] (local build)
The next option is to intersect the list of services, e.g.
when: _srvcs|length > 0
vars:
my_services:
- smartmontools.service
- smart-8.1
- smart-devel.0.0.1
_srvcs: "{{ my_services|intersect(services) }}"
Debug
Q: "It gives me a failure on the server where my service doesn't exist, cause the playbook still tries to execute the shell. Is it normal?"
A: No. It is not normal. Print debug and find out why the condition evaluates to true, i.e. what service(s) match either the regex or the list. For example
- debug:
msg: |
_srvcs:
{{ _srvcs|to_nice_yaml|indent(2) }}
when: debug|d(false)|bool
vars:
my_services:
- smartmontools.service
- smart-8.1
- smart-devel.0.0.1
_srvcs: "{{ my_services|intersect(services) }}"
gives
msg: |-
_srvcs:
- smartmontools.service
To enable the task run the playbook with the option -e debug=true.

ansible parse date using regex search

I am trying parse month,day and year from show clock output using regex_search, getting error.
from cli of a router i see this -
sh clock
16:22:12.975 PST Wed Jan 27 2021
- name: Run sh log
cisco.ios.ios_command:
commands:
- sh clock
register: output1
- name: sh clock output
debug:
msg: "{{ output1.stdout_lines | regex_search('(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)^\s+\d{1,2}\s+\d{4}' }}"
error
The offending line appears to be:
debug:
msg: "{{ output1.stdout_lines | regex_search('(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+\d{1,2}\s+\d{4}' }}"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
desired/wanted debug msg is below ,i don't want time, just need month day and year
Jan 27 2021
Use regex_replace. Put the regex into a separate single-quoted variable. For example, the task below does the job
- name: sh clock output
debug:
msg: "{{ output1.stdout_lines|regex_replace(my_regex, my_replace) }}"
vars:
my_regex: '^(.*)(Jan(uary)?|Feb(ruary)?|Mar(ch)?|Apr(il)?|May|Jun(e)?|Jul(y)?|Aug(ust)?|Sep(tember)?|Oct(ober)?|Nov(ember)?|Dec(ember)?)\s+(\d{1,2})\s+(\d{4})$'
my_replace: '\2 \14 \15'
The splitting is simpler. For example, the task below gives the same result
- debug:
msg: "{{ arr[-3] }} {{ arr[-2] }} {{ arr[-1] }}"
vars:
arr: "{{ output1.stdout_lines.split() }}"
See Handling dates and times

From a list of dictionaries, create one dictionary which members contain a list

Having the following list of dictionaries:
- set_fact:
inventory:
- dn: host1
lid: 0021-00
unit_id: 50
- dn: host2
lid: 1011-00
unit_id: 50
- dn: host2
lid: 1004-00
unit_id: 50
Iterating over that list I'd like to create a new
dictionary as like (dn becomes key and lid becomes list value):
- set_fact:
dn2lid:
host1:
- 0021-00
host2:
- 1011-00
- 1004-00
Your help is much appreciated.
The task below does the job
- set_fact:
dn2lid: "{{ dn2lid|default({})|combine({item.0: lids}) }}"
loop: "{{ inventory|groupby('dn') }}"
vars:
lids: "{{ item.1|map(attribute='lid')|list }}"
- debug:
var: dn2lid
gives
dn2lid:
host1:
- 0021-00
host2:
- 1011-00
- 1004-00

Ansible Filter regex_search does not Exist

I have this snipped of code from our playbook:
- name: Set version
set_fact:
my_version: "My version is {{ my_file.stdout | regex_search('[0-9\.]+') }}"
... where my_file is 'program_1.2.3_install.exe'
It returns that the filter regex_search does not exist.
We are running Ansible 2.0.0.2
Does anybody know how to make this regex work?
Thanks!
Given the variable
my_file: 'program_1.2.3_install.exe'
The task
- set_fact:
my_version: "{{ my_file|regex_replace(myregex, myreplace) }}"
vars:
myregex: '^(.*?)([0-9\.]+)(.*)$'
myreplace: '\2'
- debug:
var: my_version
gives
"my_version": "1.2.3"
It's also possible to use split(). The task below gives the same result
- set_fact:
my_version: "{{ my_file.split('_').1 }}"
- debug:
var: my_version

Ansible regex_replace or regex_search

I am trying to match the "OK" from the following output with regex and store it in a varible:
System 'server.mylabserver.com'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
load average [0.00] [0.01] [0.05]
cpu 0.1%us 0.1%sy 0.0%wa
memory usage 367.9 MB [20.0%]
swap usage 0 B [0.0%]
uptime 2h 10m
boot time Mon, 02 Apr 2018 06:51:01
data collected Mon, 02 Apr 2018 09:01:02
Ansible code with "regex_replace" that I've tried:
- name: Fetch the monit status
shell: "monit status | tail -n +3"
register: monit_status_raw
tags: basic_monitoring
- name: Extract monit variables
set_fact:
vmstatus: "{{ monit_status_raw | regex_replace('^\s\s([a-z]*)\s+', '\\1:')}}"
Error:
The offending line appears to be:
set_fact:
vmstatus: "{{ monit_status_raw | regex_replace('^\s\s([a-z]*)\s+', '\\1')}}"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
Ansible code with "regex_search" that I've tried:
- name: Fetch the monit status
shell: "monit status | tail -n +3"
register: monit_status_raw
- name: Extract monit variables
set_fact:
vmstatus: "{{ monit_status_raw | regex_search('^\s\sstatus\s+(.*)$') }}"
Error:
The offending line appears to be:
set_fact:
vmstatus: "{{ monit_status_raw | regex_search('^\s\sstatus\s+(.*)$') }}"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
Any idea what it's wrong in the regexes?
Thank you,
Dan
I think if you'd like to use regexp_search - you need to give a string and think about escaping characters, and then you need to use some construction as:
with_items
- "{{ monit_status_raw.stdout_lines }}"
But I think it will be simpler:
- name: Fetch the monit status
shell: 'monit status | tail -n +2 | grep "^\s*status" '
register: monit_status_raw
- set_fact:
vmstatus: "{{ monit_status_raw.stdout.split('status')[1]| replace(' ','')}}"
You will get vmstatus = 'Ok', if you use your sample.