Free tech learning for everyone, from basics to pro

Network AutomationNetworking SecurityIntermediate

Handling credentials in network automation: secrets managers, service accounts and least privilege

In short: Automation needs credentials to reach devices and APIs, which makes them valuable targets. Never store them in scripts or Git. Load them at run time from environment variables, encrypted files or, best, a secrets manager. Use dedicated automation accounts with only the permissions they need, rotate secrets regularly, and scan repositories for anything leaked.

The risk

A script with a hard-coded admin password, pushed to a shared Git repository, gives anyone with access the keys to the whole network. Leaked credentials in repositories are a very common cause of breaches.

Where automation credentials should liveSecrets managerVault, cloud KMSCI/CD secretsmasked variablesEnvironment variablesat run timeEncrypted filesAnsible Vault, SOPSNeverplain text in Git
Figure 1. The further up this list, the better the control, rotation and audit.

Options, from basic to best

  • Environment variables: set at run time and read with os.environ. Better than hard-coding, but they can appear in process listings or shell history if set carelessly.
  • Encrypted files: Ansible Vault or Mozilla SOPS encrypt values in files that can be stored in Git; the decryption key is kept elsewhere.
  • CI/CD secrets: pipeline platforms store secrets and inject them as masked variables during jobs.
  • Secrets managers: HashiCorp Vault, AWS Secrets Manager, Azure Key Vault and similar tools provide access control, audit logs, rotation and short-lived credentials.

Prompting for a password

import getpass
import os

username = os.environ.get("NET_USER") or input("Username: ")
password = os.environ.get("NET_PASS") or getpass.getpass("Password: ")

For interactive scripts, getpass hides the password as it is typed.

Automation accounts and least privilege

  • Create dedicated service accounts for automation, separate from personal accounts.
  • Use TACACS+ or RADIUS authorisation to limit commands: a backup job only needs read access, a compliance check only needs show commands.
  • Restrict where automation accounts can log in from (automation servers only).
  • Log and monitor their use.

Rotation and leaks

  • Rotate automation passwords and API tokens on a schedule and whenever staff leave.
  • Prefer SSH keys or short-lived tokens where devices support them.
  • Use secret scanning tools (for example gitleaks or built-in repository scanning) to catch credentials before they are pushed.
  • If a secret leaks, rotate it immediately; deleting the commit is not enough because history and copies remain.

Real-world example: the public repository

An engineer shared a useful backup script on a public code site, forgetting that the device password was in the file. Automated scanners found it within hours. Fortunately, management access was restricted to internal networks. The team rotated the password, moved credentials into Vault and added secret scanning to every repository.

Practice

Why is deleting a commit with a leaked password not enough?
The secret may remain in history, forks or copies; it must be rotated.
What advantages do secrets managers provide?
Access control, audit logs, rotation and sometimes short-lived credentials.
How can TACACS+ support least privilege for automation?
By authorising only the commands each automation account needs.

Key takeaways

  • Never store credentials in scripts or plain text in Git.
  • Use environment variables, encrypted files, CI secrets or a secrets manager.
  • Give automation dedicated, least-privilege accounts with restricted access.
  • Rotate secrets and scan repositories for leaks.
Tracks your learning on this device only. See your progress on the learning paths.
Verified author✓ Valid
Issued to
Mohamed Fayas
Role
Founder of FM TecHub
Credentials
CCNABTEC HND Network EngineeringBEng (Hons) Computer Networking & Cloud Security
Issuer
FM TecHub · fmtechub.com

He writes practical, plain-English guides on networking, security and cloud.

More about the author →

Original content written by Mohamed Fayas for FM TecHub. © 2026 FM TecHub. You're welcome to share a link to this page; please don't copy the text elsewhere without permission.

Email updates: coming soon

New notes, tools and security news from FM TecHub by email, explained simply. Free. We're not collecting email addresses yet; see our privacy policy.

PortServiceStatus
SMTP/25Email updatesopening soon

Meanwhile, stay connected: