Navigation
Guide That Contains This Content
[+] Expand All
[-] Collapse All

    Creating and Executing Ansible Playbooks to Manage Devices Running Junos OS

    Juniper Networks provides support for using Ansible to manage devices running Junos OS. The Juniper Networks Ansible for Junos OS library provides a simple yet powerful method to perform certain operational and configuration tasks on devices running Junos OS. This topic outlines how to create a simple playbook to execute modules in the Juniper Networks Ansible library.

    You create Ansible playbooks to handle more complex management tasks. Playbooks consist of one or more plays, or groups of tasks, that operate on a set of defined hosts. Ansible hosts that are referenced in the playbook must be defined in the Ansible inventory file, which by default resides at /etc/ansible/hosts. Each play must specify the hosts on which the tasks operate, the list of tasks to execute on each host, and any required variables or module parameters. Because devices running Junos OS do not require Python, you must run modules locally on the control machine by including connection: local in the playbook play. You can avoid gathering unnecessary facts about the server by also including gather_facts: no.

    Ansible for Junos OS modules are hosted on Ansible Galaxy under the role junos. To use the Ansible for Junos OS modules in your playbook, you must download the role to the Ansible server and include the Juniper.junos role in the playbook.

    When you execute Ansible for Junos OS modules using a NETCONF session over SSH, you must have NETCONF enabled on the devices running Junos OS. We recommend that you create a simple task in the playbook that explicitly tests whether NETCONF is enabled on each device before executing other tasks. If this task fails for any host, by default, Ansible does not execute the remaining tasks for this host. Without this test, you might get a generic connection error during playbook execution that does not indicate whether this or another issue is the cause of any failures.

    Playbooks must be formatted using YAML. Because YAML is white-space sensitive and indentation is significant, you should always use spaces rather than tabs when creating playbooks. In YAML, items preceded by a hyphen (-) are considered list items, and the key:value notation represents a hash. For detailed information about creating Ansible playbooks, refer to the Ansible website at http://docs.ansible.com/playbooks.html .

    The following sections outline the steps for creating and running a simple playbook that executes Ansible for Junos OS modules:

    Creating a Playbook

    To create a simple playbook to perform tasks on devices running Junos OS:

    1. In your favorite editor, create a new file with a descriptive playbook name that uses the .yml file extension.
    2. Enter the required opening line for the playbook, and provide a descriptive name for the play.

      ---
      - name: Get Device Facts
    3. Define a colon-delimited list of the hosts or groups of hosts on which the modules will operate, or specify all to indicate all hosts in the inventory file.

      Note that any hosts or groups referenced in the playbook must be defined in the Ansible inventory file.

      ---
      - name: Get Device Facts
        hosts: dc1
    4. Include the Juniper.junos role.

      ---
      - name: Get Device Facts
        hosts: dc1
        roles:
        - Juniper.junos
    5. Because there is no requirement for Python on the devices running Junos OS, include connection: local to execute the plays locally on the control machine where Python is installed.

      ---
      - name: Get Device Facts
        hosts: dc1
        roles:
        - Juniper.junos 
        connection: local
    6. (Optional) Because Ansible executes plays locally on the control machine, avoid gathering unnecessary facts about the server by including gather_facts: no.

      ---
      - name: Get Device Facts
        hosts: dc1
        roles:
        - Juniper.junos 
        connection: local
        gather_facts: no
    7. Define a tasks section, and include one or more tasks with each task as a list item.

      ---
      - name: Get Device Facts
        hosts: dc1
        roles:
        - Juniper.junos 
        connection: local
        gather_facts: no
      
        tasks:
    8. (Optional) As an additional check, create a task to verify NETCONF connectivity for each device running Junos OS.

      ---
      - name: Get Device Facts
        hosts: dc1
        roles:
        - Juniper.junos     
        connection: local
        gather_facts: no
      
        tasks:
        - name: Checking NETCONF connectivity
          wait_for: host={{ inventory_hostname }} port=830 timeout=5
      
    9. Create tasks that use desired Ansible for Junos OS modules, for example:

      ---
      - name: Get Device Facts
        hosts: dc1
        roles:
        - Juniper.junos 
        connection: local
        gather_facts: no
      
        tasks:
        - name: Checking NETCONF connectivity
          wait_for: host={{ inventory_hostname }} port=830 timeout=5
      
        - name: Retrieve information from devices running Junos OS
          junos_get_facts:  
            host={{ inventory_hostname }}
          register: junos
      
        - name: version
          debug: msg="{{ junos.facts.version }}"
      

    Executing the Playbook

    To execute the playbook:

    • Issue the ansible-playbook command on the control machine, and provide the playbook path and any desired options.

      [root@ansible-cm]# ansible-playbook playbook.yml
      PLAY [Get Device Facts] ****************************************************
      
      TASK: [Checking NETCONF connectivity] **************************************
      ok: [dc1a.example.com]
      
      TASK: [Retrieve information from devices running Junos OS] *****************
      ok: [dc1a.example.com]
      
      TASK: [version] ************************************************************
      ok: [dc1a.example.com] => {
          "msg": "14.1R1.10"
      }
      
      PLAY RECAP *****************************************************************
      dc1a.example.com            : ok=3    changed=0    unreachable=0    failed=0   
      

    Modified: 2015-06-22