lemano ansible import
Turns the hosts of an Ansible inventory into targets. Without --out it prints
them; with --out FILE it writes them to one generated file. It is a
command of lemano-ansible, the Ansible integration, which is installed
in the same folder as lemano and is run by lemano ansible. The
guide explains it with
examples; this page is the reference.
lemano ansible import [-i INVENTORY]... [--limit PATTERN] [--from-json FILE]
[--out FILE] [--prune] [-o json|yaml]The flags and examples of the command are also on its generated page.
Flags
| Flag | Default | Meaning |
|---|---|---|
-i, --inventory | Ansible’s default | An inventory file, directory or host list, as ansible-inventory -i takes it. Repeatable. |
--limit | all hosts | An Ansible host pattern, as ansible-inventory --limit takes it. |
--vault-id, --vault-password-file | Where the vault password is, as ansible-playbook takes them (repeatable). Given to ansible-inventory and to ansible. Without them, ANSIBLE_VAULT_PASSWORD_FILE and ansible.cfg apply. Not with --from-json. | |
--from-json | Read the output of ansible-inventory --list saved in this file, and run nothing. Not with -i, --limit or the vault options. Templates and vault values in it are not resolved, and the Ansible configuration is not asked: run the import where Ansible is installed and copy the generated target file instead. | |
--out | none: print | The file to write. It needs a value. Without it the targets go to standard output and nothing is written, read or checked on disk. |
--prune | off | Drop from the file the hosts that are gone from the inventory. Needs --out. |
-o, --output | a table | json or yaml: the report as an object (with the generated file in content for a preview). |
Flags go after ansible: lemano passes on to lemano-ansible what
follows the name and nothing before it, so a flag of lemano written
before the name (lemano --store FILE ansible ...) does not reach it. Use
the environment instead (LEMANO_STORE, LEMANO_SSH_CONFIG).
In a preview the targets are on standard output and the report on standard
error (a table), so the output can be piped or redirected. With -o json or
-o yaml everything is on standard output.
How the inventory is read
Two commands run from PATH, with your environment and their standard input
closed, each stopped after a time limit and with everything it started:
ansible-inventory --list --inventory=<value>for each-i, and--limit=<value>, for two minutes at most. Its output, up to 64 MiB, is parsed as JSON: the groups with theirhostsandchildren, and_meta.hostvars. The hosts are those the groups list: a host with no variable is not inhostvars.ansible all --inventory=<value>... --limit=<value> --module-name=debug --args=<JSON>, for five minutes at most. Thedebugaction runs on this machine and makes no connection. Its argument is one expression that has Ansible print, for each host, the final value of the variables in the table below, as JSON. Templates are rendered and vault values decrypted with the settings of your own runs; a value Ansible cannot compute makes that host fail, with a one-line message that is kept. No password variable is asked for.
Both are started with these entries added to the environment, so that
nothing Ansible prints or computes is recorded, whatever ansible.cfg says:
ANSIBLE_STDOUT_CALLBACK=minimal, ANSIBLE_NOCOLOR=1,
ANSIBLE_LOG_PATH=/dev/null, ANSIBLE_LOAD_CALLBACK_PLUGINS=false,
ANSIBLE_CALLBACKS_ENABLED=ansible.builtin.minimal (and its older name
ANSIBLE_CALLBACK_WHITELIST) and ANSIBLE_CALLBACK_PLUGINS=/dev/null.
Output made with --export is refused, since it leaves group variables out
of the hosts’ variables. With --from-json only the first step’s output
exists, and no variable is evaluated.
Variables read
Only these, and for each the first one that is set, in this order. When the
host sets none, the Ansible configuration’s value applies, where there is one
(last column); it is the effective value Ansible gives the ssh connection plugin
option through its config lookup (plugin_type='connection',
plugin_name='ssh'), from ansible.cfg or the environment, whichever applies,
and the configuration file is never read by Lemano. The column names the
ansible.cfg key and the environment variable that Ansible documents for it.
| Setting | Variables | Configuration (plugin option) |
|---|---|---|
| connection | ansible_connection (ssh, smart, paramiko and paramiko_ssh are SSH, with or without ansible.builtin.) | |
| host | ansible_host, ansible_ssh_host; else the inventory name | |
| port | ansible_port, ansible_ssh_port | port: [defaults] remote_port, ANSIBLE_REMOTE_PORT |
| user | ansible_user, ansible_ssh_user | remote_user: [defaults] remote_user, ANSIBLE_REMOTE_USER |
| key file | ansible_private_key_file, ansible_ssh_private_key_file | private_key_file: [defaults] private_key_file, ANSIBLE_PRIVATE_KEY_FILE |
| connect limit | ansible_ssh_timeout | timeout: [defaults] timeout or [ssh_connection] timeout, ANSIBLE_TIMEOUT, ANSIBLE_SSH_TIMEOUT |
| ssh arguments | ansible_ssh_common_args, then ansible_ssh_extra_args, split as a shell splits words | ssh_common_args and ssh_extra_args: [ssh_connection] keys of the same names, ANSIBLE_SSH_COMMON_ARGS, ANSIBLE_SSH_EXTRA_ARGS |
| privilege | ansible_become (a boolean as Ansible reads it), ansible_become_method | none: become is off by default whatever the plays do, so the configuration is no sign of intent |
| users’ own files | lemano_user_files, a mapping with the keys of spec.discovery.userFiles (users, preset, paths) | none |
lemano_user_files is a host or group variable like the others, so
group_vars can give one list of users to a whole group. It is evaluated by
Ansible with the rest, and a value that is not a mapping of those keys, or
names a user or a path the target file would refuse, leaves the host out
with the variable named and no value shown. Every command that builds
targets from an inventory (discover, check, export, codify, record
and import) reads it; the import writes it as spec.discovery.userFiles.
When a host’s target already has user files in the store, the variable
replaces them; a host that sets none keeps them.
Ansible itself fills ansible_user, ansible_port and
ansible_private_key_file from the configuration, so those come through the
variables; the ssh arguments and the connect limit are the settings the
configuration lookup adds. A value taken from the lookup because no variable
carries it is noted in a comment on the target (the connect limit only when it
is not the default below).
The connect limit is read in seconds, a whole number of 1 or more; any other
value makes the host not imported, with the variable named. It becomes
spec.connector.timeouts.connect, and is written only when it is not 10
seconds, which is both Ansible’s default and Lemano’s, so that most imported
files carry no timeouts. Ansible’s timeout also bounds how long a read on
an open connection may wait; Lemano takes it as the connect limit only and
keeps its own command and pass limits.
ssh_args (whose default is -C -o ControlMaster=auto ...) and command-line
options of your own runs (--user, --private-key, --timeout) are not read.
In the ssh arguments: -J and -o ProxyJump= give proxyJump; -o ProxyCommand= gives proxyCommand; when several are given the first wins
(and a ProxyJump and a ProxyCommand compete, the first of the two wins),
as in OpenSSH. Every other option is named in the report as not imported, by
keyword or flag and never by value; the options that concern host keys are
named as ignored.
No password variable is asked for or read. The report names the ones that are set (in the output of step 1) and the host:
ansible_password,ansible_ssh_pass,ansible_ssh_password(login) andansible_become_password,ansible_become_pass,ansible_sudo_pass(sudo) becomepasswordandsudoPassword, the referenceansible://<host>?config=<file>&inventory=<source>, where<host>is the inventory name,<file>the Ansible configuration file in effect (theconfig file =line ofansible --version, run in the folder the import runs in, only when a password variable is set; left out when there is none or Ansible cannot say), and each<source>an-ivalue, all percent-encoded: a path relative to the directory of--out(absolute without it), a host list (it has a comma) or a~path as given. The report lists them inreferenced, with the message “password taken from the inventory at each run, never stored”. A variable that is empty is ignored. No reference is written, and the variable is awarningsentry instead, with--from-json; for asudoPasswordwhen the privilege isuser; and for a host name with a character other than letters, digits and.,_,-,+,@.ansible_sudo_password,ansible_su_pass,ansible_su_password,ansible_private_key,ansible_ssh_private_key,ansible_private_key_passphraseandansible_ssh_private_key_passphraseare warnings only, as areansible_ssh_argsandansible_ssh_executable, which have no counterpart.
When a target needs the passwords of a reference, Lemano resolves them before
it connects, in one run for the login and the sudo password of the same
host, configuration file and inventories: ansible all --inventory=<source>... --limit=~^<host>$ --module-name=debug (the host’s name as a regular
expression, anchored), started with the environment above and
ANSIBLE_CONFIG=<file>, in the folder of that file (the current folder when
the reference has no config), for one minute at most. It prints one line:
lemano-password:, then the login password and the sudo password, each in
base64, or - when the host sets none or the password was not asked for.
Each password is the value of the variable Ansible prefers among those the
host sets (null and undefined do not count): ansible_ssh_password,
ansible_ssh_pass, ansible_password for the login, in that order, and
ansible_sudo_pass, ansible_become_pass, ansible_become_password for
sudo, as observed with ansible-core 2.21.5.
The target written
Each host is a document with metadata.name, spec.connector.type: ssh,
host, and, only when the inventory gives them, port, user,
credential: file:<absolute path>, password and sudoPassword (references
to the inventory, never passwords), proxyJump, proxyCommand and
privilege: user, and metadata.labels with ansible.lemano.<group>: "true"
for each group the host is in, directly or through child groups (all and
ungrouped excluded) whose name can be a label. Every document is read back
through the loader before it is written: what the loader refuses leaves the
host out, with the loader’s reason.
Hosts are written in target-name order, after three header lines: the marker, the source and the date.
Names
A name that is a valid target name is kept. Another is lowercased, each
character a target name refuses becomes -, runs of - become one, and
leading and trailing - and . are cut; a name of 253 bytes at most is
kept, and a name with nothing left is host. When two inventory names give
one target name, a name that is already valid keeps it, else the first in
sorted order; each other gets - and the first four hexadecimal digits of the
SHA-256 of its own inventory name (six, eight, … if that name is taken
too). A renamed host has the label ansible.lemano.host with its inventory
name when that is a label value, and a comment # inventory name: ... when
it is not; the group name host is therefore not a label. host is
ansible_host, else the inventory name as written.
Key file paths
~ and ~/... are the home directory of the user who runs the import,
~user/... is the home directory of user in the system’s user database, a
relative path is read against the current directory (a comment notes it). The
file is not opened.
Hosts not imported
Two kinds, both in the report’s skipped list with the host’s inventory name,
the target name it would have, a reason and a kind:
notSSH: the connection is not SSH. Decided from the printed value when it is plain, so that a connection plugin missing here does not matter.failed: Ansible could not evaluate the host’s variables;ansible_portis not a number from 1 to 65535; a variable is not text or a number or has a control character; the key path starts with~and the home directory is unknown, or with~userand the user does not exist here; the ssh arguments have an unterminated quote or a jump host thatproxyJumpdoes not accept; the loader refuses the target (its host or user, for one); and, with--from-json, a variable the target is made of is a template or a vault value.
A ProxyCommand that contains sshpass, password, passwd, passphrase,
secret or token, or that passes a credential as an argument (-u user:pass, an authorization header, a variable such as PROXY_KEY=), does
not skip the host: it is imported without the command, with a comment and a
warning.
A host that is skipped and has a document in the existing file keeps that
document, with a warning, and is listed as retained; --prune does not drop
it.
The file
The first line is the marker:
# Generated by "lemano ansible import". The next import rewrites this file; change the inventory instead.- A file at
--outthat exists and does not start with that line is refused and left alone;--outas a directory is refused. - Hosts in the file that the inventory no longer has are reported as
removedand kept; with--prunethey are dropped. - A host is “updated” when its document differs from the one in the file, and “unchanged” when it is the same text.
- When nothing is added, updated or pruned, the file is not written, so its header date and modification time stay.
- A document in an existing generated file that does not read as one target is an error: fix the file, or delete it.
- The file is written to a temporary file in the same directory and renamed, with mode 0600 (an existing file keeps its mode); its directory is created if it is missing.
- Before writing, the YAML files under the directory of
--outare loaded asdiscoverloads them, except--outitself. A target name that one of them declares and the import would write is an error that lists each name and the file and line, and nothing is written. A file that does not load is listed in the report as not checked. A preview checks nothing. - More than 1000 hosts, or a file over 1 MiB, is an error: import in parts
with
--limitand--out.
Errors
| Situation | Message and exit code |
|---|---|
ansible-inventory or ansible not on PATH | says how to use --from-json; 1 |
ansible-inventory exits with a status | the status and the command to run by hand, not its own output; 1 |
ansible prints no result for any host | the status; 1 |
| either command runs past its limit | stopped; 1 |
| the JSON is not an inventory | what is wrong with its shape, or the byte of a syntax error, never its text; 1 |
| no host in the inventory | an error, since Ansible prints an empty inventory for a path it cannot read; 1 |
| no host can be imported | an error that lists why each was left out (the first ten); nothing written; 1 |
| name conflict, file not generated, too many hosts | an error, nothing written; 1 |
--from-json with -i or --limit, --prune without --out, --out without a value, an unknown -o | usage; 2 |
| interrupted | nothing written; 130 |
at least one host could not be imported (skipped with kind failed) | the report is printed and, with --out, the other targets are written first; 7 (partial) |
A host that is not reached over SSH (kind notSSH) and a warning do not change
the exit status: an import that left out nothing else exits 0. When no host at
all can be imported the import is an error (1) and nothing is written.
JSON report
-o json prints: out, source, written, content (a preview’s file),
hosts (in the inventory), added, updated, unchanged, removed (names),
pruned, retained, renamed (inventory, name, reason, label),
skipped (host, name, reason, kind), warnings (host, variable,
message), badGroups (group, reason, hosts) and unchecked. Lists
are empty arrays, never null. It holds no value taken from the inventory other
than names, and a preview’s content holds the targets, which are the point
of a preview.