juniper.device.junos_commandおよびjuniper.device.junos_rpc Ansibleモジュールを使用して、コマンドとRPCを実行します
juniper.device.junos_commandおよびjuniper.device.junos_rpc Ansibleモジュールを使用して、Junos OSを実行しているデバイスやJunos OS Evolvedを実行しているデバイス上で動作モードコマンドとRPCを実行できます。
ジュニパーネットワークスは、Junosデバイス上で動作モードコマンドとリモートプロシージャコール(RPC)を実行するために使用できるAnsibleモジュールを提供しています。 表1は 、モジュールの概要を示しています。 juniper.device コレクションの特定のモジュール セットを既に使用している場合は、そのセットのモジュールを使用します。
|
コレクション |
モジュールセット |
モジュール名 |
|---|---|---|
|
|
|
|
|
|
次のセクションでは、 juniper.device.junos_command モジュールと juniper.device.junos_rpc モジュールの使用方法と、モジュール応答で予想される内容について説明します。 表2は 、モジュールでサポートされるアクションを簡単にまとめたものです。
| モジュール | アクション | モジュール引数 |
サポートされているAnsible接続 |
|---|---|---|---|
|
|
1つ以上のCLIコマンドを実行します |
|
|
|
1 つ以上の RPC を実行し、オプションで RPC 引数を含めます |
|
|
|
| evaluate コマンドまたは RPC 出力 |
|
|
|
|
|
RPC を実行し、オプションで RPC の引数と属性を含めます |
|
|
junos_commandモジュールでコマンドを実行する
juniper.device.junos_commandモジュールにより、Junosデバイス上で動作モードコマンドを実行できます。モジュールには 1 つの引数 commands が必要です。引数は 1 つのコマンドまたはデバイス上で実行する 1 つ以上のコマンドのリストです。
次のプレイブックでは、インベントリグループ内の各デバイスで2つのコマンドを実行します。プレイブックは、モジュールの応答を標準出力(stdout)で表示します。
---
- name: Get device information
hosts: junos
gather_facts: no
tasks:
- name: Get version and uptime information
juniper.device.junos_command:
commands:
- show version
- show system uptime
register: junos_result
- name: Print response
ansible.builtin.debug:
var: junos_result
モジュールの応答と出力形式については、「 モジュールの応答について 」および「 コマンドまたは RPC 出力の形式を指定する」を参照してください。
junos_commandまたはjunos_rpcモジュールでRPCを実行する
Junos XML APIは、Junos OS設定ステートメントと動作モードコマンドのXML表現です。Junos OS設定階層のすべてのステートメントと、Junos OS CLIで発行する多くの動作モードコマンドに相当するXMLを定義します。Junos XMLに対応する各動作モードコマンドは、リクエストタグ要素と、必要に応じてレスポンスタグ要素にマッピングされます。リクエストタグは、NETCONFまたはJunos XMLプロトコルセッション内のRPCで、Junosデバイスから情報をリクエストするために使用されます。サーバーは、対応する応答タグ要素内に囲まれた Junos XML 要素を使用して応答を返します。
CLIコマンドのRPCリクエストタグへのマッピングについては、操作タグの Junos XML APIエクスプローラー を参照してください。
juniper.device.junos_commandモジュールとjuniper.device.junos_rpcモジュールにより、Junosデバイス上でRPCを実行できます。junos_commandモジュールでは、インラインRPC引数を含めるオプションを使用して、1つのタスクで複数のRPCを実行できます。junos_rpcモジュールでは、RPC引数と属性を含めるオプションを使用して、単一のRPCを実行できます。次のセクションでは、モジュールを使用してRPCを実行する方法を示します。
junos_commandモジュール
junos_commandモジュールを使用してRPCを実行するには、rpcsパラメーターを含めます。rpcsパラメーターは、単一のRPCリクエストタグまたはRPCリクエストタグのリストにすることができます。オプションで、その RPC でサポートされている任意の数の RPC 引数を含めることができます。引数をkey=valueペアとしてインラインで指定します。次のプレイブックでは、junos_command モジュールを使用して複数の RPC を実行します。
---
- name: Execute multiple RPCs with junos_command
hosts: junos
gather_facts: no
tasks:
- name: Get version, uptime, and interface information
juniper.device.junos_command:
rpcs:
- get-software-information
- get-system-uptime-information
- get-interface-information interface-name=ge-0/0/1 terse=true
register: junos_result
- name: Print response
ansible.builtin.debug:
var: junos_result
junos_rpcモジュール
junos_rpc モジュールを使用して 1 つの RPC を実行するには、rpc パラメーターを含め、実行する RPC を指定します。次のプレイブックでは、インベントリグループ内の各デバイスでget-software-informationRPCを実行し、モジュール応答を標準出力で表示します。RPCは、show versionの運用モードコマンドに相当します。
---
- name: Execute a single RPC with junos_rpc
hosts: junos
gather_facts: no
tasks:
- name: Get version information
juniper.device.junos_rpc:
rpc: get-software-information
register: junos_result
- name: Print response
ansible.builtin.debug:
var: junos_result
引数と属性を持つjunos_rpcモジュール
junos_rpcモジュールは、argsとattrsのオプションをサポートしています。args オプションを使用して、RPC 引数と値のディクショナリを指定します。個々の RPC 引数に値が必要ない場合は、その値を true に等しく設定します。attrsオプションを使用して、RPCの属性のディクショナリを指定します。
RPC 引数名にはハイフンまたはアンダースコアを使用できます。たとえば、 interface-name 引数を interface-name または interface_nameとして表すことができます。
次のプレイブックでは、インベントリグループ内の各デバイスで get-interface-information RPCを実行し、モジュール応答を標準出力で表示します。モジュール引数には、lo0.0インターフェイスのみのterseレベル出力をリクエストする args オプションが含まれています。RPC は、 show interfaces lo0.0 terse 動作モード コマンドに相当します。
---
- name: Execute an RPC with arguments
hosts: junos
gather_facts: no
tasks:
- name: Get device information
juniper.device.junos_rpc:
rpc: get-interface-information
args:
interface_name: lo0.0
terse: true
register: junos_result
- name: Print response
ansible.builtin.debug:
var: junos_result
次のプレイブックでは、インベントリグループ内の各デバイスで <get-configuration database="candidate" inherit="inherit"/> RPCを実行します。 attrs オプションには、 database および inherit RPC属性が含まれます。RPCは、各デバイスの継承後の候補コンフィギュレーションを返します。
---
- name: Execute an RPC with attributes
hosts: junos
gather_facts: no
tasks:
- name: Get the post-inheritance candidate configuration
juniper.device.junos_rpc:
rpc: get-configuration
attrs:
database: candidate
inherit: inherit
register: junos_result
- name: Print response
ansible.builtin.debug:
var: junos_result
モジュールの応答と出力形式については、「 モジュールの応答について 」および「 コマンドまたは RPC 出力の形式を指定する」を参照してください。
コマンドとRPC出力を評価する
junos_commandモジュールでは、タスクの一部として出力を評価できます。指定された条件がfalseの場合、タスクは失敗し、プレイブックはそのデバイスの残りのタスクをスキップします。プレイブックは、デバイス上で残りのタスクを実行するかどうかを判断するために、出力を評価する場合があります。例えば、モデルまたは Junos OS のバージョンが指定された条件に一致しない場合、プレイブックはそのデバイスの後続のインストールまたは設定タスクをスキップする場合があります。
junos_command を使用して、コマンドまたは RPC 出力を評価できます。ただし、以下の要件を満たす必要があります。
-
Ansible接続は
ansible.netcommon.netconfに設定する必要があります。 -
一致条件では、要求された形式に適した構文を使用する必要があります。
wait_for引数を使用して、評価する条件を指定します。複数の条件を定義する場合、match引数を含めて一致ポリシーを定義できます。matchをallに設定して、すべての条件を満たす必要があることを示します。または、条件が1つだけ満たされる必要がある場合は、matchをanyに設定します。
複数のコマンドの出力で条件を評価できます。異なるコマンド出力の条件を定義するには、そのコマンドの結果を参照します。たとえば、最初のコマンドは result[0] 、2番目のコマンドは result[1] などです。
また、任意の出力形式を使用して条件を評価することもできます。ただし、条件ではその形式に適した構文を使用する必要があります。たとえば、テキストやセットの書式には、 contains や matchesなどの文字列演算子を使用します。XMLおよびJSON出力の場合は、適切な辞書パスを指定します。
次のプレイブックでは、 show version コマンドを実行し、コマンド出力(デフォルトはテキスト形式)でモデル番号とバージョン番号を確認します。
---
- name: Validate text output
hosts: junos
gather_facts: no
tasks:
- name: Validate text output for model and version
juniper.device.junos_command:
commands: show version
wait_for:
- result[0] contains mx240
- result[0] contains 23.2R2.21
match: all
タスクには match: allが含まれます。したがって、すべての条件が満たされている場合、タスクは特定のデバイスに対してパスされます。条件が満たされない場合、タスクはエラーで失敗し、プレイブックはそのデバイスの残りのタスクを無視します。
以下の出力では、最初のデバイスが指定されたすべての条件に一致し、タスクは成功します。2台目のデバイスがいずれかの条件に一致しないため、タスクは失敗します。
user@ansible-cn:~$ ansible-playbook junos-command-evaluate-output.yaml
PLAY [Validate text output] ********************************************************
TASK [Validate text output for model and version] **********************************
ok: [198.51.100.1]
[ERROR]: Task failed: Action failed: One or more conditional statements have not been satisfied
[...output omitted...]
fatal: [198.51.100.2]: FAILED! => {"changed": false, "failed_conditions": ["result[0] contains 23.2R2.21"], "msg": "One or more conditional statements have not been satisfied"}
PLAY RECAP *************************************************************************
198.51.100.2 : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
198.51.100.1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
同様に、RPC 出力を評価できます。次のプレイブックでは、同等の RPC を持つ junos_command モジュールを使用して、前の例と同じ評価を実行します。タスクは、デフォルトのRPC出力形式(XML)ではなく、テキスト出力を要求します。
---
- name: Validate text output
hosts: junos
gather_facts: no
tasks:
- name: Validate text output for model and version
juniper.device.junos_command:
rpcs: get-software-information
display: text
wait_for:
- result[0] contains mx240
- result[0] contains 23.2R2.21
match: all
XML 出力を評価するには、適切なディクショナリパスを使用するように wait_for 条件を更新します。
---
- name: Validate XML output
hosts: junos
gather_facts: no
tasks:
- name: Validate XML output for model and version
juniper.device.junos_command:
commands:
- show version
display: xml
wait_for:
# XML is parsed into a dictionary
- result[0]['rpc-reply']['software-information']['product-model'] == 'mx240'
- result[0]['rpc-reply']['software-information']['junos-version'] == '23.2R2.21'
match: all
同様に、JSON 出力の場合:
---
- name: Validate JSON output
hosts: junos
gather_facts: no
tasks:
- name: Validate JSON output for model and version
juniper.device.junos_command:
commands:
- show version
display: json
wait_for:
- result[0]['software-information'][0]['product-model'][0]['data'] == 'mx240'
- result[0]['software-information'][0]['junos-version'][0]['data'] == '23.2R2.21'
match: all
モジュール応答の理解
junos_commandモジュールとjunos_rpcモジュールは、デバイスからのRPC応答をモジュール応答の複数の異なるキー内に格納します。表3は、さまざまなフィールドの概要を示しています。出力は、特定の形式に対してのみ一部のフィールドを返します。
| モジュール | 出力フィールド | 説明 |
|---|---|---|
junos_command |
output |
XML形式の場合のみ、XML応答はJSONに変換されます。 |
stdout |
要求されたコマンドの応答を含むリスト。 |
|
stdout_lines |
stdout 値がリストに分割されます。 |
|
junos_rpc |
output |
RPCは要求された形式で応答します。 |
output_lines |
テキスト形式の場合のみ、テキスト出力は行に分割されます。 | |
xml |
XML RPC 応答文字列。 |
コマンドまたはRPC出力のフォーマットを指定する
junos_command モジュールを使用してコマンドを実行する場合、デフォルトの出力形式はテキストです。junos_command モジュールまたは junos_rpc モジュール を使用して RPC を実行する場合、デフォルトの形式は XML です。
別の出力形式を指定するには、そのモジュールに固有のパラメーターを含め、必要な形式と同じ値を設定します。 表4は 、各モジュールのパラメーター、サポートされている形式、およびデフォルト形式の概要を示しています。 junos_command モジュールを使用して複数のコマンドまたは RPC を実行する場合、指定できる形式は 1 つだけです。
| モジュール |
モジュール |
サポートされている値 |
|---|---|---|
junos_command |
display |
|
junos_rpc |
output |
|
次のプレイブックでは、 junos_command モジュールを使用して、インベントリグループ内の各デバイスで2つのRPCを実行します。プレイブックは、実行されたすべての RPC の出力のテキスト形式を要求します。
---
- name: Get device information
hosts: junos
gather_facts: no
tasks:
- name: Get version and system uptime information
juniper.device.junos_command:
rpcs:
- get-software-information
- get-system-uptime-information
display: text
register: junos_result
- name: Print response
ansible.builtin.debug:
var: junos_result
モジュールの応答では、 stdout キーと stdout_lines キーに、デフォルトのXML出力ではなくテキスト形式でのRPC応答が含まれています。
次のプレイブックでは、 junos_rpc モジュールを使用して、インベントリグループ内の各デバイスでRPCを実行します。プレイブックは、RPC 出力のテキスト形式を要求します。
---
- name: Get device information
hosts: evo
gather_facts: no
tasks:
- name: Get uptime information
juniper.device.junos_rpc:
rpc: get-system-uptime-information
output: text
register: junos_result
- name: Print response
ansible.builtin.debug:
var: junos_result
junos_rpcの場合、モジュールの応答には、output、output_lines、xml キーが含まれます。この場合、モジュールはテキスト形式を要求した場合にのみoutput_linesを返します。
PLAY [Get device information] ******************************************************
TASK [Get uptime information] ******************************************************
ok: [router1.example.com]
TASK [Print response] **************************************************************
ok: [router1.example.com] => {
"junos_result": {
"changed": false,
"failed": false,
"output": "Current time: 2026-03-16 16:25:29 PDT\nTime Source: NTP CLOCK \nNode booted: 2026-01-05 10:18:33 PST (10w0d 05:06 ago)\nSystem booted: 2026-01-05 10:18:57 PST (10w0d 05:06 ago)\nProtocols started: 2026-01-05 10:21:14 PST (10w0d 05:04 ago)\nLast configured: 2026-02-23 17:07:17 PST (2w6d 22:18 ago) by admin\n 4:25PM up 70 days, 5:06, 1 user, load averages: 0.09, 0.09, 0.10",
"output_lines": [
"Current time: 2026-03-16 16:25:29 PDT",
"Time Source: NTP CLOCK ",
"Node booted: 2026-01-05 10:18:33 PST (10w0d 05:06 ago)",
"System booted: 2026-01-05 10:18:57 PST (10w0d 05:06 ago)",
"Protocols started: 2026-01-05 10:21:14 PST (10w0d 05:04 ago)",
"Last configured: 2026-02-23 17:07:17 PST (2w6d 22:18 ago) by admin",
" 4:25PM up 70 days, 5:06, 1 user, load averages: 0.09, 0.09, 0.10"
],
"xml": "<rpc-reply message-id=\"urn:uuid:81f492e2-bce2-4818-b6e7-4675eb4df442\"><output>\nCurrent time: 2026-03-16 16:25:29 PDT\nTime Source: NTP CLOCK \nNode booted: 2026-01-05 10:18:33 PST (10w0d 05:06 ago)\nSystem booted: 2026-01-05 10:18:57 PST (10w0d 05:06 ago)\nProtocols started: 2026-01-05 10:21:14 PST (10w0d 05:04 ago)\nLast configured: 2026-02-23 17:07:17 PST (2w6d 22:18 ago) by admin\n 4:25PM up 70 days, 5:06, 1 user, load averages: 0.09, 0.09, 0.10\n</output></rpc-reply>"
}
}
PLAY RECAP *************************************************************************
router1.example.com : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0