index.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. .. meta::
  2. :description: Network automation is a continuous process of automating the configuration, management and operations of a computer network. Most network devices can be managed via Salt Proxy Minions using Salt SSH. However, some vendors allow for a Salt Minion to be installed directly.
  3. :keywords: network configuration automation, netops, juniper, configuration management, cisco, junos, napalm
  4. .. _network-automation:
  5. ==================
  6. Network Automation
  7. ==================
  8. Network automation is a continuous process of automating the configuration,
  9. management and operations of a computer network. Although the abstraction
  10. could be compared with the operations on the server side, there are many particular
  11. challenges, the most important being that a network device is traditionally
  12. closed hardware able to run proprietary software only. In other words,
  13. the user is not able to install the salt-minion package directly on a
  14. traditional network device. For these reasons, most network devices can be
  15. controlled only remotely via :ref:`proxy minions <proxy-minion>` or
  16. using the :ref:`Salt SSH <salt-ssh>`. However, there are also vendors producing
  17. whitebox equipment (e.g. Arista, Cumulus) or others that have moved the
  18. operating system in the container (e.g. Cisco NX-OS, Cisco IOS-XR),
  19. allowing the salt-minion to be installed directly on the platform.
  20. New in Carbon (2016.11)
  21. -----------------------
  22. The methodologies for network automation have been introduced in
  23. :ref:`2016.11.0 <release-2016-11-0-network-automation-napalm>`. Network
  24. automation support is based on proxy minions.
  25. - :mod:`NAPALM proxy <salt.proxy.napalm>`
  26. - :mod:`Junos proxy<salt.proxy.junos>`
  27. - :mod:`Cisco NXOS <salt.proxy.nxos>`
  28. - :mod:`Cisco NOS <salt.proxy.cisconso>`
  29. NAPALM
  30. ------
  31. NAPALM (Network Automation and Programmability Abstraction Layer with
  32. Multivendor support) is an opensourced Python library that implements a set of
  33. functions to interact with different router vendor devices using a unified API.
  34. Being vendor-agnostic simplifies operations, as the configuration and
  35. interaction with the network device does not rely on a particular vendor.
  36. .. image:: /_static/napalm_logo.png
  37. Beginning with 2017.7.0, the NAPALM modules have been transformed so they can
  38. run in both proxy and regular minions. That means, if the operating system
  39. allows, the salt-minion package can be installed directly on the network gear.
  40. The interface between the network operating system and Salt in that case would
  41. be the corresponding NAPALM sub-package.
  42. For example, if the user installs the
  43. salt-minion on a Arista switch, the only requirement is
  44. `napalm-eos <https://github.com/napalm-automation/napalm-eos>`_.
  45. The following modules are available in 2017.7.0:
  46. - :mod:`NAPALM grains <salt.grains.napalm>`
  47. - :mod:`NET execution module <salt.modules.napalm_network>` - Networking basic
  48. features
  49. - :mod:`NTP execution module <salt.modules.napalm_ntp>`
  50. - :mod:`BGP execution module <salt.modules.napalm_bgp>`
  51. - :mod:`Routes execution module <salt.modules.napalm_route>`
  52. - :mod:`SNMP execution module <salt.modules.napalm_snmp>`
  53. - :mod:`Users execution module <salt.modules.napalm_users>`
  54. - :mod:`Probes execution module <salt.modules.napalm_probes>`
  55. - :mod:`NTP peers management state <salt.states.netntp>`
  56. - :mod:`SNMP configuration management state <salt.states.netsnmp>`
  57. - :mod:`Users management state <salt.states.netusers>`
  58. - :mod:`Netconfig state module <salt.states.netconfig>` - Manage the configuration
  59. of network devices using arbitrary templates and the Salt-specific
  60. advanced templating methodologies.
  61. - :mod:`Network ACL execution module <salt.modules.napalm_acl>` - Generate and
  62. load ACL (firewall) configuration on network devices.
  63. - :mod:`Network ACL state <salt.states.netacl>` - Manage the firewall
  64. configuration. It only requires writing the pillar structure correctly!
  65. - :mod:`NAPALM YANG execution module <salt.modules.napalm_yang_mod>` - Parse,
  66. generate and load native device configuration in a standard way,
  67. using the OpenConfig/IETF models. This module contains also helpers for
  68. the states.
  69. - :mod:`NAPALM YANG state module <salt.states.netyang>` - Manage the
  70. network device configuration according to the YANG models (OpenConfig or IETF).
  71. - :mod:`NET finder <salt.runners.net>` - Runner to find details easily and
  72. fast. It's smart enough to know what you are looking for. It will search
  73. in the details of the network interfaces, IP addresses, MAC address tables,
  74. ARP tables and LLDP neighbors.
  75. - :mod:`BGP finder <salt.runners.bgp>` - Runner to search BGP neighbors details.
  76. - :mod:`NAPALM syslog <salt.engines.napalm_syslog>` - Engine to import events
  77. from the napalm-logs library into the Salt event bus. The events are based
  78. on the syslog messages from the network devices and structured following
  79. the OpenConfig/IETF YANG models.
  80. - :mod:`NAPALM Helpers <salt.modules.napalm>` - Generic helpers for
  81. NAPALM-related operations. For example, the
  82. :mod:`Compliance report <salt.modules.napalm.compliance_report>` function
  83. can be used inside the state modules to compare the expected and the
  84. existing configuration.
  85. Getting started
  86. ###############
  87. Install NAPALM - follow the notes_ and check the platform-specific dependencies_.
  88. .. _notes: https://napalm.readthedocs.io/en/latest/installation/index.html
  89. .. _dependencies: https://napalm.readthedocs.io/en/latest/installation/index.html#dependencies
  90. Salt's Pillar system is ideally suited for configuring proxy-minions
  91. (though they can be configured in /etc/salt/proxy as well). Proxies
  92. can either be designated via a pillar file in :conf_master:`pillar_roots`,
  93. or through an external pillar.
  94. External pillars afford the opportunity for interfacing with
  95. a configuration management system, database, or other knowledgeable system
  96. that may already contain all the details of proxy targets. To use static files
  97. in :conf_master:`pillar_roots`, pattern your files after the following examples:
  98. ``/etc/salt/pillar/top.sls``
  99. .. code-block:: yaml
  100. base:
  101. router1:
  102. - router1
  103. router2:
  104. - router2
  105. switch1:
  106. - switch1
  107. switch2:
  108. - switch2
  109. cpe1:
  110. - cpe1
  111. ``/etc/salt/pillar/router1.sls``
  112. .. code-block:: yaml
  113. proxy:
  114. proxytype: napalm
  115. driver: junos
  116. host: r1.bbone.as1234.net
  117. username: my_username
  118. password: my_password
  119. ``/etc/salt/pillar/router2.sls``
  120. .. code-block:: yaml
  121. proxy:
  122. proxytype: napalm
  123. driver: iosxr
  124. host: r2.bbone.as1234.net
  125. username: my_username
  126. password: my_password
  127. optional_args:
  128. port: 22022
  129. ``/etc/salt/pillar/switch1.sls``
  130. .. code-block:: yaml
  131. proxy:
  132. proxytype: napalm
  133. driver: eos
  134. host: sw1.bbone.as1234.net
  135. username: my_username
  136. password: my_password
  137. optional_args:
  138. enable_password: my_secret
  139. ``/etc/salt/pillar/switch2.sls``
  140. .. code-block:: yaml
  141. proxy:
  142. proxytype: napalm
  143. driver: nxos
  144. host: sw2.bbone.as1234.net
  145. username: my_username
  146. password: my_password
  147. ``/etc/salt/pillar/cpe1.sls``
  148. .. code-block:: yaml
  149. proxy:
  150. proxytype: napalm
  151. driver: ios
  152. host: cpe1.edge.as1234.net
  153. username: ''
  154. password: ''
  155. optional_args:
  156. use_keys: True
  157. auto_rollback_on_error: True
  158. CLI examples
  159. ############
  160. Display the complete running configuration on ``router1``:
  161. .. code-block:: bash
  162. $ sudo salt 'router1' net.config source='running'
  163. Retrieve the NTP servers configured on all devices:
  164. .. code-block:: yaml
  165. $ sudo salt '*' ntp.servers
  166. router1:
  167. ----------
  168. comment:
  169. out:
  170. - 1.2.3.4
  171. result:
  172. True
  173. cpe1:
  174. ----------
  175. comment:
  176. out:
  177. - 1.2.3.4
  178. result:
  179. True
  180. switch2:
  181. ----------
  182. comment:
  183. out:
  184. - 1.2.3.4
  185. result:
  186. True
  187. router2:
  188. ----------
  189. comment:
  190. out:
  191. - 1.2.3.4
  192. result:
  193. True
  194. switch1:
  195. ----------
  196. comment:
  197. out:
  198. - 1.2.3.4
  199. result:
  200. True
  201. Display the ARP tables on all Cisco devices running IOS-XR 5.3.3:
  202. .. code-block:: bash
  203. $ sudo salt -G 'os:iosxr and version:5.3.3' net.arp
  204. Return operational details for interfaces from Arista switches:
  205. .. code-block:: bash
  206. $ sudo salt -C 'sw* and os:eos' net.interfaces
  207. Execute traceroute from the edge of the network:
  208. .. code-block:: bash
  209. $ sudo salt 'router*' net.traceroute 8.8.8.8 vrf='CUSTOMER1-VRF'
  210. Verbatim display from the CLI of Juniper routers:
  211. .. code-block:: bash
  212. $ sudo salt -C 'router* and G@os:junos' net.cli 'show version and haiku'
  213. Retrieve the results of the RPM probes configured on Juniper MX960 routers:
  214. .. code-block:: bash
  215. $ sudo salt -C 'router* and G@os:junos and G@model:MX960' probes.results
  216. Return the list of configured users on the CPEs:
  217. .. code-block:: bash
  218. $ sudo salt 'cpe*' users.config
  219. Using the :mod:`BGP finder <salt.runners.bgp>`, return the list of BGP neighbors
  220. that are down:
  221. .. code-block:: bash
  222. $ sudo salt-run bgp.neighbors up=False
  223. Using the :mod:`NET finder <salt.runners.net>`, determine the devices containing
  224. the pattern "PX-1234-LHR" in their interface description:
  225. .. code-block:: bash
  226. $ sudo salt-run net.find PX-1234-LHR
  227. Cross-platform configuration management example: NTP
  228. ####################################################
  229. Assuming that the user adds the following two lines under
  230. :conf_master:`file_roots`:
  231. .. code-block:: yaml
  232. file_roots:
  233. base:
  234. - /etc/salt/pillar/
  235. - /etc/salt/templates/
  236. - /etc/salt/states/
  237. Define the list of NTP peers and servers wanted:
  238. ``/etc/salt/pillar/ntp.sls``
  239. .. code-block:: yaml
  240. ntp.servers:
  241. - 1.2.3.4
  242. - 5.6.7.8
  243. ntp.peers:
  244. - 10.11.12.13
  245. - 14.15.16.17
  246. Include the new file: for example, if we want to have the same NTP servers on all
  247. network devices, we can add the following line inside the ``top.sls`` file:
  248. .. code-block:: yaml
  249. '*':
  250. - ntp
  251. ``/etc/salt/pillar/top.sls``
  252. .. code-block:: yaml
  253. base:
  254. '*':
  255. - ntp
  256. router1:
  257. - router1
  258. router2:
  259. - router2
  260. switch1:
  261. - switch1
  262. switch2:
  263. - switch2
  264. cpe1:
  265. - cpe1
  266. Or include only where needed:
  267. ``/etc/salt/pillar/top.sls``
  268. .. code-block:: yaml
  269. base:
  270. router1:
  271. - router1
  272. - ntp
  273. router2:
  274. - router2
  275. - ntp
  276. switch1:
  277. - switch1
  278. switch2:
  279. - switch2
  280. cpe1:
  281. - cpe1
  282. Define the cross-vendor template:
  283. ``/etc/salt/templates/ntp.jinja``
  284. .. code-block:: jinja
  285. {%- if grains.vendor|lower == 'cisco' %}
  286. no ntp
  287. {%- for server in servers %}
  288. ntp server {{ server }}
  289. {%- endfor %}
  290. {%- for peer in peers %}
  291. ntp peer {{ peer }}
  292. {%- endfor %}
  293. {%- elif grains.os|lower == 'junos' %}
  294. system {
  295. replace:
  296. ntp {
  297. {%- for server in servers %}
  298. server {{ server }};
  299. {%- endfor %}
  300. {%- for peer in peers %}
  301. peer {{ peer }};
  302. {%- endfor %}
  303. }
  304. }
  305. {%- endif %}
  306. Define the SLS state file, making use of the
  307. :mod:`Netconfig state module <salt.states.netconfig>`:
  308. ``/etc/salt/states/router/ntp.sls``
  309. .. code-block:: yaml
  310. ntp_config_example:
  311. netconfig.managed:
  312. - template_name: salt://ntp.jinja
  313. - peers: {{ pillar.get('ntp.peers', []) | json }}
  314. - servers: {{ pillar.get('ntp.servers', []) | json }}
  315. Run the state and assure NTP configuration consistency across your
  316. multi-vendor network:
  317. .. code-block:: bash
  318. $ sudo salt 'router*' state.sls router.ntp
  319. Besides CLI, the state can be scheduled or executed when triggered by a certain
  320. event.
  321. JUNOS
  322. -----
  323. Juniper has developed a Junos specific proxy infrastructure which allows
  324. remote execution and configuration management of Junos devices without
  325. having to install SaltStack on the device. The infrastructure includes:
  326. - :mod:`Junos proxy <salt.proxy.junos>`
  327. - :mod:`Junos execution module <salt.modules.junos>`
  328. - :mod:`Junos state module <salt.states.junos>`
  329. - :mod:`Junos syslog engine <salt.engines.junos_syslog>`
  330. The execution and state modules are implemented using junos-eznc (PyEZ).
  331. Junos PyEZ is a microframework for Python that enables you to remotely manage
  332. and automate devices running the Junos operating system.
  333. Getting started
  334. ###############
  335. Install PyEZ on the system which will run the Junos proxy minion.
  336. It is required to run Junos specific modules.
  337. .. code-block:: shell
  338. pip install junos-eznc
  339. Next, set the master of the proxy minions.
  340. ``/etc/salt/proxy``
  341. .. code-block:: yaml
  342. master: <master_ip>
  343. Add the details of the Junos device. Device details are usually stored in
  344. salt pillars. If the you do not wish to store credentials in the pillar,
  345. one can setup passwordless ssh.
  346. ``/srv/pillar/vmx_details.sls``
  347. .. code-block:: yaml
  348. proxy:
  349. proxytype: junos
  350. host: <hostip>
  351. username: user
  352. passwd: secret123
  353. Map the pillar file to the proxy minion. This is done in the top file.
  354. ``/srv/pillar/top.sls``
  355. .. code-block:: yaml
  356. base:
  357. vmx:
  358. - vmx_details
  359. .. note::
  360. Before starting the Junos proxy make sure that netconf is enabled on the
  361. Junos device. This can be done by adding the following configuration on
  362. the Junos device.
  363. .. code-block:: shell
  364. set system services netconf ssh
  365. Start the salt master.
  366. .. code-block:: bash
  367. salt-master -l debug
  368. Then start the salt proxy.
  369. .. code-block:: bash
  370. salt-proxy --proxyid=vmx -l debug
  371. Once the master and junos proxy minion have started, we can run execution
  372. and state modules on the proxy minion. Below are few examples.
  373. CLI examples
  374. ############
  375. For detailed documentation of all the junos execution modules refer:
  376. :mod:`Junos execution module <salt.modules.junos>`
  377. Display device facts.
  378. .. code-block:: bash
  379. $ sudo salt 'vmx' junos.facts
  380. Refresh the Junos facts. This function will also refresh the facts which are
  381. stored in salt grains. (Junos proxy stores Junos facts in the salt grains)
  382. .. code-block:: bash
  383. $ sudo salt 'vmx' junos.facts_refresh
  384. Call an RPC.
  385. .. code-block:: bash
  386. $ sudo salt 'vmx' junos.rpc 'get-interface-information' '/var/log/interface-info.txt' terse=True
  387. Install config on the device.
  388. .. code-block:: bash
  389. $ sudo salt 'vmx' junos.install_config 'salt://my_config.set'
  390. Shutdown the junos device.
  391. .. code-block:: bash
  392. $ sudo salt 'vmx' junos.shutdown shutdown=True in_min=10
  393. State file examples
  394. ###################
  395. For detailed documentation of all the junos state modules refer:
  396. :mod:`Junos state module <salt.states.junos>`
  397. Executing an RPC on Junos device and storing the output in a file.
  398. ``/srv/salt/rpc.sls``
  399. .. code-block:: yaml
  400. get-interface-information:
  401. junos:
  402. - rpc
  403. - dest: /home/user/rpc.log
  404. - interface_name: lo0
  405. Lock the junos device, load the configuration, commit it and unlock
  406. the device.
  407. ``/srv/salt/load.sls``
  408. .. code-block:: yaml
  409. lock the config:
  410. junos.lock
  411. salt://configs/my_config.set:
  412. junos:
  413. - install_config
  414. - timeout: 100
  415. - diffs_file: 'var/log/diff'
  416. commit the changes:
  417. junos:
  418. - commit
  419. unlock the config:
  420. junos.unlock
  421. According to the device personality install appropriate image on the device.
  422. ``/srv/salt/image_install.sls``
  423. .. code-block:: jinja
  424. {% if grains['junos_facts']['personality'] == MX %}
  425. salt://images/mx_junos_image.tgz:
  426. junos:
  427. - install_os
  428. - timeout: 100
  429. - reboot: True
  430. {% elif grains['junos_facts']['personality'] == EX %}
  431. salt://images/ex_junos_image.tgz:
  432. junos:
  433. - install_os
  434. - timeout: 150
  435. {% elif grains['junos_facts']['personality'] == SRX %}
  436. salt://images/srx_junos_image.tgz:
  437. junos:
  438. - install_os
  439. - timeout: 150
  440. {% endif %}
  441. Junos Syslog Engine
  442. ###################
  443. :mod:`Junos Syslog Engine <salt.engines.junos_syslog>` is a Salt engine
  444. which receives data from various Junos devices, extracts event information and
  445. forwards it on the master/minion event bus. To start the engine on the salt
  446. master, add the following configuration in the master config file.
  447. The engine can also run on the salt minion.
  448. ``/etc/salt/master``
  449. .. code-block:: yaml
  450. engines:
  451. - junos_syslog:
  452. port: xxx
  453. For junos_syslog engine to receive events, syslog must be set on the Junos device.
  454. This can be done via following configuration:
  455. .. code-block:: shell
  456. set system syslog host <ip-of-the-salt-device> port xxx any any
  457. .. toctree::
  458. :maxdepth: 2
  459. :glob: