Run Your First Command and Playbook

    Before you work through this tutorial you need:

    • Ansible 2.5 (or higher) installed
    • One or more network devices that are compatible with Ansible
    • Basic Linux command line knowledge
    • Basic knowledge of network switch & router configuration

    Install Ansible using your preferred method. See Installation Guide. Then return to this tutorial.

    Confirm the version of Ansible (must be >= 2.5):

    1. ssh
    2. show config
    3. exit

    This manual connection also establishes the authenticity of the network device, adding its RSA key fingerprint to your list of known hosts. (If you have connected to the device before, you have already established its authenticity.)

    Instead of manually connecting and running a command on the network device, you can retrieve its configuration with a single, stripped-down Ansible command:

    • The flags in this command set seven values:
      • the host group(s) to which the command should apply (in this case, all)
      • the inventory (-i, the device or devices to target - without the trailing comma -i points to an inventory file)
      • the connection method (-c, the method for connecting and executing ansible)
      • the user (-u, the username for the SSH connection)
      • the SSH connection method (-k, please prompt for the password)
      • the module (-m, the ansible module to run)
      • an extra variable ( -e, in this case, setting the network OS value)

    NOTE: If you use with ssh keys, Ansible loads them automatically. You can omit -k flag.

    • Download , which looks like this:
    1. ---
    2.  
    3. - name: Network Getting Started First Playbook
    4. connection: network_cli
    5. gather_facts: false
    6. tasks:
    7.  
    8. - name: Get config for VyOS devices
    9. vyos_facts:
    10. gather_subset: all
    11.  
    12. - name: Display the config
    13. debug:
    14. msg: "The hostname is {{ ansible_net_hostname }} and the OS is {{ ansible_net_version }}"

    The playbook sets three of the seven values from the command line above: the group (hosts: all), the connection method () and the module (in each task). With those values set in the playbook, you can omit them on the command line. The playbook also adds a second task to show the config output. When a module runs in a playbook, the output is held in memory for use by future tasks instead of written to the console. The debug task here lets you see the results in your shell.

    • Run the playbook with the command:

    The playbook contains one play with two tasks, and should generate output like this:

    1. $ ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos first_playbook.yml
    2.  
    3. PLAY [First Playbook]
    4. ***************************************************************************************************************************
    5.  
    6. TASK [Get config for VyOS devices]
    7. ***************************************************************************************************************************
    8. ok: [vyos.example.net]
    9.  
    10. TASK [Display the config]
    11. ***************************************************************************************************************************
    12. ok: [vyos.example.net] => {
    13. "msg": "The hostname is vyos and the OS is VyOS"
    14. }
    • Now that you can retrieve the device config, try updating it with Ansible. Download , which is an extended version of the first playbook:

    The extended first playbook has four tasks in a single play. Run it with the same command you used above. The output shows you the change Ansible made to the config:

    1. $ ansible-playbook -i vyos.example.net, -u ansible -k -e ansible_network_os=vyos first_playbook_ext.yml
    2.  
    3. ************************************************************************************************************************************
    4.  
    5. TASK [Get config for VyOS devices]
    6. **********************************************************************************************************************************
    7. ok: [vyos.example.net]
    8.  
    9. TASK [Display the config]
    10. *************************************************************************************************************************************
    11. ok: [vyos.example.net] => {
    12. "msg": "The hostname is vyos and the OS is VyOS"
    13. }
    14.  
    15. TASK [Update the hostname]
    16. *************************************************************************************************************************************
    17. changed: [vyos.example.net]
    18.  
    19. TASK [Get changed config for VyOS devices]
    20. *************************************************************************************************************************************
    21. ok: [vyos.example.net]
    22.  
    23. TASK [Display the changed config]
    24. *************************************************************************************************************************************
    25. ok: [vyos.example.net] => {
    26. "msg": "The hostname is vyos-changed and the OS is VyOS"
    27. }
    28.  
    29. PLAY RECAP
    30. vyos.example.net : ok=6 changed=1 unreachable=0 failed=0