【问题标题】:Extract substring from variable in Ansible从 Ansible 中的变量中提取子字符串
【发布时间】:2017-09-30 13:37:05
【问题描述】:

编辑: 我写了这个剧本,但它没有显示提取的变量:

---
- 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 }}"

执行:

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:~$

【问题讨论】:

  • 先尝试打印 'rid' - Ansible 抱怨说 'rid' 没有名为 'output' 的属性。您可能有一个 json 字符串,而不是一个数据结构?

标签: regex ansible


【解决方案1】:

我找到了解决办法:

---
- 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 }}"

执行:

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   

【讨论】:

    【解决方案2】:

    其他解决方案,更合乎逻辑:

    ---
    - 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 }}"
    

    执行:

    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   
    

    【讨论】:

      猜你喜欢
      • 2020-01-08
      • 2020-01-12
      • 2020-11-07
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      相关资源
      最近更新 更多