AnsibleでNexus9のbackupとlog取得
CentOS8にAnsiblesをインストール--->Ansible起動--->Nexus9kのログ取得まで
NXOSはCLIとNX-APIの接続方法が可能。ここではCLIを試してみる。
やること
- CentOS8のreleas確認
- PKGのupdate
- Python3のインストールとPIPのupgrade
- RPM追加
- リポジトリ確認
- Ansiblesインストール
- Ansibleバージョンと確認
- SSH起動と確認
- Ansible実行groupの作成と確認
- userをAnsible実行groupのメンバーにする
- Ansible実行ユーザのSSH鍵ペアを生成
- Ansiblesサーバで鍵が生成されていることを確認
- Nexus9kでSCPを有効にする(scp以外の方法で転送する場合は不要)
- Nexus9kでユーザを作成しnetwork-admin権限を与える
- Ansibleサーバ公開鍵ファイルをNexus9kへ転送
- Nexus9k側に公開鍵ファイルが転送されていることを確認
- Nexus9kで公開鍵のパスを指定
- AnsibleサーバからNexus9kへパスワード無しでssh可能なことを確認
- AnsibleサーバでAnsible用のhosts(インベントリファイル)を編集
- AnsibleサーバからAnsiblePINGを確認
- configバックアップ用ymlを作成、実行、出力ファイル確認
- log出力用ymlを作成、を実行、確認
CentOS8のreleas確認
[root@localhost ~]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core)
PKGのupdate
※所要数分(インターネット回線速度に依存)
sudo dnf update -y ---snip
Python3のインストールとPIPのupgrade
[root@localhost ~]# dnf -y install python36 Last metadata expiration check: 0:41:09 ago on Sat 15 Feb 2020 01:33:50 AM EST. Package python36-3.6.8-2.module_el8.1.0+245+c39af44f.x86_64 is already installed. Dependencies resolved. Nothing to do. 98% | 100% || 1.4MB 412kB/s ---snip [root@localhost ~]# pip3 install --upgrade pip Installing collected packages: pip Successfully installed pip-20.0.2
RPM追加
※CentOSからyum
がdnf
に変わった(yumもシンボリックリンクになっていて利用可能)
[root@localhost ~]# dnf -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm Last metadata expiration check: 0:43:05 ago on Sat 15 Feb 2020 01:33:50 AM EST. epel-release-latest-8.noarch.rpm 22 kB/s | 21 kB 00:00 ---snip Complete!
リポジトリ確認
[root@localhost ~]# dnf repolist Extra Packages for Enterprise Linux 8 - x86_64 657 kB/s | 5.8 MB 00:08 Last metadata expiration check: 0:00:09 ago on Sat 15 Feb 2020 02:17:13 AM EST. repo id repo name status AppStream CentOS-8 - AppStream 5,093 BaseOS CentOS-8 - Base 2,080 *epel Extra Packages for Enterprise Linux 8 - x86_64 4,805 extras CentOS-8 - Extras 3
Ansiblesインストール
[root@localhost ~]# sudo dnf install --enablerepo epel-playground ansible Extra Packages for Enterprise Linux 8 - Playground - x86_64 393 kB/s | 4.6 MB 00:11 Last metadata expiration check: 0:00:05 ago on Sat 15 Feb 2020 02:17:44 AM EST. Dependencies resolved. ================================================================================ ---snip Complete!
Ansibleバージョンと確認
[root@localhost ~]# ansible --version ansible 2.9.5 config file = /etc/ansible/ansible.cfg configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3.6/site-packages/ansible executable location = /usr/bin/ansible python version = 3.6.8 (default, Nov 21 2019, 19:31:34) [GCC 8.3.1 20190507 (Red Hat 8.3.1-4)]
SSH起動と確認
[root@localhost ~]# systemctl status sshd sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-02-15 01:49:19 EST; 31min ago ---snip
Ansible実行groupの作成と確認
[root@localhost ansible]# groupadd -g 9010 ansible [root@localhost ansible]# grep ansible /etc/group ansible:x:9010:
userをAnsible実行groupのメンバーにする
[root@localhost ansible]# usermod ansibleuser -g ansible
Ansible実行ユーザのSSH鍵ペアを生成
※ファイル名をansibleuser
に指定すると、秘密鍵ansibleuser
、公開鍵ansibleuser.pub
が生成される。
[ansibleuser@localhost .ssh]$ ssh-keygen ※引数は適宜設定、とりあえず無くてもOK Generating public/private rsa key pair. Enter file in which to save the key (/home/ansibleuser/.ssh/id_rsa): ansibleuser Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in ansibleuser. Your public key has been saved in ansibleuser.pub. The key fingerprint is: SHA256:CNnUdHwo7K6R1UFvfWdrBrDUY9e37nOTQttNZTmz2Ko ansibleuser@localhost.localdomain The key's randomart image is: +---[RSA 3072]----+ | .+.oo+. .| | + +.=.+= .o| | o .. o ++.+ B| | . .o .. .B=| | .+S +==| | o . oo+.| | o . =.o| | . + *o| | E. . +| +----[SHA256]-----+ [ansibleuser@localhost .ssh]$
Ansiblesサーバで鍵が生成されていることを確認
[ansibleuser@localhost .ssh]$ pwd /home/ansibleuser/.ssh [ansibleuser@localhost .ssh]$ ls known_hosts ansibleuser ansibleuser.pub ※公開鍵と秘密鍵があること
Nexus9kでSCPを有効にする(scp以外の方法で転送する場合は不要)
※Nexus9kではssh serverはデフォルトで有効になっている
switch(config)# feature scp-server
Nexus9kでユーザを作成しnetwork-admin権限を与える
switch(config)# username nxuser password "password" switch(config)# username nxuser role network-admin
Ansibleサーバ公開鍵ファイルをNexus9kへ転送
※config上に公開鍵を書き込む方法もある。どちらでもOK。
[root@localhost ~]# scp /home/ansibleuser/.ssh/ansibleuser.pub nxuser@192.168.3.219: User Access Verification Password: ansibleuser.pub 100% 584 92.4KB/s 00:00 ※(参考)間違えた場合の消し方 switch# delete ansibleuser.pub Do you want to delete "/ansibleuser.pub" ? (yes/no/abort) [y] yes
Nexus9k側に公開鍵ファイルが転送されていることを確認
switch# dir 4096 Feb 15 06:45:02 2020 .rpmstore/ 4096 Feb 15 06:45:44 2020 .swtam/ 52753 Feb 15 06:53:34 2020 20200215_064803_poap_31179_init.log 0 Feb 15 06:45:32 2020 bootflash_sync_list 584 Feb 15 09:29:09 2020 ansibleuser.pub ※公開鍵ファイル 975383040 Aug 29 01:05:50 2019 nxos.7.0.3.I7.7.bin 0 Feb 15 06:54:59 2020 platform-sdk.cmd 4096 Feb 15 06:47:49 2020 scripts/ 4096 Feb 15 06:47:57 2020 virt_strg_pool_bf_vdc_1/ 4096 Feb 15 06:46:06 2020 virtual-instance/ 59 Feb 15 06:45:58 2020 virtual-instance.conf Usage for bootflash:// 1517277184 bytes used 2019942400 bytes free 3537219584 bytes total switch#
Nexus9kで公開鍵のパスを指定
switch(config)# username nxuser sshkey file bootflash:ansibleuser.pub
AnsibleサーバからNexus9kへパスワード無しでssh可能なことを確認
※-i
オプションでペアの秘密鍵を指定している。
[ansibleuser@localhost ~]$ ssh -i ./.ssh/ansibleuser nxuser@192.168.3.219 User Access Verification Cisco NX-OS Software ---snip--- switch# ※ログインできた
AnsibleサーバでAnsible用のhosts(インベントリファイル)を編集
[ansibleuser@localhost ~]$ cd /etc/ansible/ [ansibleuser@localhost ansible]$ ls ansible.cfg hosts roles ※この`hosts`を編集 [ansibleuser@localhost ansible]$ vi hosts [cisco] 192.168.3.219 ansible_hostname=nexus [cisco:vars] ansible_ssh_user=nxuser ansible_ssh_pass=nxuser ansible_network_os=nxos ansible_connection=network_cli #ansible_become=yes #ansible_become_method=enable #ansible_become_password=nxuser" #ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q cow"'
AnsibleサーバからAnsible経由のPINGを確認
[root@localhost ansible]# ansible -i ./hosts 192.168.3.219 -m ping 192.168.3.219 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/libexec/platform-python" }, "changed": false, "ping": "pong" }
configバックアップ用ymlを作成
[root@localhost ansible]# cat nexus9k_backup.yml - hosts: cisco gather_facts: no tasks: - name: Backup current switch config (nxos) nxos_config: backup: yes register: backup_nxos_location when: ansible_network_os == 'nxos'
configバックアップ用ymlを実行
[root@localhost ansible]# ansible-playbook nexus9k_backup.yml PLAY [cisco] ***************************************************************************************************** TASK [Backup current switch config (nxos)] *********************************************************************** changed: [192.168.3.219] PLAY RECAP ******************************************************************************************************* 192.168.3.219 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
configバックアップ出力ファイル
※タイムスタンプ付きで保存される。必ず'changed=1 'になる。
[root@localhost ansible]# pwd /etc/ansible [root@localhost ansible]# ls -al backup/ total 32 drwxr-xr-x. 2 root root 198 Feb 15 09:26 . drwxr-xr-x. 5 root root 130 Feb 15 09:22 .. -rw-r--r--. 1 root root 5059 Feb 15 09:12 192.168.3.219_config.2020-02-15@09:12:16 -rw-r--r--. 1 root root 5059 Feb 15 09:12 192.168.3.219_config.2020-02-15@09:12:27 -rw-r--r--. 1 root root 5059 Feb 15 09:12 192.168.3.219_config.2020-02-15@09:12:37 -rw-r--r--. 1 root root 5059 Feb 15 09:24 192.168.3.219_config.2020-02-15@09:24:23
log出力用ymlを作成
[root@localhost ansible]# cat nexus9k_show_mon.yml - hosts: cisco gather_facts: no tasks: - name: show run ios_command: commands: - show rmon events # - show diagnostic status module 1 register: config - name: save output to /etc/ansible/monitor copy: content: "{{ config.stdout[0] }}" dest: "/etc/ansible/mon/show_mon_{{ inventory_hostname }}.txt"
log出力用ymlを実行
※上書きで保存する。変更が無ければchanged=0
で変更があればchanged=1
になる。
[root@localhost ansible]# ansible-playbook nexus9k_show_mon.yml PLAY [cisco] ***************************************************************************************************** TASK [show run] ************************************************************************************************** ok: [192.168.3.219] TASK [save output to /etc/ansible/monitor] *********************************************************************** ok: [192.168.3.219] PLAY RECAP ******************************************************************************************************* 192.168.3.219 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
log出力ファイル
[root@localhost ansible]# ls -al mon/ total 4 drwxr-xr-x. 2 root root 40 Feb 15 09:11 . drwxr-xr-x. 5 root root 130 Feb 15 09:22 .. -rw-r--r--. 1 root root 564 Feb 15 09:11 show_mon_192.168.3.219.txt [root@localhost ansible]# cat mon/show_mon_192.168.3.219.txt <BU>-Bootup Diagnostics, <HM>-Health Monitoring Diagnostics <OD>-OnDemand Diagnostics, <SCH>-Scheduled Diagnostics ============================================== Card:(1) Nexus 9000v Ethernet Module ============================================== Current running test Run by -NA- -NA- Currently Enqueued Test Run by -NA- -NA- [root@localhost ansible]#