top of page

WSL上のUbutuからコンテナのAnsibleによるネットワーク機器のコンフィグ取得

  • ccf代表
  • 1 日前
  • 読了時間: 3分

目的

普段はMacを使って、コンテナ操作をするときだけLinuxにSSHして作業していますが、他の社員の環境はWindowsのため、Windows環境でansibleを使う場合の手順を整理しました。


想定する環境

Windows 10上のWSL2で、Ubuntuを動かし、dokcer-ceをインストールし、コンテナ化したansibleを動かします。そして、Yamaha SWXからコンフィグを取得することを目的としてます。


参考にしたサイト


必要な作業工程

以下の作業が必要です。

  1. Windows 11上にWSLをインストールし、ubuntuを動かす

  2. dockerfileを作成する

  3. ansible.cfgを作成する

  4. inventoryファイルとplaybookファイルを作成する

  5. aliasを定義する

  6. イメージをビルドする

  7. ansilbe-playbookコマンドでコンフィグを取得する


具体的な手順

1.Windows11上にWSLをインストールし、ubuntuを動かす

ここは他のネットでも手順があったと思うので、割愛します。


2.dockerfileを作成する

satken2さんの記事を参考にDockerfileを作成しました。今回はYamaha SWXを対象にしたため、Yamaha SWXは最新のPython3.12(Ansibleも最新版)ではうまく動かなかったため、3.8系を使用しました。

FROM python:3.8.20-slim-bullseye

RUN install pip --upgrade
RUN apt-get update -y
RUN pip install ansible paramiko

RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
    sshpass

WORKDIR /work

# Yamaha SWX用のAnsible Module追加
RUN ansible-galaxy collection install yamaha_network.swx

3.ansible.cfgを作成する

SSH接続するときにSSH Keyの確認が動いてしまうので、それを無視するために以下の設定を入れています。

[defaults]
host_key_checking = False

[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null

[paramiko_connection]
host_key_auto_add = True

4.inventoryファイルとplaybookファイルを作成する

Yamahaさんのサイトから丸パクリです。

---
- hosts: swx3100
  connection: network_cli

  tasks:
  - name: get configuration
    yamaha_network.swx.swx_command:
      commands:
        - show running-config
    register: result

  - name: debug
    debug:
      msg: "{{ result.stdout_lines[0] }}"

  vars:
    ansible_network_os: yamaha_network.swx.swx
    ansible_user: xxx
    ansible_ssh_pass: xxx
    ansible_become: xxx
    ansible_become_password: xxx

5.aliasを定義する

satken2さんの記事を参考にしました。

alias ansible-playbook='docker run -v "${PWD}":/work:ro -v ~/.ansible/roles:/root/.ansible/roles --rm ansible-yamaha:latest ansible-playbook'

6.イメージをビルドする

docker build . -t ansible-yamaha:latest

7.ansilbe-playbookコマンドでコンフィグを取得する

aliasのお陰で、ほぼ同じコマンドをコンテナに実行できます。

ansible-playbook -i inventory swx.yml

以下のように表示されれば、OKです。

[WARNING]: Collection yamaha_network.swx does not support Ansible version
2.13.13

PLAY [swx3100] *****************************************************************

TASK [Gathering Facts] *********************************************************
[WARNING]: Collection ansible.netcommon does not support Ansible version
2.13.13
[WARNING]: Collection ansible.utils does not support Ansible version 2.13.13
[WARNING]: ansible-pylibssh not installed, falling back to paramiko
ok: [192.168.xxx.xxx]

TASK [get configuration] *******************************************************
[WARNING]: Collection ansible.netcommon does not support Ansible version
2.13.13
[WARNING]: Collection ansible.utils does not support Ansible version 2.13.13
ok: [192.168.xxx.xxx]

TASK [debug] *******************************************************************
[WARNING]: Collection ansible.netcommon does not support Ansible version
2.13.13
[WARNING]: Collection ansible.utils does not support Ansible version 2.13.13
ok: [192.168.xxx.xxx] => {
    "msg": [
        "!",
        "password-encryption enable",
        "!",
        "enable password 8 password,
        "logging host 192.168.xxx.xxx",
        "logging trap debug",
        "!",
        "username ccf password 8 password,
        "!",
        "dns-client enable",
        "dns-client name-server 8.8.8.8",
        "!",
        "dhcp-server enable",
(中略)
        "!",
        "line con 0",
        "line vty 0 7",
        "!",
        "l2ms configuration",
        " l2ms role master",
        "!",
        "end"
    ]
}

PLAY RECAP *********************************************************************
192.168.xxx.xxx              : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

注意点

案の定少しハマりました。wslのubuntuへログインすると、Windows側のホームディレクトリにいます。この場所だと、権限の問題(Windows側のディレクトリのパーミッションが777)でカレントディレクトリにあるansible.cfgを読んでくれません。必ず、/home/root的なフォルダに移動してから作業がしてください。



Comments


bottom of page