Performing regex_findall and split() on the same line - regex

Here is the output that I'm trying to parse:
hostname#show bgp vrf vrfname summary | i 1.1
BGP Route Distinguisher: 1.1.1.1:0
BGP router identifier 1.1.1.1, local AS number 2222
1.1.1.3 0 64512 349608 316062 896772 0 0 2w4d 1
I have the following regex that succesfully matches just the last line. Now I need to split that line and view the last index. In this case it is "1", but I will want to fail if that value is "0".
- name: debug test
debug:
msg: "{{show_bgp_sessions.data | regex_findall('\\d+\\.\\d+\\.\\d+\\.\\d+\\s\\s.*')}}"
I tried adding a split in a couple different formats at the end of the "msg" line so that I can grab the last index to compare it in the failed_when statement:
msg: "{{show_bgp_sessions.data | regex_findall('\\d+\\.\\d+\\.\\d+\\.\\d+\\s\\s.*') | split(' ')}}"
But I'm getting the following error msg:
"template error while templating string: no filter named 'split'. String:
I've also tried to use a few different forms of "ends_with" to verify the last index in the string as I've used that a lot in my python experience, but I can't get it to work in ansible.
I can't create a new task to parse the data and perform the split seperately because I need to run this verification through a loop.

When you select the line, reverse the string, and split the first item. For example
msg: "{{ (my_line|reverse).split()|first }}"

Possibly the regex provided by #Thefourthbird is a better solution.
But for your issue at hand, this is caused by the fact that there is indeed no filter split in Jinja, see the list there: https://jinja.palletsprojects.com/en/2.11.x/templates/#list-of-builtin-filters.
The reason why there is no such a filter is simple: split() is a function of the Python String, and since Jinja is Python, you can just use it as is.
Also mind that, since regex_findall is meant for multiple matches, you'll have to select the first element of the list, for example, with the filter first.
So your message ends up being:
msg: >-
{{
(
show_bgp_sessions.data
| regex_findall('\\d+\\.\\d+\\.\\d+\\.\\d+\\s\\s.*')
| first
).split()
}}
Given the playbook:
- hosts: all
gather_facts: no
vars:
show_bgp_sessions:
data: |
hostname#show bgp vrf vrfname summary | i 1.1
BGP Route Distinguisher: 1.1.1.1:0
BGP router identifier 1.1.1.1, local AS number 2222
1.1.1.3 0 64512 349608 316062 896772 0 0 2w4d 1
tasks:
- debug:
msg: >-
{{
(
show_bgp_sessions.data
| regex_findall('\\d+\\.\\d+\\.\\d+\\.\\d+\\s\\s.*')
| first
).split()
}}
Gives the recap:
TASK [debug] ***************************************************************
ok: [localhost] => {
"msg": [
"1.1.1.3",
"0",
"64512",
"349608",
"316062",
"896772",
"0",
"0",
"2w4d",
"1"
]
}

Related

How can I add a comma to separate each group of three digits with jinja?

for this, which filter should i use?
Project Total weight = 1000000
{{project.total_weight}} >> 1.000.000
You can take advantage of Python's string formatting syntax:
{{ '{:,}'.format(project.total_weight)
Here's how I tested that in a playbook:
---
- hosts: localhost
gather_facts: false
vars:
project:
total_weight: 1000000
tasks:
- debug:
msg: "{{ '{:,}'.format(project.total_weight) }}"
The above will output:
TASK [debug] ******************************************************************************************
ok: [localhost] => {
"msg": "1,000,000"
}
This is documented in the docs for the format method:
The ',' option signals the use of a comma for a thousands separator. For a locale aware separator, use the 'n' integer presentation type instead.

Ansible List Variable and Select Filter to ignore matched items

I have a variable dir_lst_raw in an ansible playbook whose value is a list as shown below:
"dir_lst_raw": [
"/path1/dir1/user",
"/path2/dir2/admin",
"/path3/dir3/user.ansible_backup_2020-03-16",
"/path1/dir1/config.ansible_backup_2020-03-16",
"/path2/dir2/dir3/somefile"
]
I need to remove all the lines containing .ansible_backup_ and save to another variable as a list.
I've googled for regex and tried to not match the pattern with the select filter as below:
- set_fact:
dir_lst: "{{ dir_lst_flt_r | select('match','(^.ansible_backup_)+') | list }}"
but the new variable dir_lst turned out as an empty list. I am expecting dir_lst as below:
"dir_lst_raw": [
"/path1/dir1/user",
"/path2/dir2/admin",
"/path2/dir2/dir3/somefile"
]
Could somebody please suggest how can I get it done?
Q: "Remove all the lines containing .ansible_backup_"
A: The task below does the job
- set_fact:
dir_lst: "{{ dir_lst_raw|reject('match', my_regex)|list }}"
vars:
my_regex: '^(.*)\.ansible_backup_(.*)$'
- debug:
var: dir_lst
gives
dir_lst:
- /path1/dir1/user
- /path2/dir2/admin
- /path2/dir2/dir3/somefile

How to Insert a new string into telegraf.conf's inputs.ping using ansible regexp

I'm trying to use ansible to update telegraf.conf's [[inputs.ping]].
telegraf.conf looks like the following:
[[inputs.ping]]
urls = ["tac-temp1","tac-temp2", "tac-temp3","tac-temp4"] #tac
count = 30
timeout = 15.0
[inputs.ping.tags]
name = "tac"
[[inputs.ping]]
urls = ["prod-temp1","prod-temp2", "prod-temp3","prod-temp4"] #prod
count = 30
timeout = 15.0
[inputs.ping.tags]
name = "prod"
[[inputs.ping]]
urls = ["test-temp1","test-temp2", "test-temp3","test-temp4"] #test
count = 30
timeout = 15.0
[inputs.ping.tags]
name = "test"
I'm trying to add ,"tac-temp10" after ,"tac-temp4" in line 2 shown above.
- hosts: Servers
become: yes
become_method: sudo
tasks:
- name: Loading telegraf.conf content for search
shell: cat /tmp/telegraf.conf
register: tele_lookup
- name: Adding Server to /tmp/telegraf.conf if does not exists
lineinfile:
path: /tmp/telegraf.conf
state: present
regexp: '^((.*)"] #tac$)'
line: ',"tac-temp10"'
backup: yes
when: tele_lookup.stdout.find('tac-temp10') != '0'
regexp: '^((.*)"] #tac$)' is replacing the whole line with ,"tac-temp10". Expected output:
[[inputs.ping]]
urls = ["tac-temp1","tac-temp2", "tac-temp3","tac-temp4","tac-temp10"] #tac
count = 30
timeout = 15.0
[inputs.ping.tags]
name = "tac"
Warning: Ugly regexp ahead. Beware of unpredictable understanding for next guys (including you after time passed by...) doing maintenance.
The following will add your server at the end of the list if it is not already present (anywhere in the list) with a single idempotent task.
- name: add our server if needed
lineinfile:
path: /tmp/test.conf
backup: yes
state: present
regexp: '^( *urls *= *\[)(("(?!tac-temp10)([a-zA-Z0-9_-]*)",? *)*)(\] #tac)$'
backrefs: yes
line: '\1\2, "tac-temp10"\5'
You need to use backreferences to put back on the line the already matched parts of the expression. I used backup: yes so I could easily come back to the original for my tests. Feel free to drop it.
As you can see (and as advised in my warning) this is pretty much impossible to understand for anyone having to quickly read the code. If you have to do anything more fancy/complicated, consider using a template and storing your server list in a variable somewhere.

Extracting all named values from an ansible array

I'm not even quite sure how to describe what I'm trying to do!
So I'll just jump straight in.
I have a hosts file :
[jvbservers]
jvb0 ansible_host=serverA
jvb1 ansible_host=serverB
etc...
And I want a list of the servers in the jvbservers group. What I've come up with so far feels a bit OTT.
set_fact:
jvb_names: "{{ jvb_names|default([]) + [ hostvars[groups.jvbservers[item|int]].ansible_host ] }}"
with_sequence: start=0 end="{{groups.jvbservers|length-1}}"
Gives me a result like I want :
ok: [localhost] => {
"jvb_names": [
"serverA",
"serverB"
]
}
Is this sensible? It feels like there should be a neater way to do something like :
hostvars[groups.jvbservers[*]].ansible_host
(Where "*" would be a pattern matching all valid options. Obvs that particular syntax doesn't exist!)
You may want to check extract filter:
{{ groups['jvbservers'] | map('extract', hostvars, 'ansible_host') | list }}
Have you tried this?:
- name: show all the hosts matching jvbservers
debug:
msg: "{{ item }}"
with_inventory_hostnames:
- all: jvbservers
https://docs.ansible.com/ansible/devel/plugins/lookup/inventory_hostnames.html

Ansible variable manipulation

I have a variable in my playbook that has a number of values separated by commas. At this point I am not sure if the variable is a string or a list. I believe from the output below the "[]" indicate a list.
Variable populated
-set_fact:
snap_master_01: "{{ ec2_snapshot_facts.snapshots |
selectattr(tags.HostName, equalto, ICINGA2_MASTER_1.tag_value) |
sort(attribute=start_time) | reverse | map(attribute=snapshot_id) | list }}"
- name: Print snapshot ID's
debug:
msg:
- "{{ snap_master_01 }}"`
Gives the following output:
`TASK [Print snapshot ID's] ******************************************************************************** ***********************
task path: /home/r_ansible/playbooks/backup_aws.yml:252
ok: [172.16.1.58] => {
"changed": false,
"msg": [
[
"snap-04c88ef6XXXXXXXXX",
"snap-0bd5785fXXXXXXXXX",
"snap-045e0f4bXXXXXXXXX",
"snap-055fda51XXXXXXXXX",
"snap-03759206XXXXXXXXX"
]
]
}`
I would like to delete the last 3 values. What is the best way of achieving this in Ansible?
To manipulate lists in Ansible, you can use Python slices.
In your case snap_master_01[:-3] will give you all but last three elements.