はじめに
<バージョン>
ansible 2.9.1
Ansibleのネットワークモジュールに、junos機器の情報が取得出来るjunos_factsというモジュール
があります。interfaceの状態確認であるならshow interfaces descriptionsの結果から確認した方が
良いと思いますが、Playbookを書く練習としてjunos_factsを使ってみることにしました。
Playbook紹介1~junos_factsの出力確認~
junos_factsの結果は、ansible_factsという変数に格納されます。
---
- name: Junos Module TEST
hosts: junos
gather_facts: no
vars:
ansible_python_interpreter: /usr/bin/python3
tasks:
- name: get fact
junos_facts:
- name: debug
debug:
msg: "{{ ansible_facts }}"
実行結果1
interfaceの情報は、ansible_facts.net_interfaces配下に格納されています。(下図、チェック(1))
さらにその中でinterfaceの状態を表すadmin-statusとoper-statusの部分を取得します。(下図、チェック(2))
[ec2-user@ip-<addr> ansible]$ ansible-playbook -i hosts fact_test1.yml
PLAY [Junos Module TEST] ********************************************************************************************************************************************************************
TASK [get fact] *****************************************************************************************************************************************************************************
[WARNING]: default value for `gather_subset` will be changed to `min` from `!config` v2.11 onwards
ok: [junos_router_1]
TASK [debug] ********************************************************************************************************************************************************************************
ok: [junos_router_1] => {
"msg": {
"net_api": "netconf",
"net_filesystems": [
"/dev/gpt/junos",
"tmpfs",
"tmpfs"
],
"net_gather_network_resources": [],
"net_gather_subset": [
"hardware",
"default",
"interfaces"
],
"net_has_2RE": false,
"net_hostname": "<ip-addr>",
"net_interfaces": { ←チェック(1)
".local.": {
"admin-status": "up", ←チェック(2)
"macaddress": "Unspecified",
"mtu": "Unlimited",
"oper-status": "up", ←チェック(2)
"speed": "Unlimited",
"type": "Loopback"
},
"cbp0": {
"admin-status": "up",
"macaddress": "<mac-addr>",
"mtu": "9192",
"oper-status": "up",
"speed": "Unspecified",
"type": "Ethernet"
~以下、省略~
Playbook紹介2~interfaceの状態確認~
with_itemsでinterfaceの情報を一つずつ取得していきます。
ポイント
(1)欲しい情報(今回のadmin-statusとoper-status)が深い階層にある場合は、いったんset_factで
上の階層の情報を変数に格納した方がPlaybookを書きやすくなる
(2)interfaceの情報は辞書型で格納されているので、各々のinterface名をint_results.keys()で
取得してリストに格納する
(3)with_itemsでループさせて取り出す変数はitemという変数に格納されていますが、
さらにそれをvarsで変数に格納するとスマートに書けます
---
- name: Junos Module TEST
hosts: junos
gather_facts: no
vars:
ansible_python_interpreter: /usr/bin/python3
tasks:
- name: get fact
junos_facts:
- name: set fact
set_fact:
int_results: "{{ ansible_facts.net_interfaces }}" #ポイント(1)
- name: debug
debug:
msg: "admin_status : {{ admin_status }} / oper_status : {{ oper_status }}"
with_items: "{{ int_results.keys() | list }}" #ポイント(2)
vars:
admin_status: "{{ int_results[item]['admin-status'] }}" #ポイント(3)
oper_status: "{{ int_results[item]['oper-status'] }}"
実行結果2
interfaceの状態が取得出来ていることがわかります。
追加機能として、whenを使ってdownの時だけdebugに出力するなどの処理も可能です。
[ec2-user@ip-<addr> ansible]$ ansible-playbook -i hosts fact_test2.yml
PLAY [Junos Module TEST] ********************************************************************************************************************************************************************
TASK [get fact] *****************************************************************************************************************************************************************************
[WARNING]: default value for `gather_subset` will be changed to `min` from `!config` v2.11 onwards
ok: [junos_router_1]
TASK [set fact] *****************************************************************************************************************************************************************************
ok: [junos_router_1]
TASK [debug] ********************************************************************************************************************************************************************************
ok: [junos_router_1] => (item=demux0) => {
"msg": "admin_status : up / oper_status : up"
}
ok: [junos_router_1] => (item=tap) => {
"msg": "admin_status : up / oper_status : up"
}
ok: [junos_router_1] => (item=vtep) => {
"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