NW 機器に Ansible

Posted on 2019-02-04 in zakki

cisco router に Ansible

しようと思ったけど出来なかったので原因をメモ。

なんてことはなく秘密鍵が誤ってて出来なかっただけ。考えてみれば当たり前。

  • ansible.cfg
[defaults]
inventory = ./hosts
private_key_file = /home/vagrant/private_key

private_key_file をコメントアウトしたら出来た。

  • playbook.yml
 - hosts: router
   gather_facts: no
   tasks:
    - name: cisco show command run
      local_action:
        module:   ios_command
        commands:
          - show clock
        host:     "{{ inventory_hostname }}"
        username: "{{ ansible_ssh_user }}"
        password: "{{ ansible_ssh_password }}"
        authorize: true
      register: result

    - debug:
       var: result
  • 結果
[vagrant@localhost ansible-prj]$ ansible-playbook -i hosts cisco-showclock.yml

PLAY [router] ***************************************************************************************************************************************

TASK [cisco show command run] ***********************************************************************************************************************
[DEPRECATION WARNING]: Param 'host' is deprecated. See the module docs for more information. This feature will be removed in version 2.9.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[DEPRECATION WARNING]: Param 'username' is deprecated. See the module docs for more information. This feature will be removed in version 2.9.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
[DEPRECATION WARNING]: Param 'password' is deprecated. See the module docs for more information. This feature will be removed in version 2.9.
Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.
ok: [192.168.0.253 -> localhost]

TASK [debug] ****************************************************************************************************************************************
ok: [192.168.0.253] => {
    "result": {
        "changed": false,
        "deprecations": [
            {
                "msg": "Param 'host' is deprecated. See the module docs for more information",
                "version": 2.9
            },
            {
                "msg": "Param 'username' is deprecated. See the module docs for more information",
                "version": 2.9
            },
            {
                "msg": "Param 'password' is deprecated. See the module docs for more information",
                "version": 2.9
            }
        ],
        "failed": false,
        "stdout": [
            "00:04:19.185 JST Tue Feb 5 2019"
        ],
        "stdout_lines": [
            [
                "00:04:19.185 JST Tue Feb 5 2019"
            ]
        ]
    }
}

PLAY RECAP ******************************************************************************************************************************************
192.168.0.253              : ok=2    changed=0    unreachable=0    failed=0

2.9 でこいつは死ぬらしい。

network_cli

じゃあちょっとナウくして network_cli にする。

  • playbook.yml
- name: Cisco Playbook
  hosts: router
  connection: network_cli
  gather_facts: no
  vars:
    ansible_user: "{{ ansible_ssh_user }}"
    ansible_ssh_pass: "{{ ansible_ssh_password }}"
    ansible_network_os: ios
  tasks:
    - name: cisco show command run
      ios_command:
        commands:
          - show clock
      register: result

    - debug:
        var: result
  • 結果
[vagrant@localhost ansible-prj]$ ansible-playbook -i hosts cisco-showclock.yml

PLAY [Cisco Playbook] *******************************************************************************************************************************

TASK [cisco show command run] ***********************************************************************************************************************
ok: [192.168.0.253]

TASK [debug] ****************************************************************************************************************************************
ok: [192.168.0.253] => {
    "result": {
        "changed": false,
        "failed": false,
        "stdout": [
            "00:07:41.189 JST Tue Feb 5 2019"
        ],
        "stdout_lines": [
            [
                "00:07:41.189 JST Tue Feb 5 2019"
            ]
        ]
    }
}

PLAY RECAP ******************************************************************************************************************************************
192.168.0.253              : ok=2    changed=0    unreachable=0    failed=0

少しはナウくなった。

cli_command

もっとナウい cli_command ってのがあるらしいけど 2.7 かららしい。さっき、ansible を upgrade したらエラー吐いて失敗したのでそちらはまた今度。

まあ

ナウいって言いたいだけ感。あと show clock が若干ずれててややウケ。

Ansible Cisco