Commands / Services and tunnels

Services and tunnels

ansible

Also ansible-inventory, ad hoc, inventory, configuration management, automation

Runs one module on every host of an inventory group over SSH, and shows what the inventory holds.

Ansible is agentless: the control node logs in over SSH and runs a small program on each managed host. The inventory (hosts.yml) names the hosts and groups them; group_vars and host_vars hold their data. ansible GROUP -m MODULE runs one module ad hoc (ping checks login and Python, not ICMP; -a with the default command module runs a command). ansible-inventory --graph shows the groups and --host NAME the variables one host ends up with.

ansible command usage

ansible PATTERN -m MODULE [-a ARGS]
ansible PATTERN -a 'COMMAND'
ansible-inventory --graph | --host NAME

Options

-m ping
check each host can be logged in to and runs Python (answers pong)
-a 'command'
the arguments of the module; with no -m, the command to run
-i FILE
the inventory to use (default from ansible.cfg)
--graph
ansible-inventory: the groups and their hosts as a tree
--host NAME
ansible-inventory: the variables that host gets, merged

ansible examples

ansible routers -m ping
Logs in to every router of the group and answers pong for each that works.
ansible routers -a "vtysh -c 'show ip route static'"
Runs the same show command on every router and prints each answer.
ansible-inventory --graph
Prints the inventory's groups and the hosts in them.
ansible all -i inventory.ini -m ping
Checks every host in that inventory file, instead of the configured one.
ansible all --list-hosts
Lists the hosts a pattern selects, without connecting to any.
ansible 'routers:!coreRouter' -m ping
Selects every router except coreRouter; : joins groups and ! leaves hosts out.
ansible routers -l edgeRouter -a 'ip route'
Limits the run to one host of the group.
ansible routers -m shell -a 'ip route | grep default'
Runs through a shell, for pipes and redirects that the default command module does not understand.
ansible routers -m setup -a 'filter=ansible_default_ipv4'
Prints the facts Ansible gathers about each host's default route and address.
ansible web -b -m apt -a 'name=nginx state=present'
Installs nginx where it is missing, as root (-b, become); a second run changes nothing.
ansible web -b -m service -a 'name=nginx state=restarted'
Restarts the nginx service on every web server.
ansible all -b -m copy -a 'src=resolv.conf dest=/etc/resolv.conf'
Copies a local file to every host, only where it differs.
ansible all -u admin -k -m ping
Logs in as admin and asks for the SSH password, for hosts without your key yet.
ansible-inventory --host edgeRouter
Prints the variables one host gets from the inventory and its groups.

Pitfalls

  • -m ping is not ICMP: a router that drops pings can still answer pong, and one that answers pings can fail it.
  • Ad hoc commands are not idempotent and leave no record: changes belong in a playbook.

Run it on real machines

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Start with a free account: Google or email, no card.

Open this lab