ansible-juniper-example - Ready-to-use Ansible virtualenv to use the insa_strasbourg.juniper collection
This project provides everything needed to implement a ready-to-use virtualenv to leverage the insa_strasbourg.juniper collection, along with configuration examples.
| Device | Support |
|---|---|
| EX4650 | ✅ |
| EX4600 | ✅ |
| EX4300 | ✅ |
| EX4100 | ✅ |
| EX3400 | ✅ |
| EX3300 | ✅ |
| EX2300 | |
| EX2200 | ✅ |
git clone https://github.com/DSIN-INSA-Strasbourg/ansible-juniper-collection.git [dest_dir]The virtualenv creation script was created and tested under Ubuntu. It uses the dpkg-query and apt commands and should therefore work under most Debian-based distributions but will NOT work on other types of distributions.
- Checking for a supported version of Python on the system
- Installing the following packages if they are not already present on the system:
- direnv
- python3-venv
- sshpass
- whois
- libssh-dev
- Checking for the presence of the
direnvcommand on the system - this will automatically activate and deactivate the virtualenv by changing the working directory - Adding the working folder to the folders allowed by direnv (
direnv allow) - Checking that direnv has loaded correctly, and that its hook exists
- Deleting and creating the virtalenv in the
.venv-ansiblefolder, unless the-eor--dont-recreate-venvoption was specified - Deleting the Ansible cache folder
.ansiblecontaining roles and collections, unless the-dor--dont-delete-ansible-diroption was specified - Installing Ansible and the Python
wheeldependencies, as well as those contained in therequirements-python.txtfile - unless the-por--dont-install-python-requirementsoption was specified. This installation includes a patch forncclientto connect to legacy (non-ELS) Juniper switches - Installing Ansible dependencies (collections/roles) based on the contents of the
requirements.ymlfile - unless the-aor--dont-install-ansible-requirementsoption was specified - Installing ansible-lint according to the contents of the
requirements-python-ansible-lint.txtfile - only if the-lor--install-ansible-lintoption was specified - Installing the VSCode Ansible extension
redhat.ansible- only if the-cor--install-ansible-vscode-extensionsoption was specified - Installing VSCode working folder settings in
.vscode/settings.json, and a ready-to-load workspace file containing the same settings in.vscode/*root folder name*.code-workspace- only if the-sor--install-ansible-vscode-settingsoption was specified
If the requirements are met, simply run one of these commands, depending on your needs:
# - Create the virtualenv
# - Install Ansible, Python dependencies, and Ansible dependencies
# - Install ansible-lint
# - Install the 'redhat.ansible' extension for vscode
# - Configure the folder/workspace for vscode: a 'vscode/ansible-juniper-example.code-workspace' file will be created
./reset_ansible_env -l -c -s
# - Create the virtualenv
# - Install Ansible, Python dependencies, and Ansible dependencies
# - Install ansible-lint
./reset_ansible_env -l
# - Create the virtualenv
# - Install Ansible, Python dependencies, and Ansible dependencies
./reset_ansible_env- The virtualenv and Python dependencies are installed in the
.venv-ansiblefolder - Ansible dependencies (roles, collections) and the ansible-galaxy cache are installed in the
.ansiblefolder - The VSCode configuration is installed in the
.vscodefolder
insa_strasbourg.juniper.ex_config: deploy configuration on Juniper EX switchesinsa_strasbourg.juniper.ex_firmware: deploy firmwares on Juniper EX switches
insa_strasbourg.juniper.ex_config: deploy configuration on Juniper EX switchesinsa_strasbourg.juniper.ex_firmware: deploy firmwares on Juniper EX switchesinsa_strasbourg.juniper.ex_plan_reboot: schedule a reboot on Juniper EX switches, or cancel a scheduled rebootinsa_strasbourg.juniper.ex_plan_shutdown: schedule a shutdown on Juniper EX switches, or cancel a scheduled shutdowninsa_strasbourg.juniper.ex_show_plan: print any scheduled reboot or shutdown on Juniper EX switches
cp -a EXAMPLES/base/* .
network file: Inventory file containing all required groups, plus an optional juniper_datacenter group, and containing four example network switches:
datacenter-001(EX4600)datacenter-002(EX4300)distribution-003(EX3300)distribution-004(EX4100)
The global configuration is defined by the configuration of the juniper group, of which all devices are members. For clarity, it has been split into 14 files in the group_vars/juniper/ folder, but it could just as easily be present in a single group_vars/juniper.yml file. These files are:
firmware.yml: Global configuration for firmware updatesansible.yml: Ansible-specific configurationglobal.yml: Global optionsfirewall_denied_ips.yml: List of IP addresses to be blocked by the switch firewallnetwork.yml: Switch network configurationntp.yml: NTP servers to use for clock synchronizationpoe.yml: Enable or disable PoE by defaultport_security.yml: Network security options configuration: storm control, dhcp snooping, dynamic ARP inspection (DAI), IP source guardqos.yml: QoS rules (CoS in Juniper jargon)snmp.yml: SNMP clients allowed to query the switches, and SNMP trap targetsstp.yml: Spanning-tree configurationsyslog.yml: Syslog servers to propagate logs tousers.yml: Users to create on the switchesvlans.yml: List of VLANs to deploy on the switches
In our example, these switches apply the configuration of the juniper group, and override it with the values defined in group_vars/juniper_datacenter.yml. This overriding is made possible by the ansible_group_priority inventory variable.
There is also a file for each switch model family in the group_vars folder. For example, group_vars\juniper_ex4100.yml.
These files contain certain parameters that allow the roles to function and should not be modified, as well as two variables that must be set to manage firmware updates: ex_firmware_dir and ex_firmware.
The specific configuration of the switches is set in the host_vars files of each device in the inventory. In our example:
host_vars/datacenter-001.ymlhost_vars/datacenter-002.ymlhost_vars/distribution-003.ymlhost_vars/distribution-004.yml
Whether for a configuration deployment or a firmware update, the user running the Ansible playbook must be able to SSH into the switches, ideally by key without having to enter a password/passphrase. Using an SSH agent is strongly recommended!
If it's not possible to connect to the switch via SSH, for example to deploy an initial configuration, it's possible to generate a configuration file that can be copied to the device using a USB stick. This can also be applied to the provided example configuration to get an idea of the configuration that will be applied to the switches.
The configuration files will be generated in the folder specified by the ex_config_save_dir variable, defined in group_vars/juniper/ansible.yml. In the provided example, this will be in the $HOME/.juniper-configs-example/ folder.
# Generate offline configuration for all switches in the inventory
ansible-playbook -i network insa_strasbourg.juniper.ex_config --tags offline
# Generating offline configuration for switches that are members of the juniper_datacenter group
ansible-playbook -i network -l juniper_datacenter insa_strasbourg.juniper.ex_config --tags offline
# Generate offline configuration for specified switches
ansible-playbook -i network -l distribution-003,distribution-004 insa_strasbourg.juniper.ex_config --tags offline