What this is #
Ansible automation for APC Smart-UPS units that each have an AP9641 Network Management Card 3 (NMC3), the card that gives a UPS its own web interface, SSH login, and alerts. It replaces a manual per-device checklist (accounts, DNS, NTP, time zone, daylight saving time, low battery duration, shutdown behavior, UPS name, and CEIP off) and handles firmware for both the network card and the UPS. A new unit now takes a short session at the serial console, an entry in the inventory, and one command.
It runs on a corporate network, under one rule: nothing is sent to outside services. That shaped the whole design: plain ansible-core and pexpect, no Galaxy collections or telemetry, an encrypted vault, and strict SSH host key checking.
Claude wrote the playbooks. I set the requirements and the security rules, made the design decisions, tested every playbook against real hardware, and approved the result. The full story, including what went wrong, is in the blog post Ansible vs. a Rack of Stubborn UPS Cards. The source is public at github.com/zle3/apc-ups-ansible, under the MIT license.
By the numbers #
- 4 UPS units onboarded in one run of
onboard.yml, with no failures. - Drift fixed on 2 hand-configured cards: wrong time zone, no DNS servers, NTP off (one clock hours off and on the wrong date), low battery at 2 minutes instead of 7, a default UPS name, and DST off. A third card had outdated NTP servers.
- 4 network cards updated from 2.5.3.3 to 3.6.1.2, one at a time, in a single step each.
- 10 playbooks and 2 roles, 1,411 lines of YAML and Python, tested against a fake card before touching hardware.
What it does #
| Playbook | What it does | Changes devices? |
|---|---|---|
onboard.yml |
New devices in one command: trust keys, accounts, configure, firmware, backup, report | Yes |
configure.yml |
DNS, NTP, time zone, DST, UPS name, low battery, shutdown behavior, CEIP off | Only what differs |
firmware.yml |
Card firmware, then UPS firmware, to the newest version available | Yes, reboots the card |
accounts.yml |
Creates the automation and admin accounts if missing | Yes |
backup.yml |
Copies each card’s config.ini to a private, git-ignored folder |
No |
report.yml |
CSV of models, serial numbers, MAC addresses, and firmware for the asset system | No |
trust_host_keys.yml |
Scans SSH host keys and checks them against recorded fingerprints | Local known_hosts only |
Every playbook supports --check (a dry run that reads and compares without changing anything), and a second run reports ok instead of changed when nothing needs doing.
Architecture #
No agent on the device. The NMC is an appliance with a menu-driven CLI and no Python, so standard Ansible modules can’t run on it, and network_cli has no “cliconf” plugin for APC. Every task runs on the control machine (delegate_to: localhost) and drives the card’s SSH session with ansible.builtin.expect.
Read, compare, change, verify. Without modules to provide idempotency, every task follows the same pattern: read the current state, compare it with the wanted state, change only what differs, and verify the result.
Settings with no CLI command. DST, low battery duration, shutdown behavior, UPS name, and CEIP have no CLI command. The card accepts a partial config.ini upload and leaves other settings alone, so the playbook downloads the current file, compares it with Ansible’s ini lookup, uploads only the changed keys, and downloads again to verify. Partial uploads were proven safe by changing a value by hand, letting the playbook restore it, and diffing full backups from before and after.
Inventory by type and platform. Devices are grouped by type (ups: what the device should do) and by platform (apc_nmc3: how Ansible talks to it), with site-wide settings in group_vars/all. Adding another kind of device means new groups and a role with the same task names.
Firmware planning. Devices go to the newest firmware available. If a vendor requires a step upgrade, the in-between version is listed as a waypoint and installed only when it lies between the current and target versions, compared with Ansible’s version test. Vendor zips dropped in a folder are unpacked with Python’s zipfile, through a nested self-extracting archive, without executing anything.

Infrastructure and security #
- ansible-core and pexpect only. No Galaxy collections, callback plugins, or telemetry. Nothing leaves the network except traffic to the devices in the inventory.
- Strict host key checking, verified out of band. Each card’s fingerprint is recorded at the serial console during setup with
ssh key -fand stored in the inventory. The playbook trusts a matching key without prompting, stops a card whose key doesn’t match, and never replaces a changed key on a known device automatically. - Separate accounts. The built-in Super User is break-glass only and used just to create accounts. A dedicated automation account does the daily work.
- Secrets. Passwords live only in an encrypted Ansible Vault, tasks that send them use
no_log, and SSH allows one password attempt so a wrong one can’t lock an account. - Backups. Each card’s
config.iniis stored withchmod 600and git-ignored, since it can contain secrets. - Vendor telemetry off. Schneider’s CEIP usage reporting is forced off on every card.
Challenges, briefly #
- Exit codes that mean nothing. The card ends every SSH session with exit code 255, even on success, and its scp exits 1 after a complete transfer. Success is judged from the output: a clean session close, no
E1xx:error codes, and a100%transfer line plus a file check. - Dictionary keys aren’t templated. A prompt written as a variable never matched, so the
apc>prompt is written literally in every task. - Native types. Since Ansible 2.19, a templated number renders as an integer while the
inilookup returns a string, so comparisons cast both sides with| string. - Vendor documents that disagree. The CLI guide covers UPS firmware only from USB, a separate FAQ covers the network method, and the card firmware FAQ names a vendor tool as the only supported method while the download ships its own SCP updater. A plain
scpupload worked. - Reboot timing. After a card firmware update, the card needs time before it talks to the UPS again, so the UPS read retries for up to 5 minutes.
- Key scanning. The cards’ SSH server answers nothing when
ssh-keyscanasks for several key types at once, so each type is requested separately.
Tech stack #
Ansible (ansible-core 2.22), pexpect, Ansible Vault, Python, OpenSSH, WSL (Ubuntu), APC AP9641 NMC3, Claude Code.
Where it stands #
In use for all four units, with documentation written for someone who has never used Ansible or these cards: a new-device procedure, password rotation, restore, and a troubleshooting table. The documented way to catch new firmware is a Schneider Security Notifications subscription, then firmware.yml --check to show which devices are behind. Next on the list is SNMP trap configuration on each card.


