Your invetory file will hold your host and variable information. Lets say we have 3 nodes that we want to deploy software to; node-1, node-2 and node-3. We will group them under nodes. This will be saved under the a new file inventory.init:
inventory.ini
1234
[nodes]node-1
node-2
node-3
Next we will populate information about our node names, this will be done under our ~/.ssh/config configuration:
~/.ssh/config
1234567891011121314151617181920
Host node-1
Hostname 10.0.0.2
User root
IdentityFile ~/.ssh/id_rsa
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
Host node-2
Hostname 10.0.0.3
User root
IdentityFile ~/.ssh/id_rsa
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
Host node-3
Hostname 10.0.0.4
User root
IdentityFile ~/.ssh/id_rsa
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
Now we need to generate a ssh key for our node where we will run our ansible commands from:
Now we will copy the contents of ~/.ssh/id_rsa.pub into our destination nodes ~/.ssh/authorized_keys or if you have password authentication enabled, we can do $ ssh-copy-id root@10.0.0.x etc. Now we should be able to ssh to our nodes to node-1, node-2 and node-3.
Deploy Python:
As Ansible requires Python, we need to bootstrap our nodes with Python. Since we are able to ssh to our nodes, we will use ansible to deploy Python to our nodes:
1
$ ansible -m raw -s -a "apt update && apt install python -y" -i inventory.ini nodes
This should succeed, then we can test our connection by running the ping module:
Let’s run shell commands, the traditional hello world, using the ansible-playbook command. First we need a task definition, which I will name shell_command-1.yml: