Working with Command Output in Network Modules

Conditionals in Networking Modules

Ansible allows you to use conditionals to control the flow of your playbooks. Ansible networking command modules use the following unique conditional statements.

  • eq - Equal
  • neq - Not equal
  • gt - Greater than
  • ge - Greater than or equal
  • lt - Less than
  • le - Less than or equal
  • contains - Object contains specified item

Conditional statements evaluate the results from the commands that areexecuted remotely on the device. Once the task executes the commandset, the wait_for argument can be used to evaluate the results beforereturning control to the Ansible playbook.

For example:

  1. ---
  2. - name: wait for interface to be admin enabled
  3. eos_command:
  4. commands:
  5. - show interface Ethernet4 | json
  6. wait_for:
  7. - "result[0].interfaces.Ethernet4.interfaceStatus eq connected"

In the above example task, the command show interface Ethernet4 | jsonis executed on the remote device and the results are evaluated. Ifthe path(result[0].interfaces.Ethernet4.interfaceStatus) is not equal to“connected”, then the command is retried. This process continuesuntil either the condition is satisfied or the number of retries hasexpired (by default, this is 10 retries at 1 second intervals).

The commands module can also evaluate more than one set of commandresults in an interface. For instance:

  1. ---
  2. - name: wait for interfaces to be admin enabled
  3. eos_command:
  4. commands:
  5. - show interface Ethernet4 | json
  6. - show interface Ethernet5 | json
  7. wait_for:
  8. - "result[0].interfaces.Ethernet4.interfaceStatus eq connected"
  9. - "result[1].interfaces.Ethernet5.interfaceStatus eq connected"

In the above example, two commands are executed on theremote device, and the results are evaluated. By specifying the resultindex value (0 or 1), the correct result output is checked against theconditional.

The wait_for argument must always start with result and then thecommand index in [], where 0 is the first command in the commands list,1 is the second command, 2 is the third and so on.