Ansible的ntc-ansible模块集总结(第三部分:执行配置更改模块)

首先

在前两篇文章中,我们介绍了Ansible的第三方网络模块ntc-ansible的概述,安装设置以及执行确认命令模块的步骤。

「Ansible的ntc-ansible模块汇总(①概述、设置篇)」

「Ansible的ntc-ansible模块汇总(②执行确认命令模块篇)」

本文概述了8个模块中用于进行配置更改的ntc_config_command模块的操作确认结果。

2. 库存文件

与上次相同,目标设备将被设定为test3(思科CSR1000V)。
创建以下清单文件,并存放在/home/user/ntc-ansible/目录下。

[cisco]
192.168.100.201

[cisco:vars]
hostname=test3
ansible_username=test3
ansible_password=cisco
enable_secret=test3

実例1: 将IP地址设置为(将命令记录在Playbook中)。

3.1. 游戏策略手册

创建以下Playbook,并存储在/home/user/ntc-ansible/目录下。
在ntc_config_command模块中,进行对物理接口的IP地址设置。
而在公式的ios_config模块中,为了保证冪等性,在parents选项中需要定义interface GigabitEthernet3命令。但是ntc_config_command模块并未考虑冪等性,因此将其记录在同一个commands选项下。

此外,ntc_config_command和debug模块也用于获取和显示相关接口的Running Config摘录。

---

- hosts: cisco
  gather_facts: no
  connection: local

  tasks:
    - name: change configuration on remote devices
      ntc_config_command:
        connection: telnet   # 今回はログイン方式としてtelnetを指定。デフォルト値はssh
        commands:
          - interface GigabitEthernet3
          - ip address 10.10.10.10 255.255.255.0
        provider: "{{ cli }}"

    - name: run show command on remote devices
      ntc_show_command:
        connection: telnet
        command: show running-config interface GigabitEthernet3
        use_templates: false
        provider: "{{ cli }}"
      register: result

    - name: display show command
      debug:
        var: result.response

  vars:
    cli:
      host: "{{ inventory_hostname }}"
      username: "{{ ansible_username }}"
      password: "{{ ansible_password }}"
      secret: "{{ enable_secret }}"
      platform: cisco_ios   # 対象機器のOSに合わせて指定する必要あり

3.2. 运行结果

从debug模块(显示show命令)的执行结果可以看出,IP地址已经成功设置为10.10.10.10。

[user@localhost ntc-ansible]$ ansible-playbook -i inventory playbook2.yml

PLAY [cisco] ******************************************************************************************************************

TASK [change configuration on remote devices] *********************************************************************************
changed: [192.168.100.201]

TASK [run show command on remote devices] *************************************************************************************
ok: [192.168.100.201]

TASK [display show command] ***************************************************************************************************
ok: [192.168.100.201] => {
    "result.response": [
        "Building configuration...\n\nCurrent configuration : 90 bytes\n!\ninterface GigabitEthernet3\n ip address 10.10.10.10 255.255.255.0\n negotiation auto\nend\n"
    ]
}

PLAY RECAP ********************************************************************************************************************
192.168.100.201            : ok=3    changed=1    unreachable=0    failed=0   

4. 示例2:IP地址设置(将设置命令记录到另一个文件中)

4.1. 操作手冊、設定檔案

接下来,在Playbook中加载包含设置命令的文件,并尝试进行IP地址设置。
创建以下Playbook,并保存在/home/user/ntc-ansible/目录下。
与示例1的不同之处在于在commands_file选项中指定了设置文件的存储位置。

---

- hosts: cisco
  gather_facts: no
  connection: local

  tasks:
    - name: change configuration on remote devices
      ntc_config_command:
        connection: telnet
        commands_file: './{{ hostname }}_setup_config.txt'
        provider: "{{ cli }}"

    - name: run show command on remote devices
      ntc_show_command:
        connection: telnet
        command: show running-config interface GigabitEthernet3
        use_templates: false
        provider: "{{ cli }}"
      register: result

    - name: display show command
      debug:
        var: result.response

  vars:
    cli:
      host: "{{ inventory_hostname }}"
      username: "{{ ansible_username }}"
      password: "{{ ansible_password }}"
      secret: "{{ enable_secret }}"
      platform: cisco_ios

为了确认ntc_config_command模块在配置文件中是否像官方模块一样考虑了Config的层次结构,所以我们特意删除了第二行的ip address~的缩进。

interface GigabitEthernet3
ip address 10.10.10.11 255.255.255.0

4.2. 执行结果 (shí jié guǒ)

从IP地址成功修改为10.10.10.11可以推测出,ntc_config_command模块不考虑配置的层级结构,只是简单地从上往下执行命令。

[user@localhost ntc-ansible]$ ansible-playbook -i inventory playbook3.yml

PLAY [cisco] ******************************************************************************************************************

TASK [change configuration on remote devices] *********************************************************************************
changed: [192.168.100.201]

TASK [run show command on remote devices] *************************************************************************************
ok: [192.168.100.201]

TASK [display show command] ***************************************************************************************************
ok: [192.168.100.201] => {
    "result.response": [
        "Building configuration...\n\nCurrent configuration : 90 bytes\n!\ninterface GigabitEthernet3\n ip address 10.10.10.11 255.255.255.0\n negotiation auto\nend\n"
    ]
}

PLAY RECAP ********************************************************************************************************************
192.168.100.201            : ok=3    changed=1    unreachable=0    failed=0  

5. 最后

这次我们看了ntc-ansible的配置变更模块。
虽然不考虑幂等性是一个缺点,但既可以使用官方模块进行文件配置,又支持Telnet,可能在一些特定的情况下有用途。

广告
将在 10 秒后关闭
bannerAds