Ansible and regex_search

Viewed 1546

I'm currently struggling to have a working playbook using regex_search, I'm trying to have the playbook go through the stdout of a network command (show run vlan ID) and then go through a regex expression which will extract all the interface tagged,

ultimately that variable would be use to deploy a new vlan and match the configuration of the existing working vlan

here is the code I'm trying however that return the following error;

fatal: [10.163.199.131]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{ results.stdout.lines | regex_search(regexp,'\\1') }}): expected string or buffer"}

here is the output of the STDOUT

TASK [debug] ********************************************************************************************************************************************************************************************************************************
ok: [10.163.199.131] => {
    "msg": {
        "ansible_facts": {
            "discovered_interpreter_python": "/usr/bin/python"
        },
        "changed": false,
        "failed": false,
        "stdout": [
            "vlan 1000 name Guest_WiFi by port\n tagged ethe 1/1/1 to 1/1/9 ethe 1/1/11 to 1/1/25 ethe 1/1/27 to 1/1/44 ethe 1/2/2 \n router-interface ve 1000\n!\n!"
        ],
        "stdout_lines": [
            [
                "vlan 1000 name Guest_WiFi by port",
                " tagged ethe 1/1/1 to 1/1/9 ethe 1/1/11 to 1/1/25 ethe 1/1/27 to 1/1/44 ethe 1/2/2 ",
                " router-interface ve 1000",
                "!",
                "!"

I'm trying to extract from the above STDOUT the following;

1/1/1 to 1/1/9 ethe 1/1/11 to 1/1/25 ethe 1/1/27 to 1/1/44 ethe 1/2/2

and here is my current playbook;

- hosts: icx
  vars:
    vlan_lookup: 1000
    vlan_to_config: 1001
    vlan_name: TestVlan
    

  tasks:
    - name: Show output of vlan {{ vlan_lookup }}
      icx_command:
        commands: 
           - show run vlan {{ vlan_lookup }}
      register: results
    - set_fact:
        myvalue: "{{ results.stdout.lines | regex_search(regexp,'\\1') }}"
      vars:
        regexp: '\[1-9]+\D\w+\D'
    - debug:
        var: '{{myvalue}}'
1 Answers

I think you need to use regex_findall and not regex_search. Note that, when you are trying to parse stdout_lines its a list of lines of stdout. So you would have to convert into string by using string filter.

- set_fact:
    myvalue: "{{ results.stdout_lines|string|regex_findall('\\d+\/\\d+\/\\d+.*\\d+\/\\d+\/\\d+') }}"

- debug: 
    var: myvalue

The above task would display, you can change the regex as per requirement:

TASK [set_fact] ********************************************************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [debug] ***********************************************************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "myvalue": [
        "1/1/1 to 1/1/9 ethe 1/1/11 to 1/1/25 ethe 1/1/27 to 1/1/44 ethe 1/2/2"
    ]
}
Related