我是Ansible (ansible 2.9.6)的新手,最近开始为一个项目设计/目录结构。我的任务是在测试实验室中配置设备(特别是不同的Cisco设备),以支持测试用例。
我希望有一个包含所有site.yml测试用例的20+文件。根据参数,用户可以针对特定的供应商设备类型( Cisco 4507、Cisco 3850等)执行所有、多个或单个测试用例。通过site.yml。
最初,我将site.yml构建为一个游戏,包含20个任务,其中包括每个测试用例的一个角色(ntp、lldp、vlan等)。并贴上适当的标签。但是,我无法为每个任务(测试用例)使用不同的主机。每个测试用例都需要使用自己的设备子集。下面是一个示例:
-i目录/网络暂存site.yml -标签=ntp -e type=C4507
~/site.yml
---
- name: Test Cases
hosts: all
gather_facts: false
connection: local
tasks:
- name: ntp role
hosts: "{{type}}_ntpTC"
include_role:
name: ntp
tags:
- ntp
- name: vlan role
hosts: "{{type}}_vlanTC"
include_role:
name: vlan
tags:
- vlan
我当前的site.yaml有多个游戏,每个游戏代表一个测试用例,并使用适当的主机。但是,根据角色/测试用例的不同,我需要使用来自主机组的不同设备来执行任务。下面是一个例子:
-i目录/网络分期site.yml -标签=ntp,vlan -e type=C4507
~/site.yml
---
- name: NTP Test Case
hosts: "{{type}}_ntpTC"
gather_facts: false
connection: local
tasks:
- name: ntp role
include_role:
name: ntp
tags:
- ntp
- name: VLAN Test Case
hosts: "{{type}}_vlanTC"
gather_facts: false
connection: local
tasks:
- name: vlan role
include_role:
name: vlan
tags:
- vlan
~/inventories/network_staging/hosts/cisco
###main.yml inventory list
## IPs defined in ~/inventories/network_staging/host_vars/SW6.yml SW7.yml and SW8.yml
##Cisco 4507 Test Cases
#NTP Test Case
[C4507_ntpTC]
SW8
#VLAN Test Case
[C4507_vlanTC]
SW7
SW6
~/roles/ntp/tasks/main.yml
---
# Tasking for NTP Test Case
- name: import ntp.yml
tags:
- ntp
~/roles/ntp/tasks/ntp.yml
---
- name: show NTP
ios_command:
commands:
- <insert ntp show status commands on SW8 here….>
~/roles/vlan/tasks/main.yml
---
# Tasking for VLAN Test Case
- name: import vlan.yml
tags:
- vlan
~/roles/vlan/tasks/vlan.yml
---
- name: configure VLAN SW7
ios_command:
commands:
- <insert vlan access switchport configuration here for SW7….>
- name: configure VLAN SW6
ios_command:
commands:
- <insert vlan access switchport configuration here for SW6….>
- <insert ping SW7 here>
问题1:在我最初的设计中,是否可以在一次播放中使用不同的主机?
问题2:是我目前正在努力完成的最佳设计吗?
问题3:在我的角色/<>//<>..yml文件中,对于我当前的设计,我需要在不同的设备上执行不同的任务来完成测试用例角色。我已经定义了我的主机库存组,它包括所有需要的设备,但是我如何为特定的任务指定特定的主机?
发布于 2020-05-06 08:09:55
您可以通过包含when语句来尝试。例如:
tasks:
- name: ntp role
include_role:
name: ntp
tags:
- ntp
when: ansible_hostname in groups['group_name']
https://stackoverflow.com/questions/61623335
复制相似问题