Reputation: 31
I’m new to Ansible (ansible 2.9.6) and recently started to work on the design/directory structure for a project. My task is to configure equipment (specifically different Cisco devices) in the test lab to support test cases.
I would like to have one site.yml file that contains ALL 20+ test cases. The idea would be that the user, based on parameters, could execute all,multiple,or a single test case against a specific vendor device type( Cisco 4507, Cisco 3850, etc…) via the site.yml.
Originally, I structured the site.yml to be one play with 20 tasks that included a role for each test case (ntp, lldp, vlans,etc…) and tagged appropriately. However, I was not able to use different host for each task (test case) which is needed. Every test case will need to use its own subset of devices. Here is an example:
ansible-playbook -i inventories/network_staging site.yml --tags=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
My current site.yaml has multiple plays each representing a test case and using the appropriate hosts. However, I will need to use different devices for tasking, from the host group, depending on the role/test case. Here is the example:
ansible-playbook -i inventories/network_staging site.yml --tags=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>
QUESTION 1: Can I use different hosts per tasks in one play in my original design?
QUESTION 2: Is my current design the best design for what I’m trying to accomplish?
QUESTION 3: In my roles/<>/tasks/<>.yml file for my current design, I need to perform different task on different devices to accomplish the test case role. I have defined my host inventory group which includes all the devices that will be needed but how do I specify the specific host for certain task?
Upvotes: 3
Views: 2805
Reputation:
You can try by including the when statement. For example:
tasks:
- name: ntp role
include_role:
name: ntp
tags:
- ntp
when: ansible_hostname in groups['group_name']
Upvotes: 2