てくなべ (tekunabe)

ansible / network automation / 学習メモ

[Ansible] つまずきながら進める Ansible 【Part1】ふりかえり

はじめに

2020/05/16 に、YouTube Live でつまずいきながら進める Ansible 【Part1】という配信をしました。 実際に作業しながらエラーと戦ってすすめるものです。

とりあえず目指したのは以下のアンケートで一番多かった、show コマンドの結果をファイルで保存する Playbook です。

Part 1 では、Ansible のインストールからはじめました。 環境は、CentOS 8.0 を利用しました。

つまずいたエラーと原因、対処をふりかえります。

動画

www.youtube.com


■ 検証環境 VM への SSH 接続 (Ansible 以前の以前の問題・・)

VS Code Remote - SSH で接続で fingerprint のエラーになる

原因

VM ごとを消して再作成したら fingerprint が変更されて、known_hosts に記録されていたものと不一致になっていたため。

対処

known_hosts を削除した(乱暴)。

■ venv の作成 と ansible のインストール

python -m venv ansiblepython コマンドがないというエラーになる

原因

RHEL8/CentOS 8 でシステムに組み込まれてるのは /usr/libexec/platform-python であって、python コマンドは利用できないため

対策

dnf install python3 で別途インストール。(一応、システムに組み込みのものとは別にする)

■ Playbook 実行

paramiko is not installed: No module named 'paramiko' というエラーになる

原因

ネットワーク機器に SSH 接続するときに必要なライブラリ paramiko がインストールされていないなめ

詳細: [Ansible] エラー「paramiko is not installed: No module named 'paramiko'」の原因と対策 - てくなべ (tekunabe)

対処

pip install paramiko でインストール

fingerprint エラーになる

原因

初回接続の相手であり、known_hosts に載っていないため

対処

ansible.cfg に以下のように設定する。(検証環境のため、簡易的な対処)

[defaults]
host_key_checking=False

設定が意図通り変更されたかどうかは ansible-config dump --only-changed で確認する。

詳細: [Ansible] デフォルトから変更されている設定項目を確認する方法(ansible-config dump --only-changed) - てくなべ (tekunabe)

ios_command モジュール実行時に unsupported parameters というエラーになる

    - name: test
      ios_command:
        command:
          - show ip route

というタスクに対して以下のエラー。

"Unsupported parameters for (ios_command) module: command Supported parameters include: auth_pass, authorize, commands, host, interval, match, password, port, provider, retries, ssh_keyfile, timeout, username, wait_for"

原因

オプション名は command ではなく、commands が正しい

対処

以下に修正

    - name: test
      ios_command:
        commands:
          - show ip route


Part 2にむけて

いきなり VM への接続で予想外につまずいてあせりました・・。

show ip route コマンドの実行結果を、ファイルに保存しましたが、以下のように、改行もされず、余計なものが入っている状態でした。これをもう少し欲しい状態にします。

{"changed": false, "stdout": ["Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP\n       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area \n       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2\n       E1 - OSPF external type 1, E2 - OSPF external type 2\n       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2\n       ia - IS-IS inter area, * - candidate default, U - per-user static route\n       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP\n       a - application route\n       + - replicated route, % - next hop override, p - overrides from PfR\n\nGateway of last resort is 192.168.1.1 to network 0.0.0.0\n\nS*    0.0.0.0/0 [1/0] via 192.168.1.1\n      10.0.0.0/8 is variably subnetted, 4 subnets, 2 masks\nC        10.0.0.0/24 is directly connected, GigabitEthernet0/3\nL        10.0.0.1/32 is directly connected, GigabitEthernet0/3\nC        10.255.255.1/32 is directly connected, Loopback0\nO        10.255.255.2/32 [110/2] via 10.0.0.2, 05:24:25, GigabitEthernet0/3\n      172.16.0.0/16 is variably subnetted, 2 subnets, 2 masks\nC        172.16.1.0/24 is directly connected, GigabitEthernet0/1\nL        172.16.1.253/32 is directly connected, GigabitEthernet0/1\n      192.168.1.0/24 is variably subnetted, 2 subnets, 2 masks\nC        192.168.1.0/24 is directly connected, GigabitEthernet0/0\nL        192.168.1.11/32 is directly connected, GigabitEthernet0/0"], "stdout_lines": [["Codes: L - local, C - connected, S - static, R - RIP, M - mobile, B - BGP", "       D - EIGRP, EX - EIGRP external, O - OSPF, IA - OSPF inter area ", "       N1 - OSPF NSSA external type 1, N2 - OSPF NSSA external type 2", "       E1 - OSPF external type 1, E2 - OSPF external type 2", "       i - IS-IS, su - IS-IS summary, L1 - IS-IS level-1, L2 - IS-IS level-2", "       ia - IS-IS inter area, * - candidate default, U - per-user static route", "       o - ODR, P - periodic downloaded static route, H - NHRP, l - LISP", "       a - application route", "       + - replicated route, % - next hop override, p - overrides from PfR", "", "Gateway of last resort is 192.168.1.1 to network 0.0.0.0", "", "S*    0.0.0.0/0 [1/0] via 192.168.1.1", "      10.0.0.0/8 is variably subnetted, 4 subnets, 2 masks", "C        10.0.0.0/24 is directly connected, GigabitEthernet0/3", "L        10.0.0.1/32 is directly connected, GigabitEthernet0/3", "C        10.255.255.1/32 is directly connected, Loopback0", "O        10.255.255.2/32 [110/2] via 10.0.0.2, 05:24:25, GigabitEthernet0/3", "      172.16.0.0/16 is variably subnetted, 2 subnets, 2 masks", "C        172.16.1.0/24 is directly connected, GigabitEthernet0/1", "L        172.16.1.253/32 is directly connected, GigabitEthernet0/1", "      192.168.1.0/24 is variably subnetted, 2 subnets, 2 masks", "C        192.168.1.0/24 is directly connected, GigabitEthernet0/0", "L        192.168.1.11/32 is directly connected, GigabitEthernet0/0"]], "failed": false}