首页 > 解决方案 > 是否可以让 Ansible 检查设备上的配置并在满足条件时运行某些任务?

问题描述

我正在尝试在我们的基础架构中自动进行更改。在这种情况下,我们有 3 种需要或不需要配置的开关变体:

我目前的目标是让 Ansible 了解设备是否有错误的 IP 地址,需要对其进行配置以获取正确的信息。或者,如果它具有正确的 IP 地址,则保持原样。

前两个很容易。我遇到的问题是让 Ansible 明白,如果两者都没有找到,那么什么也不做。

我的主机文件

[test:vars]
ansible_user=Username
ansible_password=Password
ansible_become_pass=Password

[test]
Manseau-E5 ansible_port=30005 ansible_host=67.53.178.51
Manseau-E6 ansible_port=30006 ansible_host=67.53.178.51
Manseau-E7 ansible_port=30006 ansible_host=67.53.178.51
Manseau-E8 ansible_port=30006 ansible_host=67.53.178.51

我的剧本可以执行我提到的前两个任务,但我似乎无法弄清楚如何创建支票。我能想到的一种方法是在三个单独的任务上使用 running_config 命令,三个单独的标志通向 3 个单独的处理程序。

---
- hosts: icx
  #gather_facts: no
  vars:
    ansible_network_os: icx
    ansible_connection: network_cli
    ansible_become: True
    ansible_become_method: enable
    ansible_command_timeout: 60

  tasks:
  - name: Check for legacy AAA if found Change to modern AAA
    icx_config:
      lines:
        - radius-client coa host 52.39.117.1 key 2 $Zl5ucm5nUGlebi0=
        - radius-server host 52.41.63.155 auth-port 1812 acct-port 1813 default key 2 $Zl5ucm5nUGlebi0= dot1x mac-auth web-auth
      before:
        - no radius-client coa host 66.45.82.108 key 2 $Zl5ucm5nUGlebi0=
        - no radius-server host 69.48.211.170 auth-port 1812 acct-port 1813 default key 2 $Zl5ucm5nUGlebi0= dot1x mac-auth web-auth
    notify: "save icx"

  handlers:
    - name: save icx
      icx_command:
        commands:
          - command: "wr mem"

我想到但肯定没有给出正确输出的一个例子如下:

---
- hosts: test
  #gather_facts: no
  vars:
    ansible_network_os: icx
    ansible_connection: network_cli
    ansible_become: True
    ansible_become_method: enable
    ansible_command_timeout: 60

  tasks:
  - name: Check for Legacy AAA
    icx_config:
      running_config: |
        radius-client coa host 66.45.82.108 key 2 $Zl5ucm5nUGlebi0=
        radius-server host 69.48.211.170 auth-port 1812 acct-port 1813 default key 2 $Zl5ucm5nUGlebi0= dot1x mac-auth web-auth
      lines:
        - radius-client coa host 52.39.117.1 key 2 $Zl5ucm5nUGlebi0=
        - radius-server host 52.41.63.155 auth-port 1812 acct-port 1813 default key 2 $Zl5ucm5nUGlebi0= dot1x mac-auth web-auth
    check_mode: True

我在上面的脚本中的目标是让 ansible 跨不同的开关读取该行并进行相应的匹配......当然,我或多或少不知道我在 Ansible 中做了什么。

模块 icx_config 和 icx_command 具有与 ios_config 和 ios_command 几乎相同的功能。如果有人有类似的案例,他们在 ios vs icx 上解决了,请发帖!任何事情都有帮助。

标签: ansibleansible-2.x

解决方案


我想到了。

以下脚本执行我需要的功能:

---
- hosts: test
  #gather_facts: no
  vars:
    ansible_network_os: icx
    ansible_connection: network_cli
    ansible_become: True
    ansible_become_method: enable
    ansible_command_timeout: 60

  tasks:
  - name: Check for RADIUS existence
    icx_command:
      commands:
        - show aaa
      wait_for: result[0] contains ElevenRules
  - name: Check for Modern AAA
    icx_config:
      lines:
        - radius-client coa host 52.39.117.1 key 2 $Zl5ucm5nUGlebi0=
        - radius-server host 52.41.63.155 auth-port 1812 acct-port 1813 default key 2 $Zl5ucm5nUGlebi0= dot1x mac-auth web-auth
    check_mode: True

推荐阅读