Cisco CCNA (200-301)Automation and ProgrammabilityMedium
A network automation engineer is developing an Ansible playbook to configure VLANs on a group of Cisco IOS XE switches. The engineer wants to ensure that if a VLAN already exists with the desired configuration, Ansible does not attempt to re-apply the configuration, and if it doesn't exist, it creates it. Which Ansible module and state combination should be used to achieve this behavior?
- Aios_command with `command: 'vlan <id>'`
- Bios_config with `state: present`
- Cios_vlan with `state: configured`
- Dios_vlan with `state: present`
Show answer & explanationAnswer & explanation
Correct answer: D. ios_vlan with `state: present`
The `ios_vlan` module is specifically designed for managing VLAN configurations on IOS devices. Using `state: present` with this module ensures that the VLAN is created if it doesn't exist, and if it already exists with the specified parameters, no changes are made, demonstrating idempotency.
Why the other options are wrong
- A. `ios_command` is used for executing arbitrary CLI commands. While it can create a VLAN, it lacks idempotency; it would attempt to run the command every time, potentially causing errors if the VLAN already exists or not confirming the state effectively.
- B. `ios_config` can manage configurations, but `ios_vlan` is more specific and provides better idempotency for VLANs. `state: present` is a valid state for `ios_config` but less granular for VLANs.
- C. `state: configured` is not a valid state option for the `ios_vlan` module. Common states include `present`, `absent`, `merged`, `replaced`, `overridden`.
Ansible `ios_vlan` Module
The `ios_vlan` Ansible module is used to manage VLAN configurations on Cisco IOS and IOS XE devices, ensuring idempotent creation, modification, or deletion of VLANs.
- Part of `cisco.ios` collection.
- Manages VLAN properties like ID, name, state.
- Supports `state: present` for creation/ensuring existence.
- Supports `state: absent` for deletion.
Memory trick: For 'VLANs on IOS', 'ios_vlan present' is the idempotent solution.