Variables

We discussed variables a bit in the inventory section. Because our managed nodes are not exactly alike, we need to accommodate the differences via variables. Variable names should be letters, numbers, and underscores and always start with a letter. Variables are commonly defined in three locations:

  • Playbook
  • The inventory file
  • Separate files to be included in included files and roles

Let's look at an example of defining variables in a playbook, cisco_1.yml:

    ---    - name: Configure SNMP Contact      hosts: "nexus"      gather_facts: false      connection: local      vars:        cli:          host: "{{ inventory_hostname }}"          username: cisco          password: cisco          transport: cli      tasks:        - name: configure snmp contact          nxos_snmp_contact:            contact: TEST_1            state: present provider: ...

Get Mastering Python Networking now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.