Anyone interested in network automation has to confront an uncomfortable question: Python or Ansible?

Personally, I started with Python – but there isn’t an incorrect answer. Both Python and Ansible have libraries and collections to interact with a variety of servers, network devices, and other integrated systems.

When I come across a task that I want to automate, I’ll often look at the task and think logically about which platform would be a better choice. Here is how I would approach backing up Cisco running configuration files with both Python and Ansible:

Python Configuration Backup and Ansible Configuration Backup

How can you measure which solution is better?

Does it work?
Both solutions work and will provide error information to the console or terminal window.

Is it readable?
They’re also readable, even for novice users. The Ansible playbook may be 9 lines shorter, but the Python script has a built-in list of devices to target (ip_list). Whereas the Ansible playbook has a inventory file that must be configured separately.

Is it faster?
They both take about the same amount of time when run against the same hosts.

Is it more secure?
In this particular example, the Python script includes the username and password in plain-text. An alternative could be written to use input and getpass to securely get real credentials when ran. The Ansible playbook has a similar issue – but is usually circumvented by using the -u and -k arguments to provide a username and prompt for password when executing the playbook. You could also use an ansible-vault file to more securely store the credentials.

Is it scaleable?
The Python script could be scaled using a service or cronjob that runs periodically. The Ansible playbook can be run with a service, or more commonly, through Ansible Tower (which can also hold your credentials in a secure profile). This would provide a GUI output with statuses and reports about the playbook’s successes and failures.

Conclusion:

For this particular use case, I’d go with the Ansible playbook for better automation integration with Ansible Tower as the central running platform. Notwithstanding, some tasks still have easier integration with Python (in my opinion)!

Pick one and get started today! You’ll thank yourself later!