Importing an Ansible inventory
If your servers are already in an Ansible inventory, you do not have to write
a target file for each of them. lemano ansible import reads the
inventory and turns every host into a target, with its address, port, user,
key file, jump host and groups. Ansible stays optional: Lemano needs it only
to read the inventory, when you run the import.
The import is the Ansible integration, lemano-ansible, a program that is
installed in the same folder as lemano and released with it. lemano ansible ... runs it; lemano itself knows nothing of Ansible.
Before you start
ansible-coreon the machine where you run the import: the import runs itsansible-inventoryandansiblecommands. Lemano has no inventory parser of its own. Ansible evaluates INI and YAML files, dynamic inventories, plugins, templates, group and host variables and vault, and Lemano uses the result. A parser of its own would get some of these wrong without saying so.- Or the output of
ansible-inventory --list, saved on a machine that has Ansible, and read with--from-json(see Without Ansible here).
Preview, then write
lemano ansible import -i inventories/prod/hosts.ymlWithout --out the targets are printed and nothing is written: the
documents go to standard output, so they can be piped or redirected, and the
report goes to standard error. Read them, then write them:
lemano ansible import -i inventories/prod/hosts.yml --out targets/prod.yaml-i and --limit are passed to Ansible as they are, so -i can be given
more than once, can name a directory, and --limit takes any Ansible host
pattern (web:&eu). Without -i, Ansible’s own default applies
(ansible.cfg, ANSIBLE_INVENTORY).
What the report says
For an inventory whose hosts all import cleanly, the report is one line, and the targets are on standard output:
Preview: 3 targets from the 3 hosts of the inventory; nothing was written (give --out FILE to write them)An inventory with more in it gets one section for each thing worth knowing. This one has most of them at once:
Preview: 15 targets from the 19 hosts of the inventory; nothing was written (give --out FILE to write them)
names changed to valid target names (4):
My Host -> my-host (made a valid target name; inventory name in a comment: it cannot be a label value)
WEB-01 -> web-01-859d (another inventory name maps to the same target name: a hash of this one is added; inventory name in the label ansible.lemano.host)
Web_Odd.Name -> web-odd.name (made a valid target name; inventory name in the label ansible.lemano.host)
web_01 -> web-01-1bcc (another inventory name maps to the same target name: a hash of this one is added; inventory name in the label ansible.lemano.host)
not reached over SSH, so not targets (3):
d-01: ansible_connection is community.docker.docker, not SSH
l-01: ansible_connection is local, not SSH
w-01: ansible_connection is winrm, not SSH
could not be imported: fix the inventory, or the value named (1):
x-01: Ansible could not evaluate its variables: Error processing keyword 'remote_user': 'undefined_thing' is undefined
groups that cannot be a label, left out of the labels (1):
host (1 host): the label ansible.lemano.host holds the inventory name of a host whose target name differs
in the targets as ansible:// references:
ansible_password: password taken from the inventory at each run, never stored (p-01)
not stored or not imported:
ProxyCommand: looks like it holds a password or a token and is not imported: the target has no proxyCommand (p-01)
name: cannot be a label value: the inventory name is in a comment of the target (My Host)With --out, the first line says what was written
(Wrote targets/prod.yaml: 15 added, 0 updated, 0 unchanged, 0 gone from the inventory (19 hosts in the inventory)), and -o json and -o yaml give the
whole report for scripts. This example exits with status 7, because of
x-01: see Hosts that are not imported.
How the values are computed
The import asks Ansible twice:
ansible-inventory --listfor the hosts and the groups.ansiblewith thedebugaction, for the final value of the connection variables of every host (ansible_host,ansible_port,ansible_user, the key file, the ssh arguments, the connection and the become settings).
The second step is what makes the values right. ansible-inventory --list
prints variables as they are written: a template stays {{ ... }}, and a
vault-encrypted value stays an encrypted blob. The step renders and decrypts
them, with the same settings your own runs have: ansible.cfg, ANSIBLE_*
variables, vault password files and ids. It does not connect to any host:
the debug action runs on your machine (checked on ansible-core 2.21 with
unreachable addresses: no connection is made), and it takes about a second
for a dozen hosts.
From ansible-core 2.22, Ansible replaces the value of every secret (a vault
plaintext, a password) by $REDACTED$ in what it prints and hands to
callbacks, wherever the value appears in a string. Lemano therefore reads the
evaluation through a small output plugin of its own, which takes the message
from the task’s arguments. If a value it reads still holds $REDACTED$, the
command stops with an error and writes nothing: it never takes a masked value
for the real one.
- Both commands run with your environment and with their standard input closed, so they never wait for a prompt. A vault password has to come from a file or the environment, not from a prompt.
- Evaluating runs the lookups and filters your inventory contains (a
lookup('pipe', ...), for one), on this machine, as any use of that inventory does. Read an inventory you did not write as you would read a script. - The import asks for the connection variables only, never for a password
variable. Ansible is started so that it records nothing it prints or
computes, whatever your
ansible.cfgsays: the log file is/dev/null, and no callback is loaded, so that a result logger, a notifier or a run recorder you have enabled for your own runs does not copy the values (ansible-inventory --listprints every variable as written, plain-text passwords included). The screen output is the plainminimalone, which is the one Lemano reads. - Settings that Ansible applies to every host from its configuration are
used when the host’s variables do not set them, so that Lemano connects as
Ansible would: the remote user (
remote_userinansible.cfg, orANSIBLE_REMOTE_USER), the private key file (private_key_file), the port (remote_port), the connectiontimeoutandssh_common_argsandssh_extra_argsfrom[ssh_connection](which is where a jump host or aProxyCommandis set for a whole fleet). Lemano asks Ansible for the effective value, whatever its source, and never readsansible.cfgitself. An inventory variable wins over the configuration, setting by setting, as in Ansible. A jump host from the configuration’s ssh arguments is noted in a comment on the target. Command line options of your ownansibleruns (--user,--private-key,--timeout) are not part of the configuration and are not seen.
A host whose variables Ansible cannot evaluate (an undefined variable, a vault value and no vault password) is listed with Ansible’s one-line message: fix it, as Ansible itself would need you to.
What each host becomes
| In the inventory | In the target |
|---|---|
| the inventory name | metadata.name, made a valid target name when it is not one (see below) |
ansible_host, else the name | spec.connector.host |
ansible_port | spec.connector.port |
ansible_user | spec.connector.user |
ansible_ssh_private_key_file | spec.connector.credential: file:<absolute path> |
ansible_ssh_timeout, or Ansible’s timeout setting, when it is not 10 seconds | spec.connector.timeouts.connect: the limit for opening the connection, for a slow link or a loaded host |
-J or -o ProxyJump= in ansible_ssh_common_args or ansible_ssh_extra_args | spec.connector.proxyJump |
-o ProxyCommand= in the same arguments | spec.connector.proxyCommand |
ansible_password (or ansible_ssh_pass, ansible_ssh_password) | spec.connector.password: ansible://<host>?config=<file>&inventory=<source>, a reference: see Passwords from the inventory |
ansible_become_password (or ansible_become_pass, ansible_sudo_pass) | spec.connector.sudoPassword, the same kind of reference |
ansible_become: false, or ansible_become_method other than sudo | spec.connector.privilege: user, with a comment |
lemano_user_files (users, preset, paths) | spec.discovery.userFiles: the users whose own files are read |
| each group the host is in | the label ansible.lemano.<group>: "true" |
The older names ansible_ssh_host, ansible_ssh_port, ansible_ssh_user and
ansible_private_key_file are read too, in the order Ansible reads them. A
setting the inventory does not give is left out of the target, so the
defaults apply: port 22 and, for the user, lemano, the dedicated account
Lemano is meant to use (Ansible would use your own user name).
Notes on the mapping:
- Key files.
~and~/are your home directory,~user/is that user’s home directory in the system’s user database, and a relative path is read against the directory you run the import in; the target holds the absolute path, and a comment says when it was relative. The key itself is never read at import time. A key with a passphrase is used through the agent only. - Jump hosts. The syntax is OpenSSH’s:
[user@]host[:port], hops separated by commas. If the arguments name both a jump host and aProxyCommand, the first one wins, as it does in OpenSSH, and the other is listed as ignored. - Privilege. Lemano escalates with
sudo -nonly. A host that Ansible does not escalate (ansible_become: false) or escalates withsu,doasor another method is declaredprivilege: user: its pass runs as the login user and what needs root is recorded as skipped. - Other SSH options (
-o ControlMaster=auto,-p,-iand so on) are listed by name as not imported. The host key options (StrictHostKeyChecking,UserKnownHostsFile,HostKeyAliasand the like) are ignored on purpose: Lemano pins every host key itself and refuses a changed one, whatever the inventory says.
Host names
A target name is lowercase letters, digits, - and ., starting and ending
with a letter or a digit. An inventory name can be anything: Ansible keeps its
case, and web_01 or Web-01 are common. So every inventory name is mapped
to a valid target name, and no host is left out for its name:
- a name that is already valid is never changed;
- otherwise it is lowercased, each character a target name does not allow
becomes
-, runs of-become one, and the ends are trimmed (Web_Odd.Nameisweb-odd.name,My Hostismy-host); - the inventory name is kept in the label
ansible.lemano.host, when it can be a label value (letters, digits,-,_and., at most 63 characters), so thatlemano discover -l ansible.lemano.host=web_01finds it, and otherwise in a comment on its document, which the report says; - when two inventory names map to the same target name, the one that is already
valid keeps it (otherwise the first in sorted order) and each other gets
-and four hexadecimal digits of the SHA-256 of its own inventory name (web-01-1bcc). The suffix depends only on the inventory names, so it is the same at every import. (A longer suffix is used if a host happens to have the shortened name already.)
The report lists every name that changed. The host the target connects to is
ansible_host, else the inventory name as written, so a renamed target still
reaches the right server.
Groups are labels
Each group a host is in, directly or through a child group (all and
ungrouped do not count), becomes a label:
metadata:
name: web-01
labels:
ansible.lemano.eu-west.1: "true"
ansible.lemano.internal: "true"
ansible.lemano.web: "true"so that you select by group the way you select by any label:
lemano discover -l ansible.lemano.web=true
lemano target list -l ansible.lemano.internal=true,ansible.lemano.eu-west.1=trueA label name is letters, digits, -, _ and ., starting and ending with a
letter or a digit, and at most 63 characters; with the prefix that leaves 48
for the group’s name. A group whose name does not fit (it ends in _, or has
a space) is reported and left out of the labels: Lemano never changes a name
to make it fit, as two groups could then end up with one label. Its hosts are
imported without it. A group called host would collide with the label that
holds a renamed host’s name, and is reported the same way.
The prefix ansible.lemano. belongs to the import. A target file that is not
generated by it cannot declare a label that starts with it, and the loader
says so; this keeps a label you wrote from being taken for an imported one.
Passwords from the inventory
Keys are recommended, but some hosts take a password, and the inventory
already has it, often vault-encrypted. The import never reads that value.
When a host sets ansible_password (or ansible_ssh_pass,
ansible_ssh_password), its target gets password, and when it sets
ansible_become_password (or ansible_become_pass, ansible_sudo_pass),
sudoPassword, each as a reference to the inventory:
spec:
connector:
type: ssh
host: 192.0.2.30
password: ansible://web-pw?config=../ansible/ansible.cfg&inventory=../ansible/hosts.yml
sudoPassword: ansible://web-pw?config=../ansible/ansible.cfg&inventory=../ansible/hosts.ymlThe reference names the host as the inventory writes it, the Ansible
configuration file that was in effect when you ran the import (config=, as
ansible --version reports it: ANSIBLE_CONFIG, an ansible.cfg in the
folder you ran it in, ~/.ansible.cfg or /etc/ansible/ansible.cfg; none
when there was none), and the inventory sources you gave with -i. Paths are
relative to the target file’s folder, as file: paths are (a preview, which
has no file, writes absolute paths). Without -i the reference names no
inventory, and Ansible’s own default applies, which the configuration file
usually sets. The report lists these variables in a section of their own, as
information: the password is taken from the inventory at each run.
When Lemano needs the passwords of a target, it asks Ansible for them, the
way the import evaluates the connection variables: the debug action, which
makes no connection, with the configuration file the reference names, your
ANSIBLE_* variables and your vault settings. So a vault-encrypted or
templated password is decrypted or rendered by Ansible, and a password you
change in the inventory is the one used at the next run, without importing
again.
Where Ansible runs. Lemano passes the configuration file explicitly (
ANSIBLE_CONFIG, which wins over a variable of yours) and runs Ansible in the folder that file is in, so the paths in it, a vault password file or an inventory, mean what they meant at the import, wherever you runlemanofrom. The inventories are paths relative to the target file, so they do not depend on the folder either. Only agroup_varsor a lookup that is found relative to the folder you imported from, when it is not the configuration file’s own, is not seen at run time. With no configuration file, Ansible runs in the folder you runlemanoin. Importing again after moving the configuration file updates the reference.One run for both passwords. A pass that needs both the login and the sudo password of one host and inventory asks Ansible once. It asks before the connection starts, so a slow inventory does not count against the connect timeout (10 seconds by default); Ansible itself is stopped after a minute. The price is that it runs at every pass, for every target that has such a reference, whether or not the password turns out to be needed.
When the password is used. The login password is offered after the key attempts and only to a host whose key is pinned; the sudo password only where
sudo -nis refused. If Ansible could not give a password, that is kept until the password is needed: a target whose key works does not fail for it.The value lives in Lemano’s memory, in a buffer that is overwritten after use. It is not stored, logged, shown in an error or put on a command line, and it is not copied into a file by Ansible either: Ansible is started with its log file off and no callback loaded or enabled, whatever your
ansible.cfgsays (see the security model).Ansible must be installed where Lemano runs, and the configuration file the reference names must still be there. Your vault password has to come from a file or the environment, as for the import.
When Ansible is missing, has no vault password for the value, or the host has none of the variables set, and the password is needed, the connection fails with a credential error (exit status 3 for the login password) that names the host and the variables and says what is wrong, never a value. For the sudo password the pass runs as the login user and is partial (exit status 7), with the same message among its errors.
For an inventory that takes more than a minute to read, store the password with
lemano secret setand usesecret://instead.Only the host names that are plain names can be referenced: letters, digits and
.,_,-,+,@. Ansible reads:,,,*,[and!in a name as part of a host pattern, and Lemano never asks for the password of a pattern that could reach other hosts. Such a host is imported, and the report names its password variables as a warning.No reference is written from
--from-json, which cannot be asked again; nor asudoPasswordfor a host whose privilege isuser. They are warnings. Writepassword: env:NAME,file:orsecret://by hand.Which variable wins. When a host sets several, Lemano uses the one Ansible uses. Ansible 2.21 was observed, with each variable holding another value: for the login
ansible_ssh_passwordwins overansible_ssh_pass, which wins overansible_password; for sudoansible_sudo_passwins overansible_become_pass, which wins overansible_become_password. A variable that is null or undefined does not count; an empty one does, as it does for Ansible, and gives no password.ANSIBLE_BECOME_PASSand the[sudo_become_plugin]setting of the configuration are not read: only variables of the inventory (a variable also wins over them in Ansible).
What is never stored
Lemano holds an inventory’s variables in memory while it works and writes only the connection fields above: nothing else is written to a file or a log, or shown in an error.
- Passwords are never read at import time and never stored. The login and
sudo password variables become references, as above. The others
(
ansible_sudo_password,ansible_su_pass, the key and passphrase variables and their older names) are never asked for: a host that has one is imported, and the report names the variable and the host, never the value. For the login, a key file or the agent is the better choice. - A
ProxyCommandthat containssshpass,password,passwd,passphrase,secretortoken, or that passes a credential as an argument (-u user:pass, an authorization header, a variable such asPROXY_KEY=), is not imported: the host is imported without it, with a comment and a warning, since a password in a command line would be stored in the file. Its target will not reach a host that needs the proxy until you write one that does not carry the secret. - When
ansible-inventoryoransiblefails, the error carries its exit status and not its own error output, which can quote lines of the inventory. Run the same command yourself to read it: the error prints it. The one-line message Ansible gives for a host it could not evaluate is shown, since it names the problem and holds no value.
Hosts that are not imported
A host is left out of the targets in two cases only, and the report says which:
- It is not reached over SSH. Its
ansible_connectionislocal,winrm,docker, and so on (ssh,smartandparamikoare SSH). - It cannot be a target as it is, and only a fix in the inventory helps: Ansible could not evaluate its variables; its port is not a number from 1 to 65535; its ssh arguments have an unterminated quote or a jump host that is not valid; a variable is a list where text is meant; a key path names a user that does not exist on this machine; its host or user is something the loader refuses. The report names the host and the variable to fix, and never repeats a value that could be a secret; no default is substituted for a value that is wrong.
Exit status. The command prints everything, and with --out writes what it
could, and then exits 7 (partial, the code discover uses for a partial
pass) when at least one host could not be imported. A host that is not reached
over SSH is not a failure and does not change the exit status. A script can
therefore tell a complete import (0) from one that needs a fix (7).
A host that an earlier import wrote and that cannot be imported this time is
kept as it was in the file, with a warning: a transient problem never
deletes a target, and --prune never drops it.
Without Ansible here
--from-json reads the output of ansible-inventory --list saved where Ansible
is installed:
ansible-inventory -i inventories/prod --list > inventory.json
lemano ansible import --from-json inventory.jsonDo not save it with --export: that form leaves group variables out of
the hosts’ variables, and Lemano refuses it. Nothing is run, so nothing
evaluates the variables, and the Ansible configuration is not asked: a
host whose connection variables are templates or vault values is listed as
one that could not be imported, with the reason, rather than guessed. Only
Ansible can resolve them, and the generated target file is already the
resolved form, with the final values and no password (a password variable
stays a warning there: the saved output cannot be asked again, so no
reference is written): run the import on a machine that has Ansible
installed, without --from-json, and copy the target file it generates to
where it is needed. Everything else in a saved output is imported as
above.
Importing again
The file starts with a marker line:
# Generated by "lemano ansible import". The next import rewrites this file; change the inventory instead.
# Source: ansible-inventory --list --inventory=inventories/prod/hosts.yml
# Imported: 2026-10-27Run the import again when the inventory changes. The file is rewritten:
- hosts that are new are added, hosts that changed are updated, and hosts that did not change are left as they were;
- hosts that the file holds and the inventory no longer has are reported as
gone from the inventory and stay in the file, so that a wrong
-inever deletes your targets.--prunedrops exactly these; - a host that is still in the inventory but could not be imported is kept as it was, as said above;
- when nothing would change, the file is not written.
Change a target in the inventory, not in the generated file: the next import replaces it. To keep a target as you wrote it, write it in a file of your own.
An import never touches a file it did not generate: a file at --out
without the marker line is refused. Targets that you wrote by hand are
therefore safe, wherever they are. Before it writes, the import also reads
the target files in the directory of --out, as discover does, and stops,
writing nothing, if a name it would write is declared there: two targets
cannot share a name.
error: 1 target name(s) are already declared in other files, so nothing was written to targets/hosts.yaml:
web-01: targets/mine.yaml:1
Remove them from those files, or import to another directory with --outA file in that directory that does not load cannot be checked; the report
lists it, and discover will refuse it too.
An inventory with no host at all is an error. Ansible prints an empty inventory, without failing, when it cannot read a path, and importing that would report every host as gone.
Limits
- One file holds at most 1000 targets and 1 MiB, which the loader enforces.
For a larger inventory import in parts, each with its own
--limitand--out. - A host that two imports both write is a conflict: give each import its own hosts.
Jump hosts and ProxyCommand
A proxyJump makes Lemano reach the host through the jump hosts, and pins
each hop’s host key like the host’s own. A proxyCommand runs a command on
the machine where lemano runs, with your rights, to carry the connection.
That is what ansible_ssh_common_args: '-o ProxyCommand="ssh -W %h:%p -q user@gateway"' makes Ansible do, and what an inventory that you did not
write can make it do. Read the generated file before you run discover on
it, as you would read a script. See the
target file reference.