Extract substring from variable in Ansible

36,265

Solution 1

I found the solution:

---
- hosts: fppc
  gather_facts: false
  remote_user: xyz
  connection: local

  tasks:
  - name: N1
    ios_command:
       commands:
         - sh run         
    register: sr

  - set_fact:
      temp: "{{ sr.stdout_lines | join }}"

  - set_fact:
      rid: "{{  temp | regex_replace('.*ID: (..).*', '\\1')  }}"

  - debug: msg="{{ rid }}"

Execution:

ansible@Ansible:~$ ansible-playbook pb1.yml

PLAY [fppc] ********************************************************************

TASK [N1] **********************************************************************
ok: [192.168.250.161]

TASK [set_fact] ****************************************************************
ok: [192.168.250.161]

TASK [set_fact] ****************************************************************
ok: [192.168.250.161]

TASK [debug] *******************************************************************
ok: [192.168.250.161] => {
    "msg": "A4"
}

PLAY RECAP *********************************************************************
192.168.250.161            : ok=4    changed=0    unreachable=0    failed=0   

Solution 2

Other solution, more logical:

---
- hosts: fppc
  gather_facts: false
  remote_user: xyz
  connection: local

  tasks:
  - name: N1
    ios_command:
       commands:
         - sh run         
    register: sr

  - set_fact: 
      rid: "{{ sr.stdout | regex_search('(?<=ID:)\\s+\\S+', multiline=True) | trim }}"

  - debug: msg="{{ rid }}"

Execution:

ansible@Ansible:~$ ansible-playbook pb1.yml

PLAY [fppc] ********************************************************************

TASK [N1] **********************************************************************
ok: [192.168.250.161]

TASK [set_fact] ****************************************************************
ok: [192.168.250.161]

TASK [debug] *******************************************************************
ok: [192.168.250.161] => {
    "msg": "A4"
}

PLAY RECAP *********************************************************************
192.168.250.161            : ok=3    changed=0    unreachable=0    failed=0   
Share:
36,265
Mahdi ABEDI
Author by

Mahdi ABEDI

Updated on October 22, 2020

Comments

  • Mahdi ABEDI
    Mahdi ABEDI over 3 years

    Edited: I wrote this playbook but it does not show the extracted variable:

    ---
    - hosts: fppc
      gather_facts: false
      remote_user: xyz  
      connection: local
      tasks:
      - name: N1
        ios_command:
           commands:
             - sh run | i bann          
        register: sr
    
      - debug: msg="{{ sr.stdout}}"
    
      - set_fact: 
          rid: "{{ sr.stdout | regex_search('.*ID: (..)')  }}"
    
      - debug: msg="{{ rid }}"
    

    Execution:

    ansible@Ansible:~$ ansible-playbook pb1.yml
    
    PLAY [fppc] *************************************************************************
    
    TASK [N1] ***************************************************************************
    ok: [192.168.250.161]
    
    TASK [debug] ************************************************************************
    ok: [192.168.250.161] => {
        "msg": [
            "banner login ^CID: A4"
        ]
    }
    
    TASK [set_fact] *********************************************************************
    fatal: [192.168.250.161]: FAILED! => {"failed": true, "msg": "Unexpected templating type error occurred on ({{ sr.stdout | regex_search('.*ID: (..)')  }}): expected string or buffer"}
            to retry, use: --limit @/home/ansible/pb1.retry
    
    PLAY RECAP **************************************************************************
    192.168.250.161            : ok=2    changed=0    unreachable=0    failed=1   
    
    ansible@Ansible:~$