Ansible - 3 - 2 Demo ansible inventory
It is time to put the inventory to work. We start by manually SSH-ing from the controller to ansible-target-1, accepting the host key fingerprint, then logging out — that initial handshake stores the target's key in the controller's known_hosts file, which Ansible will reuse later. From there we create a small project folder and a custom inventory file inside it.
# inventory
target1 ansible_host=192.168.1.10 ansible_ssh_pass=osboxes.org
ansible -i inventory target1 -m ping
The ansible command runs an ad-hoc task on the named host. The -m ping module verifies that the controller can connect, log in and execute Python on the target. A successful run returns a green SUCCESS message — proof that controller and target talk to each other.
The classic SSH host key trap
- Add a second host
target2in the inventory without performing the manual SSH handshake first. - Running the same ping command fails because the target's host key is not in
known_hostsyet. - Two fixes exist: SSH into the new target once to accept the fingerprint, or disable host key checking in
/etc/ansible/ansible.cfgby uncommentinghost_key_checking = False.
Disabling host key checking is convenient in a lab but unsafe in production. The recommended approach is to deploy SSH public keys to every target and rely on key-based authentication, which removes the need for plain-text passwords in the inventory at the same time. For now we know how to declare hosts, run an ad-hoc command and recognise the most common SSH error — a strong foundation before diving into playbooks.