-1

Ansible で networktocode ntc-ansible モジュールを使用して、Cisco IOS デバイス (現在はスイッチ) を制御しています。ntc_show_Command を使用して「show version」と「show ip int brief」を取得し、結果をローカル ファイルに保存することに成功しました。しかし、ansible-playbook コマンドの最後に -vvv を使用すると、ターミナルに構造化された JSON 出力が表示されます。ntc_show_command から「module_args」と「response」にアクセスするにはどうすればよいですか。「show ip int brief」を使用して、int gi1/0/5 のステータスを知りたい場合。どうすればアクセスできますか? そして...探している特定のデータを取得するために、どのプレイブックコマンドを使用できますか?

これは、-vvv を指定してプレイブックを実行したときに表示される出力ですが、構造化データにアクセスする方法がわかりません

ok: [VTgroup_SW] => {
    "changed": false,
    "invocation": {
        "module_args": {
            "command": "show ip interface brief",
            "connection": "ssh",
            "connection_args": {},
            "delay": "1",
            "file": null,
            "global_delay_factor": "1",
            "host": "x.x.x.x",
            "index_file": "index",
            "key_file": null,
            "local_file": "verification/192.168.2.101.cfg",
            "optional_args": {},
            "password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "platform": "cisco_ios",
            "port": null,
            "provider": null,
            "secret": null,
            "template_dir": "/home/melshman/.ansible/plugins/modules/ntc- 
ansible/ntc-templates/templates",
            "trigger_device_list": null,
            "use_keys": false,
            "use_templates": true,
            "username": "admin"
        }
    },
    "response": [
        {
            "intf": "Vlan1",
            "ipaddr": "y.y.y.y",
            "proto": "down",
            "status": "administratively down"
        },
        {
            "intf": "Vlan2",
            "ipaddr": "x.x.x.x",
            "proto": "up",
            "status": "up"
        },
TRUNCATED...

ローカル ファイルの Show ip int brief の出力を次に示します。

Interface              IP-Address      OK? Method Status                
Protocol
Vlan1                  172.16.x.xxx    YES NVRAM  administratively down down
Vlan2                  192.168.x.xxx   YES NVRAM  up                    up
Vlan10                 10.10.yy.xxx      YES NVRAM  administratively down down

これが私のプレーです...

    - name: VERIFY INTERFACES
      ntc_show_command:
        connection: ssh
        platform: "cisco_ios"
        command: 'show ip interface brief'
        delay: 1 # delay before performing actions / commands in seconds
        global_delay_factor: 1 # delay between commands
        local_file: verification/{{ ansible_host }}.cfg
        host: "{{ ansible_host }}"
        username: "{{ ansible_user }}"
        password: "{{ ansible_ssh_pass }}"
        use_templates: True
        template_dir: '/home/melshman/.ansible/plugins/modules/ntc- 
  ansible/ntc-templates/templates'

更新私はそのような提案に基づいてコードを更新しました:

私は進歩しましたが、まだ何か重要なものが欠けているようです。私の最初のデバッグ ステートメント (以下) は、完全な出力 (ここには示されていません) を出力していますが、これは機能します。set_fact を「interfaces」に正常に設定できます。インターフェイス変数をループして、when ステートメントで設定された条件を検出できます。これを変更して、すべての up/up インターフェイスを検索します。しかし...私の出力(以下に示すのは、あなたが示した詳細を示していません。私の出力はすべて(item = None)を示し、条件に一致するアイテムにはOKを示していますが、出力が示す詳細は示していません(2番目のコードブロック元の回答).
UPDATED - デバッグ変数を 'item.intf' に変更したところ、intf 名を取得できましたが、まだすべての詳細ではありません。

**質問: 基準を満たすインターフェースのみを示す出力を取得するにはどうすればよいですか? この構造化データをファイルに保存できますか?

更新 2::: copy モジュールを使用して {{ インターフェイス }} を構造化データとしてファイルにコピーできましたが、残りのデータではなくインターフェイスのみを表示しようとすると、{{ インターフェイスを使用します.intf }} エラーが発生します。(下記参照)

質問: インターフェイス名のみを示す出力/ファイルを取得するにはどうすればよいですか?
質問: up/up のインターフェース名のみを示す出力/ファイルを取得するにはどうすればよいですか?

私の 2 番目のデバッグ ステートメント (以下) は、これを出力しています。これは、アップ/アップ インターフェイスを識別します: UPDATED

skipping: [VTgroup_SW] => (item=None)
ok: [VTgroup_SW] => (item=None) => {
    "item.intf": "Vlan2"
}
skipping: [VTgroup_SW] => (item=None)
skipping: [VTgroup_SW] => (item=None)
skipping: [VTgroup_SW] => (item=None)
ok: [VTgroup_SW] => (item=None) => {
    "item.intf": "GigabitEthernet1/0/27"

プレイブックUPDATE 2への追加

  - debug:
      var: output

  - set_fact:
      interfaces: "{{ output.response }}"

# shows all the intf that are up/up
  - debug:
      var: item.intf
    when:
      - '"up" in item.proto'
      - '"up" in item.status'
    loop: "{{ interfaces }}"


  - name:  Structured Data - INTERFACES
    copy:
      content:  "{{ interfaces }}"
      dest: "verification/interfaces_{{ansible_host}}.txt"

# This causes error (see traceback)
  - name:  Structured Data - INTERFACES with .intf
    copy:
      content:  "{{ interfaces.intf }}"
      dest: "verification/interfaces_{{ansible_host}}.txt"

プレイからのアウトプット; - 名前: 構造化データ - インターフェース (TRUNCATED)

[{"status": "administratively down", "intf": "Vlan1", "ipaddr": "172.16.0.253", "proto": "down"}, {"status": "up", "intf": "Vlan2", "ipaddr": "192.168.2.101", "proto": "up"}, {"status": "administratively down", "intf": "Vlan10", "ipaddr": "10.10.10.1", "proto": "down"}, {"status": "administratively down", "intf": "Vlan20", "ipaddr": "10.10.20.1", "proto": "down"}, {"status": "administratively down", "intf": "Vlan51", "ipaddr": "192.168.1.1", "proto": "down"}, ---- TRUNCATED

最後のプレイでトレースバック。- 名前: 構造化データ - .intf とのインターフェース

TASK [Structured Data - INTERFACES] ************************************************************************************
task path: /home/melshman/projects/ansible/from-kbyer-ios-pb.yml:117
fatal: [VTgroup_SW]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The error was: 'list object' has no attribute 'intf'\n\nThe error appears to have been in '/home/melshman/projects/ansible/from-kbyer-ios-pb.yml': line 117, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n  - name:  Structured Data - INTERFACES\n    ^ here\n"
4

1 に答える 1