-1

I'm trying to extract a specific number from my stdout_lines in Ansible and use that as a variable. I'm running a show command in my playbook and all I want to get from the output is the highest sequence number from my crypto map. For example this is my playbook:

 - 
  asa_command:
      commands:
        -  show run crypto map
      provider: "{{ base_provider }}"
  register: result

-
  debug: var=result.stdout_lines

This produces the output fine but I'm not sure how to go about extracting the sequence number from the following (I have omitted most of the crypto map just to make it easier to explain).

"crypto map map1 60 set ikev1 transform-set test",
"crypto map map1 60 set security-association lifetime seconds 3600", 
"crypto map map1 61 set peer 1.1.1.1 ",
"crypto map map1 61 set ikev1 transform-set test1", 
"crypto map map1 61 set security-association lifetime seconds 3600", 
"crypto map map1 interface outside"

So basically, I would like to extract the highest sequence number (in this case "61") so I can input it as a variable in the same playbook. Any thoughts would be appreciated :-)

I tried looking at some jinja2 filters but I couldn't figure out what would be most appropriate for my usage.

http://ansible-docs.readthedocs.io/zh/stable-2.0/rst/playbooks_filters.html

I also tried the suggestions on this page but I didn't get far with that either.

ansible parse text string from stdout

mmbecks7
  • 3
  • 1
  • 3
  • is the number always preceded by `map1` and followed by `set`? – emsimpson92 Jul 05 '18 at 15:55
  • Yes, that's correct emsimpson92. – mmbecks7 Jul 05 '18 at 16:10
  • techraf - I did try to solve the problem before asking. I have edited my question to reflect that but my jinja2 knowledge is not very strong. – mmbecks7 Jul 05 '18 at 16:16
  • The linked document does not claim you did not try to solve the problem. It also gives you a clear guidance on **what to do next**. Please follow the advices given in this section instead of linking to other pages and claiming "I tried that". It's not helpful to anyone. – techraf Jul 05 '18 at 16:25

1 Answers1

0

Note that I'm pants-ing this in a notepad without full access to the tools, so please check my syntax, especially on those double-backslash escapes. That said, let's take a stab at a chain of filters that gets what you need. How about:

- debug: msg="{{ result.stdout                                       |
                 regex_findall ('^"crypto map map1 \\d\\d set ')     |
                 regex_replace ('^"crypto map map1 (\\d\\d) set .*',
                                '\\1')                               |
                 max
              }}"
Paul Hodges
  • 13,382
  • 1
  • 17
  • 36
  • I don't think you can replace `asa_command` module with `shell` in Ansible. If you are going to provide free coding services on SO at least try to do it reliably. – techraf Jul 05 '18 at 16:44
  • Crap. I think you're right as usual...and it doesn't look like it allows embedded pipes either. – Paul Hodges Jul 05 '18 at 16:46
  • Scrapped and rewrote it with likely a whole new set of crap, lol – Paul Hodges Jul 05 '18 at 17:04
  • Thank you very much for putting me on the right track Paul Hodges. I need to brush up on my regex knowledge. – mmbecks7 Jul 08 '18 at 09:18