Part 2: Managing Hosts with Ansible

first steps managing a host with Ansible.

Sebastiaan avatar
  • Sebastiaan
  • 4 min read

What do we need?

There are a few things we need to start with managing hosts with ansible.

  • SSH keys
  • Place to store our files
  • Inventory file

Creating SSH key

To manage hosts, we need to use a SSH key. We can create them with the following command: ssh-keygen -t ed25519. Choose where the SSH files needs to be placed and if you want to use a password or not (much more secure if you do).

I’ve setup a proxmox test host, were we can test our commands, playbooks and roles on. To use manage this host with ansible, we need to copy our SSH pub key to the test proxmox host. we do this with the following command, it will ask for the password of the hosts;

ssh-copy-id root@<192.168.200.155>

Place to store our files

Lets create a Ansible folder on our desktop, for me is this /Users/sebas/Desktop.
We can create the Ansible folder through CLI or GUI, it doesn’t matter which way you use.
I’ve used CLI and for that is the command mkdir /Users/sebas/Desktop/ansible.\

Creating an Inventory file

Creating the file can be through GUI or CLI, I prefer CLI so the command for it is touch /Users/sebas/Desktop/ansible/inventory.
We can call this file anything we want but for easy reference we use inventory.
In the inventory file we place the following code block;

It creates a group called proxmox and added an ip address from a host. We are not bound to use ip addresses in the file we also can use FQDN names for it.

[proxmox]
192.168.200.155

Time to use ADHOC commands

When we use the following command:

ansible proxmox -i inventory -m ping -u root

We specify which group we want to call in the inventory file in this case proxmox or all to check all host(s) in thefile.
Specify with -i the inventory file which has the host(s) information we want to connect to.
We use a module (-m) called ping, which checks the connection to the host(s) we’ve added in the inventory file.
With -u we specify the user we want to use to connect to the host, only necessary if your local user is different from the remote user.

the output of the command;

192.168.200.155 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3.11"
    },
    "changed": false,
    "ping": "pong"
}

We can use other ansible modules in our adhoc commands like ansible.builtin.shell any you can find on the docs. In the command below we use -a to specifiy a command we want to run, without calling a specific module we standard use the command module in the adhoc command. If you add –ask-become-pass or -K, Ansible prompts you for the password to use for privilege escalation like sudo.

ansible proxmox -i inventory -a 'apt update' -K -u root

The output of the output;

sebas@eyeofmordor ansible % ansible proxmox -i inventory -a 'apt update' -K -u root
BECOME password: 
[WARNING]: Platform linux on host 192.168.200.155 is using the discovered
Python interpreter at /usr/bin/python3.11, but future installation of another
Python interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-
core/2.18/reference_appendices/interpreter_discovery.html for more information.
192.168.200.155 | CHANGED | rc=0 >>
Hit:1 http://ftp.nl.debian.org/debian bookworm InRelease
Hit:2 http://ftp.nl.debian.org/debian bookworm-updates InRelease
Hit:3 http://security.debian.org bookworm-security InRelease
Get:4 http://download.proxmox.com/debian/pve bookworm InRelease [2,768 B]
Get:5 http://download.proxmox.com/debian/ceph-quincy bookworm InRelease [3,470 B]
Get:6 http://download.proxmox.com/debian/pve bookworm/pve-no-subscription amd64 Packages [381 kB]
Get:7 http://download.proxmox.com/debian/ceph-quincy bookworm/no-subscription amd64 Packages [41.5 kB]
Fetched 429 kB in 1s (651 kB/s)
Reading package lists...
Building dependency tree...
Reading state information...
14 packages can be upgraded. Run 'apt list --upgradable' to see them.
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Conclusion

Managing hosts with Ansible is a way to automate and streamline infrastructure management. By leveraging adhoc commands, you can quickly connect and manage hosts.

In this series we will delve deeper into Ansible, where we explore more techniques such as playbooks and roles. Stay tuned for the next post in this series!

Sebastiaan

Written by : Sebastiaan

Sysadmin/Platform/Devops Engineer

Recommended for You

Part 1: Ansible? what is that!

Part 1: Ansible? what is that!

Automation and configuration with Ansible.