Skip to content
lemano ansible import

lemano ansible import

Turn the hosts of an Ansible inventory into targets

Description

Read an Ansible inventory and turn each host into a target. Without --out the
targets are printed, as a preview, and nothing is written; with --out FILE
they are written to that one file.

The inventory is read by Ansible itself, which must be installed:
ansible-inventory --list gives the hosts and groups, and ansible evaluates
the connection variables of each host, templates rendered and vault values
decrypted with your own settings (ansible.cfg, ANSIBLE_* variables, vault
password files), without connecting to any host. So lemano has no inventory
parser of its own, and no host is skipped for a template or a vault value.
-i, --limit, --vault-id and --vault-password-file are passed to Ansible as
they are (without -i, Ansible's own default applies; without a vault option,
ANSIBLE_VAULT_PASSWORD_FILE and ansible.cfg). Evaluating runs the lookups and filters your inventory
contains, on this machine, as any use of it does. Settings Ansible applies
to every host from its configuration (remote user, private key file, port,
timeout, ssh_common_args and ssh_extra_args) are used when a host does not
set them; a variable of the inventory wins. Without Ansible on this machine,
the best way is to run the import where it is installed and copy the target
file it generates, which holds the final values and no password. A saved
output can be read here, but templates and vault values in it cannot be
resolved, and the hosts that have them are listed as not imported:

  ansible-inventory --list > inventory.json
  lemano ansible import --from-json inventory.json

What each host maps to:
  name           the inventory name, made a valid target name when it is not
                 one: lowercase, other characters as '-'; a host renamed
                 this way keeps its inventory name in the label
                 ansible.lemano.host, and two names that give the same
                 target name get a short hash of their own name
  host           ansible_host, else the inventory name
  port, user     ansible_port, ansible_user
  credential     ansible_ssh_private_key_file, as a file: reference
  timeouts       ansible_ssh_timeout or Ansible's timeout setting, as the
                 connect limit when it is not 10 seconds, the default; the
                 limit on an open connection is not imported
  timeouts       ansible_ssh_timeout or Ansible's timeout setting, as the
                 connect limit when it is not 10 seconds, the default; the
                 limit on an open connection is not imported
  proxyJump      -J or -o ProxyJump in ansible_ssh_common_args or
                 ansible_ssh_extra_args
  proxyCommand   -o ProxyCommand in the same arguments; it runs on this
                 machine when the target is reached
  privilege      user when ansible_become is false or ansible_become_method
                 is not sudo
  discovery      lemano_user_files (users, preset, paths) as
                 spec.discovery.userFiles
  labels        ansible.lemano.GROUP=true for each group the host is in,
                 directly or through a child group (not all or ungrouped),
                 so that lemano discover -l ansible.lemano.web=true selects
                 by group

Passwords are never stored and never read by the import. When the inventory
is read by Ansible itself, a host that sets ansible_password (or
ansible_ssh_pass, ansible_ssh_password) gets the target field password, and
one that sets ansible_become_password (or ansible_become_pass,
ansible_sudo_pass) gets sudoPassword, each as the reference
ansible://HOST?config=FILE&inventory=SOURCE: it names the host, the Ansible
configuration file in effect and the inventory (paths relative to the target
file), and Ansible is asked for the value each time a login or sudo needs
it, with that configuration and your vault settings, from any folder. The
report says so, in a line of its own. Other password variables
(ansible_sudo_password, ansible_su_pass, a key passphrase), a host name
Ansible would read as a pattern and a saved output (--from-json), which
cannot be asked again, are named in a warning instead, with the host, and
their values are not asked for. A ProxyCommand that looks like it holds a
password is not imported, with a warning. A host that is not reached over
SSH is left out; a host whose values Ansible cannot evaluate or a target
cannot hold is listed with the reason, and is kept as it is in the file
when an earlier import wrote it. SSH options other than the two proxy
options are listed as not imported; the host key options (such as
StrictHostKeyChecking) are ignored, as lemano pins and checks every host
key itself.

The file starts with a marker line, and the next import rewrites it: new
hosts are added, changed ones updated, and hosts that are gone from the
inventory are reported and stay in the file unless --prune drops them. A
file without the marker is never touched. A target name that another target
file under the same directory declares stops the import before anything is
written. Nothing is written when nothing would change.

Exit code 7 (partial) when at least one host could not be imported, after
everything was printed and, with --out, written. Hosts not reached over SSH
are not a failure and do not change the exit code.

Synopsis

lemano ansible import [-i INVENTORY]... [--limit PATTERN] [--out FILE] [--prune]

Examples

lemano ansible import -i inventories/prod/hosts.yml
lemano ansible import -i inventories/prod/hosts.yml --out targets/prod.yaml
lemano ansible import -i hosts.ini --limit 'web:&eu' --out targets/web-eu.yaml
lemano ansible import --from-json inventory.json --out targets/all.yaml --prune

Flags

FlagTypeDefaultDescription
--from-jsonstringread the output of ansible-inventory –list saved in this file; nothing is run, and templates and vault values stay unresolved
-i, --inventorystringArrayan inventory file, directory or host list, as ansible-inventory -i takes it (repeatable)
--limitstringonly the hosts matching this Ansible pattern, as ansible-inventory –limit takes it
--outstringwrite the targets to this file; an import rewrites it and never touches a file it did not generate (without it, print them)
-o, --outputstringoutput format: json or yaml (default: a table for people)
--prunedrop from the file the hosts that are gone from the inventory
--vault-idstringArraythe vault identity to use, as ansible-playbook –vault-id takes it (repeatable)
--vault-password-filestringArraya file (or script) that gives the vault password, as ansible-playbook takes it (repeatable)

Exit codes

See exit codes.

See also