Skip to content
Exporting to Ansible

Bringing servers under Ansible

A server set up by hand, or one that drifted far from its roles, is hard to rebuild. lemano ansible export writes what Lemano last read on it as an ordinary Ansible project: roles, a playbook, an inventory and a README. The project is yours: edit it, template it, commit it.

Before you start

  • lemano and lemano-ansible installed on the controller, and an inventory that reaches the servers.
  • A recent read of each server:
lemano ansible discover -i inventory.yml web1 web2

Export

lemano ansible export -i inventory.yml --limit web1:web2 --out ./ansible-web

The summary says what went where, and what needs you:

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

Only what matters on each host is exported: packages installed by hand (not those of the system, not libraries), configuration files changed or added, services whose unit was written on the host, accounts of people, their keys and sudo rules, cron files, kernel settings files and fstab mounts. --all exports everything else too.

Users’ own files

Lemano does not read the files in users’ homes unless a target names the users, so a server move leaves their shell settings, ~/.ssh/config and the like behind; the README lists the users whose files were not read. To move them, name the users in the target file and read the hosts again before you export:

spec:
  discovery:
    userFiles:
      users: [alice, deploy]
      paths: [.config/nvim/init.lua]

When the hosts come from an Ansible inventory, give the same mapping in the variable lemano_user_files, for a host or a group:

all:
  vars:
    lemano_user_files:
      users: [alice, deploy]

Their files then go in the files role, after the accounts, with their owner and mode. Private keys, command histories and other files that hold credentials are never read: copy the keys you need by hand, or make new ones on the new machine.

Read the README

README.md in the project lists:

  • the roles and the hosts each one runs on;
  • the secrets to provide (next section);
  • what was not exported: packages installed from local files, which no repository can install, and files whose content Lemano does not keep, which you copy by hand.

Secrets

Files that may hold secrets (sudo rules, authorized keys, crontabs, files only root can read) are never written in plain text. By default, each one becomes a variable you define; until you do, the play stops at its task with a message naming the file. Define them in host_vars/HOST.yml, encrypted with ansible-vault encrypt_string.

Or let the export encrypt them:

lemano ansible export -i inventory.yml --limit web1 --out ./ansible-web1 \
  --with-secrets --vault-password-file ~/.vault-pass

Account passwords are never exported: set them yourself.

Check before you apply

Run the project in check mode against the servers it came from: it should change nothing.

ansible-playbook -i ./ansible-web/inventory.yml ./ansible-web/site.yml --check --diff

To rebuild a server on a new machine, give an inventory that names the new machine with the exported host’s name, apply the project, and check the result with lemano ansible check: everything exported should be in sync.

ansible-playbook -i new.yml ./ansible-web/site.yml --vault-password-file ~/.vault-pass
lemano ansible check ./ansible-web/site.yml -i new.yml --vault-password-file ~/.vault-pass

Each role is tagged role_NAME: --tags role_users applies one role.

Next