LoginSignup
0
1

More than 3 years have passed since last update.

【Ansible×NW自動化】interfaceの状態を確認する(junos_command×json編)

Last updated at Posted at 2020-05-11

はじめに

<バージョン>
ansible 2.9.1

Ansibleのネットワークモジュールに、junos機器のshowコマンドの結果が取得出来るjunos_command
というモジュールがあります。
コマンドの結果はjson形式で出力した方がwhenでの条件分岐などの処理がしやすくなります。
今回は、interfaceの状態(up/down)をjson形式で取得して確認してみます。

Playbook紹介1~json形式の出力確認~

junos_commandのオプションで display: json と記入することにより、
json形式でコマンド結果を出力することが出来ます。

int_json_test1.yml
---
- name: int json TEST
  hosts: junos
  gather_facts: no
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:
    - name: get command
      junos_command:
        commands: show interfaces
        display: json    # ポイント
      register: results

    - name: fact_json1
      set_fact:
        int_results_json: "{{ results.stdout[0] }}"

    - name: debug
      debug:
        msg: "{{ int_results_json }}"

実行結果1

interfaceの情報は、interface-information.physical-interface配下に格納されています。(下図、チェック(1))
さらに、その中でinterfaceの状態を表すadmin-statusとoper-statusの部分を取得します。(下図、チェック(2))

出力1
[ec2-user@ip-<addr> ansible]$ ansible-playbook -i hosts int_json_test1.yml

PLAY [int json TEST] ************************************************************************************************************************************************************************

TASK [get command] *****************************************************************************************************************************************************************************
ok: [junos_router_1]

TASK [fact_json1] ***************************************************************************************************************************************************************************
ok: [junos_router_1]

TASK [debug] ********************************************************************************************************************************************************************************
ok: [junos_router_1] => {
    "msg": {
        "interface-information": [
            {
                "physical-interface": [    # チェック(1)
                    {
                        "admin-status": [
                            {
                                "attributes": {
                                    "junos:format": "Enabled"
                                },
                                "data": "up"      # チェック(2)
                            }
                        ],
~中略~
                        "name": [
                            {
                                "data": "cbp0"    # interface名
                            }
                        ],
                        "oper-status": [
                            {
                                "data": "up"      # チェック(2)
                            }
~以下、省略~

Playbook紹介2~interfaceの状態確認~

with_itemsでinterfaceの情報を一つずつ取得していきます。

ポイント
(1)欲しい情報(今回のadmin-statusとoper-status)が深い階層にある場合は、いったんset_factで
  上の階層の情報を変数に格納した方がPlaybookを書きやすくなる。
(2)loop_controlでlabelを指定すると、出力結果がスマートになります。
<補足>with_itemsでループさせて取り出す変数はitemに格納されますが、itemの中身が多い場合はansible実行時の出力が多くなってしまう
(3)with_itemsで取り出した内容もvarsで変数に代入できるので、ここで宣言しなおすと見やすくなります。

int_json_test2.yml
---
- name: int json TEST
  hosts: junos
  gather_facts: no
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:
    - name: get command
      junos_command:
        commands: show interfaces
        display: json
      register: results

    - name: fact_json1
      set_fact:
        int_results_json: "{{ results.stdout[0] }}"

    - name: fact_json2    # ポイント(1)
      set_fact:
         int_results: "{{ int_results_json['interface-information'][0]['physical-interface'] }}"

    - name: debug
      debug:
        msg: "admin_status : {{ admin_status }} / oper_status : {{ oper_status }}"
      with_items: "{{ int_results }}"
      loop_control:
        label: "{{ intf_name }}"    # ポイント(2)
      vars:    # ポイント(3)
        intf_name: "{{ item['name'][0]['data'] }}"
        admin_status: "{{ item['admin-status'][0]['data'] }}"
        oper_status: "{{ item['oper-status'][0]['data'] }}"

実行結果2

interfaceの状態が取得出来ていることがわかります。
また、ポイント(2)でラベルを指定したのでitemの内容がintf_nameになっています。

出力2
[ec2-user@ip-172-31-44-156 ansible]$ ansible-playbook -i hosts int_json_test2.yml

PLAY [int json TEST] ************************************************************************************************************************************************************************

TASK [get command] *****************************************************************************************************************************************************************************
ok: [junos_router_1]

TASK [fact_json1] ***************************************************************************************************************************************************************************
ok: [junos_router_1]

TASK [fact_json2] ***************************************************************************************************************************************************************************
ok: [junos_router_1]

TASK [debug] ********************************************************************************************************************************************************************************
ok: [junos_router_1] => (item=cbp0) => {    # ポイント(2)のおかげで出力がスマートに!!
    "msg": "admin_status : up / oper_status : up"
}
ok: [junos_router_1] => (item=demux0) => {
    "msg": "admin_status : up / oper_status : up"
}
ok: [junos_router_1] => (item=dsc) => {
    "msg": "admin_status : up / oper_status : up"
}
~以下、省略~

PLAY RECAP **********************************************************************************************************************************************************************************
junos_router_1             : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

関連記事

【Ansible×NW自動化】interfaceの状態を確認する(junos_facts編)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1