てくなべ (tekunabe)

ansible / network automation / 学習メモ

Ansible 2.7 で導入予定のネットワークモジュール cli_configを試す

■ はじめに

現在開発中の Ansible 2.7 (develブランチ) で、ネットワーク機器への 設定系 コマンドを投入するモジュール cli_config が登場しました。

なお、同じく Ansible 2.7 で開発中の 参照系 コマンドを実行するモジュール cli_command については過去記事「Ansible 2.7 で導入予定のネットワークモジュール cli_configを試す」をご参照ください。

この記事では、Junos で試した結果を記載します。

基本情報

cli_config モジュール 公式ドキュメント

cli_config - Push text based configuration to network devices over network_cli — Ansible Documentation

コード (cli_config.py)

ansible/cli_config.py at devel · ansible/ansible · GitHub

※現在(2018/8/14)開発中のため、仕様が変わる可能性があります。


■ 準備(開発版 Ansible のインストール)

pip install git+https://github.com/ansible/ansible.git@devel

現在、これで開発中の Ansible 2.7 がインストールされます。



■ Juniper Junos で試す

インベントリファイル

以下のインベントリファイルを用意します。今回は各変数は Playbook 内で定義することにします。

[junos]
172.16.0.1

Playbook(netconf指定で失敗パターン)

今回は、単純に set system ntp server 10.0.0.123 の1行を投入する Playbook を試すことにします。 junos_config モジュールの lines オプションはリストで指定できるのに対して、 cli_config モジュールの config オプションは文字列での指定になる点は注意が必要です。

コネクションプラグインには、junos_config モジュールと同じ調子で netconf を試してみます。(後述しますが失敗します)

- hosts: junos
  gather_facts: no

  tasks:
    - name: config test
      cli_config:
        config: set system ntp server 10.0.0.123

  vars:
    ansible_connection: netconf
    ansible_network_os: junos
    ansible_user: testuser
    ansible_ssh_pass: testuserpsss

実行(netconf指定で失敗パターン)

おっと、エラーになってしまいました。

(ansibledev) [vagrant@centos7 vagrant]$ ansible-playbook -i inventory cli_config.yml

PLAY [junos] **********************************************************************************************

TASK [config test] ****************************************************************************************
fatal: [172.16.0.1]: FAILED! => {"changed": false, "msg": "Connection type netconf is not valid for cli_config module"}
        to retry, use: --limit @/vagrant/cli_config.retry

PLAY RECAP ************************************************************************************************
172.16.0.1                 : ok=0    changed=0    unreachable=0    failed=1

どうやら、このモジュールは netconf コネクションプラグインは対応していないようです。

後で気がついたのですが、ドキュメントにきちんと

This module provides platform agnostic way of pushing text based configuration to network devices over network_cli connection plugin.

という記載がありました。

となれば、次に試すべきは network_cli コネクションプラグインなので試してみます。

Playbook(network_cli指定で成功パターン)

- hosts: junos
  gather_facts: no

  tasks:
    - name: config test
      cli_config:
        config: set system ntp server 10.0.0.123

  vars:
    ansible_connection: network_cli    # network_cli にしてみる
    ansible_network_os: junos
    ansible_user: testuser
    ansible_ssh_pass: testuserpsss

実行(network_cli指定で成功パターン)

今度は正常に完了しました。

(ansibledev) [vagrant@centos7 vagrant]$ ansible-playbook -i inventory cli_config.yml

PLAY [junos] **********************************************************************************************

TASK [config test] ****************************************************************************************
changed: [172.16.0.1]

PLAY RECAP ************************************************************************************************
172.16.0.1                 : ok=1    changed=1    unreachable=0    failed=0

念の為、Junos側で設定が入ったことを確認します。

root@vsrx1# show system ntp | display set
set system ntp server 10.0.0.123

無事に入りました。


■ まとめ

簡単ですが、 cli_config モジュールの動作を確認しました。 最近のネットワークモジュールは、プラットフォームに依存しないタイプ(内部で ansible_network_os変数を見てプラットフォーム固有モジュールを呼ぶ?)も増えてきており、 cli_config モジュールもその流れだと思います。

ネットワークモジュールとしては、cli_configcli_command モジュールの登場が Ansible 2.7 の特徴の一つと言えるでしょう。