ansible - 是否可以让 Ansible 检查设备上的配置并在满足条件时运行某些任务?
问题描述
我正在尝试在我们的基础架构中自动进行更改。在这种情况下,我们有 3 种需要或不需要配置的开关变体:
第一个变体是包含导致过时 RADIUS 服务器的旧配置的交换机。
第二种变体是具有现代配置的交换机,可通向新的 RADIUS 服务器。
最后一种变体是不需要任何更改且未配置任何 AAA 的聚合交换机。
我目前的目标是让 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 上解决了,请发帖!任何事情都有帮助。
解决方案
我想到了。
以下脚本执行我需要的功能:
---
- 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
推荐阅读
- php - php websocket棘轮不从学说中获取最新数据
- c++ - 动态内存如何与 CLion 一起工作?
- r - 如果 file.csv 使用“;”,则将字符变量转换为数字时出错 而不是“,”分隔符。R
- c++ - 如何在 VC++ 中定义一个超过 MAXINT 元素的布尔数组
- android - 使用 AppAuth-Android 和使用 IdS4 的 aspnet 核心进行身份验证
- javascript - 回调函数无法访问父函数范围内的变量
- javascript - 如何分离日期输入引导日期范围选择器
- airflow - Airflow: how to use xcom_push and xcom_pull in non-PythonOperator
- python - 通过添加 2 个 1D 数组的每个组合在 Python 中创建 2D 矩阵
- python - Flask 不会解释 css、js 文件