ansible之數據提取與Juniper實例演示


一、Ansible列表兩種表達方式

  基於YAML的列表

my_list:
    - a
    - b
    - c
    - d

 

  基於Json格式的列表

{"my_list":[
    "a",
    "b",
    "c",
  ]
}

 

二、Ansible列表數據的提取方式

  正向列表索引取值

每個列表里的元素,都存在位置,即索引號,可根據列表變量名[索引號]提取,如:my_list[2],取第3個元素,索引號從0開始

  反向列表索引取值

每個列表里的元素,都存在位置,即索引號,反向取值可根據列表變量名[-索引號]提取,如:my_list[-2],取倒數第2個元素,反向索引號從-1開始,即代表最后一個元素

  正反向切片取值

根據my_list[索引1:索引2],可取到索引1到索引2范圍的元素,索引1不填則表示從索引0開始;索引2不填則默認取到最后一個元素
如:my_list=['a','b','c','d','e']
my_list[2:3]  #結果:['c','d'],從第3個元素開始,到第4個元素結尾,不包含第4個元素
my_list[:2]    #結果:['a','b','c'],從第1個元素開始,到第3個元素,不包含第3個元素
my_list[2:]    #結果:['c','d','e'],從第3個元素開始,到最后一個元素,包含最后一個元素
my_list[2:-1]    #結果:['c','d','e'],從第3個元素開始,到最后一個元素,不包含最后一個元素

 

三、Ansible字符串數據提取方式

  字符串提取方法與上面列表一樣

 

四、playbook兩種方法輸出dict字典內容

  基於Jinja2格式輸出

my_dict.key,    如my_dict={"a":'a1',"b":"b1"}
my_dict.a      #輸出a1值

  基於Python格式輸出

my_dict[key],    如my_dict={"a":'a1',"b":"b1"}
my_dict["a"]    #輸出值a1

  

五、案例

  5.1 Juniper收集設備信息以及數據處理

1.juniper初始化配置

  2.ansible主控端:nano group_vars/junos.yaml

---
ansible_connection: local
ansible_network_os: junos
ansible_user: admin
ansible_ssh_pass: juniper123

  3.ansible主控端:nano test_host文件

[junos]
junipervsrx junos_host=192.168.xx.xx

  4.ansible主控端: nano 3_8_3_filter_junos_facts.yaml劇本

---

- name: get junos facts
  hosts: junipervsrx
  gather_facts: no
  roles:
    - Juniper.junos

  tasks:

   - name: get junos facts
     juniper_junos_facts:
       host: "{{junos_host}}"
     register: facts_result

   - name: debug output
     debug:
       msg: "hostname:{{ facts_result.ansible_facts.junos.hostname }},
            model:{{ facts_result.ansible_facts.junos.model }},
            sn:{{ facts_result.ansible_facts.junos.serialnumber }},
            version:{{ facts_result.ansible_facts.junos.version }}"

  結果:

 

   - name: debug output
     debug:
       msg:
       - "hostname:{{ facts_result.ansible_facts.junos.hostname }}"
       - "model:{{ facts_result.ansible_facts.junos.model }}"
       - "sn:{{ facts_result.ansible_facts.junos.serialnumber }}"
       - "version:{{ facts_result.ansible_facts.junos.version }}"
優化msg以列表形式輸出

   結果2:

 

 

  5.2 Juniper路由表信息收集

juniper初始化配置:
1.root%界面下輸入cli
2.root>界面下輸入configure,進入配置模式
3.root#界面下輸入如下配置
  delete system autoinstallation
  set system host-name test-SRX
  set system root-authentication plain-text-password回車  #設置root密碼
  輸入密碼juniper123
  set system login user admin class super-user  #新增用戶admin,並具有所有權限
  set system login user admin authentication plain-text-password
  輸入密碼juniper123
  set system services ssh root-login allow  #開啟ssh服務
  set system services netconf ssh port 830  #開啟netconf服務承載在ssh,端口830
  set interfaces fxp0 unit 0 family inet address 192.168.3.20/24  #配置帶外管理口fxp0地址
  commit  #提交變更
1.Juniper初始化配置

  2.ansible主控端:nano group_vars/junos.yaml

---
ansible_connection: local
ansible_network_os: junos
ansible_user: admin
ansible_ssh_pass: juniper123

  3.ansible主控端:nano test_host文件

[junos]
junipervsrx junos_host=192.168.xx.xx

  4.ansible主控端: nano 3_8_3_filter_junos_facts.yaml劇本

---
- name: get junos routing table
  hosts: junos
  gather_facts: no
  roles:
    - Juniper.junos

  tasks:
    - name: get junos routes
      juniper_junos_command:
        commands: 
          - "show route table inet.0"
        host: "{{junos_host}}"
      register: route_result

    - name: debug output
      debug: 
        msg: "The subnet is {{route_result.stdout_lines[5][0:15]}},
             and the host route is: {{route_result.stdout_lines[7][0:16]}}"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM