lemano ansible export
Writes a ready-to-run Ansible project into DIR from what Lemano last
read on each host: roles, a playbook, an inventory and a README. The
guide walks
through a first export.
lemano ansible export --out DIR [-i INVENTORY]... [--limit PATTERN]
[--all] [--pin-versions]
[--with-secrets [--vault-password-file FILE]]
[-o json|yaml]The flags and examples of the command are also on its generated page.
Hosts
The hosts are the inventory’s, as Ansible reads it (-i, repeatable;
without, Ansible’s configured inventory), narrowed by --limit as
ansible-playbook --limit takes it. --vault-id and --vault-password-file (repeatable) decrypt a vaulted inventory; --vault-password-file also encrypts the secrets with --with-secrets. Each host’s latest generation is
used: read the hosts first with lemano ansible discover.
A host with no target is named on standard error and left out (exit 7).
--out must name a new or empty folder: nothing is overwritten.
The project
DIR/
README.md how to run it, the secrets to provide, what was left out
ansible.cfg SSH keepalives, pipelining, and handlers run even after a failure
site.yml one play, every role, tagged role_NAME
inventory.yml the hosts, with the address, port and user Ansible uses
group_vars/all.yml
host_vars/HOST.yml what differs on HOST, its roles, its Vault values
host_files/HOST/ the files that differ on HOST, or describe it
roles/, in the order they run:
package_sources/ what the package manager reads: sources, keys, settings
packages/ packages installed by hand
users/ accounts, groups, authorized keys, sudo rules
checkouts/ version-control checkouts
files/ other configuration files changed or added
NAME/ a service: its package, its files, whether it starts at boot
system/ kernel settings files, cron files, network mounts, swap files
ssh_server/ the SSH server's configuration
firewall/ firewall rules, policies and configuration files
roles/NAME/tasks/start.yml, run from post_tasks once every role has run:
starts or stops the serviceThe order is what makes the project work on a machine that has nothing yet:
- The package sources come first, and the package cache is refreshed when they change, so that a package from a third-party repository can install.
- The packages come before the accounts, since a package makes its own groups.
- What can cut the play’s connection comes last among the roles. Each SSH
configuration file is tested with
sshd -tbefore it is put in place, and the whole configuration before it takes effect. Under socket activation, where systemd holds the port, the unit generators run again and the socket restarts. The play then waits for the server on its ports, and only then does the firewall role run. ufw is enabled once its rules and policies are in place, and only when the project lets in the SSH server’s port. - The services start last, as at boot: a service starts once the
configuration it reads is in place, the firewall’s included. Its role
installs it, writes its files and sets whether it starts at boot; the
step that starts or stops it waits until every role has run. The
services start in the order of their units (
After=, or the other units’Before=, also through a target or another unit that one is before and the other after) on any host, even through services that are not exported. Records read by an older version hold less of this order, and units ordered in a cycle have none to follow: those services start in the order of their names, and the project’s README lists them. Until then, a changed file restarts a service only if it is already running.
On a machine that listens on another port, connect the first run to the port
it has now: the play goes on over the connection it already has. Afterwards,
set ansible_port to the new port and accept the host key for that port. The
project’s README says so, with the port, when the port is not 22.
ansible.cfg turns on pipelining: Ansible sends each module over the open
SSH connection instead of copying it to the host first, which saves several
round trips per task, most of a play’s time on a slow network. Pipelining
needs sudo without requiretty on the hosts. Debian, Ubuntu (with sudo or
sudo-rs) and the RHEL family ship without it. On a host whose sudoers has
Defaults requiretty, the play’s first task, a module run with privilege,
fails with sudo’s “you must have a tty”; the play then sets
ansible_pipelining: false for that host alone, prints one line naming it,
and gathers facts and goes on without pipelining there. Any other failure of
that task stops the host as usual. The setting is in the generated project only: lemano ansible check and lemano ansible record run your own playbooks with your own
configuration.
In check mode nothing is written, so a task can fail only because what an
earlier task would have done is not there yet. Each role but package_sources
sets ignore_errors: "{{ ansible_check_mode }}": such failures are shown and
counted as ignored, and a secret still to define is named the same way. A real
run stops at any failure.
Each role has tasks/main.yml, and defaults/main.yml, files/ and
handlers/main.yml when it needs them. Files are written as plain files
(ansible.builtin.copy), never as templates. The project uses
ansible.builtin modules, and community.general.ufw,
ansible.posix.firewalld and ansible.posix.mount when it has ufw rules,
firewalld rules, fstab mounts or swap files: requirements.yml then lists their
collections.
| Object | Task |
|---|---|
| Package | one ansible.builtin.package task with the list of names, state: present; with --pin-versions, name=VERSION (apt) or name-VERSION (dnf) |
| FirewallRule (ufw) | community.general.ufw with the rule, direction, protocol, port, source, destination and comment; the ufw package is installed first, even when the source’s image came with it, since the module needs it |
| FirewallRule (firewalld) | ansible.posix.firewalld with the zone and the service, port or rich rule, permanent and immediate; the firewalld package is installed first, as for ufw |
| nftables rule set | /etc/nftables.conf (or /etc/sysconfig/nftables.conf), as a whole file in the firewall role, when it changed |
| File | ansible.builtin.copy with the owner, group and mode Lemano read; the folders Lemano read first, with ansible.builtin.file |
| Service | ansible.builtin.systemd_service with its enabled and running state, or masked; its package, its unit or drop-ins, the programs its written units start that no package owns, with their mode; and a handler that restarts it, only when the host ran it |
| User, Group | ansible.builtin.user and ansible.builtin.group with the groups, home and shell; never a password. An ID outside the range of ordinary accounts (1000 to 60000, or the host’s UID_MIN and UID_MAX for accounts) is not written: the task says system: true, and the machine assigns one |
| SshAuthorizedKey | the authorized_keys file, in a folder of mode 0700 |
| SudoRule | the file of /etc/sudoers.d, checked with visudo -cf before it is put in place |
| Cron | the cron file |
| Sysctl | the file of /etc/sysctl.d, loaded with sysctl -p when it changes |
| Mount | ansible.posix.mount for an fstab entry that names no local disk (a network file system, tmpfs, a bind mount), always per host |
| Swap | a swap file of /etc/fstab, per host: made with dd of the size Lemano read when it does not exist (creates:), mode 0600 for root, formatted with mkswap and turned on with swapon only when the task made it (and only if it was in use on the host), and its fstab line written with ansible.posix.mount (path: none, fstype: swap) |
| Checkout | one ansible.builtin.git task with the remote, the folder and the commit the host is at, per host, in the checkouts role; never its files. A checkout with no remote, or not a git one, is listed to copy by hand |
What is exported
By default, what matters on each host: the objects
lemano ansible check would
list as forgotten if no task managed them, chosen the same way: the
export reads each host first (as the check does; --no-refresh skips
it), so what came with the system image, files made by package scripts
and files used by containers are counted, not exported, and a folder of
10 or more files no package owns is listed in the README to copy or
deploy by hand. --all exports every object, except the files the
system writes for itself, which are never exported.
Not exported, and listed in the project’s README:
- application data and containers: the containers a host runs and the data of its applications (databases, volumes, uploads) are not moved and need their own backup and restore; the README names the containers whose mounted folders Lemano read, and the end line says so;
- packages installed from a local file (
dpkg -i,rpm -i): no repository can install them; - files whose content Lemano does not keep (larger than the content limit, binary, or excluded by the target): copy them by hand;
- files that hold credentials (private keys, login files, command histories), which Lemano never reads: move or replace them by hand;
- the users’ own files in their homes, unless the target names the users
in
spec.discovery.userFiles: the README lists the users whose files were not read. The files a target names this way, or adds withincludePaths, are exported wherever they are, in the files role after the users role, with their owner, group and mode and the folders on their way; one that only its owner may read is a secret like any other; - firewall rules other than ufw’s rules and default policies and
firewalld’s services, ports and rich rules; a ufw rule tied to a network
interface or to a source application profile is not exported either.
ufw’s rules are written in the order ufw numbers them on the source,
since the first rule that matches decides; the kernel’s
live rules, which programs such as container engines load, are not
configuration: with
--allthey are listed; - mounts whose options hold a password;
- mounts of a local disk (by
UUID=,LABEL=or a/dev/path): they name a disk of that machine; - swap partitions and devices, for the same reason; a swap turned on by hand (nothing turns it on at boot); a swap a systemd unit or generator turns on (the unit or its configuration is a file);
- a program a written unit starts, when Lemano does not read its place or does
not keep its content, with the service named. A program under
/usr/bin,/usr/liband the like is taken for a package’s; - symbolic links, to make by hand.
ufw’s own files (/etc/ufw/user.rules, user6.rules, ufw.conf) are never
exported: the rules and the state they hold are tasks, and written back they
would undo those tasks. Its custom rules (before.rules, after.rules) go out
in the firewall role, and reload ufw when they change.
Another release or another CPU
The project can be applied to a machine of another release or architecture of the same family. The README says which system, release and architecture each host was read from, and the first task of the play prints them beside the new machine’s when they differ; it never stops the play.
- A package source of the distribution itself is not copied: the new machine
keeps the sources its image came with. A source counts as the distribution’s
when a package owns the key that signs it. A source naming no key is
verified by the keys apt trusts for every source; it counts as the
distribution’s when a package owns all of them and the source does not turn
the check off (
trusted=yes). It is listed under “Not exported”; redo a hand-made change in it on the new machine.lemano ansible checkandcodifyuse the same rule: such a source is counted as part of the distribution, never listed as forgotten. - In other sources, a suite equal to the exported host’s codename, or that
codename and a suffix such as
-updates, is written as the new machine’s release (ansible_facts.distribution_release), and anArchitectures:field orarch=option naming only the exported host’s architecture is dropped. Other suites (stable) and everything else are kept as they were. Such a file is a template. In dnf repository files, a literal architecture or release in an address is written from the new machine’s facts;$basearchand$releaseverstay.
Not promised: a third-party repository may not publish the new release or CPU;
a compiled program under /usr/local is not exported and runs on its own CPU
only; with --pin-versions the versions may not exist on another release; a
move between families is not supported.
Several hosts
Roles are shared. A file, a package or a setting identical on every host
of a role stays in the role. A list item only some hosts have goes to
host_vars/HOST.yml (as ROLE_LIST_host), a value that differs goes
there too, and a file that differs goes to host_files/HOST/, which the
role reads through playbook_dir. A role only some hosts need runs where
host_roles (in host_vars/HOST.yml) names it.
Files that describe one machine always stay per host, even when they are
identical: /etc/hostname, /etc/hosts and /etc/fstab.
The network configuration (/etc/netplan/, /etc/network/interfaces*,
NetworkManager connections, /etc/sysconfig/network*,
/etc/systemd/network/) is never exported: it names the hardware (a MAC
address, an interface name) and the addresses of one machine, and written
on another it can cut that machine off the network. It is listed under
“Not exported”.
A package source’s signing key is exported with the source when no
package owns it: the keys in /etc/apt/keyrings/, /etc/apt/trusted.gpg.d/
and /usr/share/keyrings/ (and a key a source names there) are public by
design and are written byte for byte before the package cache is
refreshed. A file in those folders that is not made only of public keys is
never exported. A key a source names that cannot be exported is listed
under “Not exported” with the source that needs it.
Secrets
The content of a file that may hold secrets is never written in plain
text. Such a file is one that only some users can read, one whose
content Lemano keeps sealed elsewhere (crontabs, sudo rules, fstab,
authorized keys, sshd_config), or one whose name or content looks like
it holds secrets, however readable it is: a .env file, a private key,
a password or token assigned in a configuration file (see
Secrets by pattern).
- By default, the task reads the content from a variable that the
role’s defaults define with Ansible’s
undef()and a hint: until you define it, a real run stops at that task with the hint. The README lists each variable with its file and hosts. - With
--with-secrets, the content is written inhost_vars/HOST.ymlas an Ansible Vault value (!vault, asansible-vault encrypt_stringwrites it), encrypted with a password read from--vault-password-fileor asked twice without echo. Run the playbook with--ask-vault-passor--vault-password-file.
Account passwords are never exported: Lemano holds only keyed hashes of
sensitive values. A user task without password leaves an account’s
password as it is.
Output
lemano ansible: exported 2 hosts into ./ansible-web
roles: packages, users, files, nginx, system
shared: 14 files, 6 packages; per host: 3 files (host_files/), 4 values (host_vars/)
not exported: 2 packages installed from local files, 1 file whose content is not kept (see README.md)
secrets: 2 files left as variables (see README.md, or rerun with --with-secrets for a faithful copy)
Lemano moves configuration, not application data
next: ansible-playbook -i ./ansible-web/inventory.yml ./ansible-web/site.yml --check-o json and -o yaml print the same summary as data.
Exit codes
| Code | Meaning |
|---|---|
| 0 | the project was written |
| 1 | an error, such as a store that cannot be opened |
| 2 | a usage error, such as an --out folder that is not empty |
| 7 | a host could not be exported; the others were |
| 3, 4, 5 | as for every command, by the reason the host’s fresh reading failed; 4 for a host key that is not pinned and not in your known_hosts files (give --accept-new-host-key to pin it after checking the fingerprint), or changed. The highest code of the hosts is returned |