Skip to content

How do I do Hello World in Ansible?

Step one Install Pip

Step two, install ansible via pip

python3 -m pip install --user ansible

Step three, test ansible command

ansible -m ping localhost

Step four, add nodes to ansible inventory and test inventory

All the hosts are stored in a configuration file that. So when you want to configure servers you select them from this file and run command form them.

sudo mkdir /etc/ansible
cd /etc/ansible
touch hosts
sudo vim hosts

Below is an example ansible hosts file, please update the IP address or hostnames to a server you can SSH into.

ungrouped:
  hosts:
    192.168.7.219:
webservers:
  hosts:
    server1.homelab.local:
    server2.homelab.local:

To test if ansible is aware of the hosts run the following command

ansible-inventory --list -y

To test if you can connect to the servers run the following command, changing root for the user you ssh into, make sure you have ssh keys configured, you can learn more about configuring ssh keys at ssh-copy-id.

ansible all -m ping -u root 

To actually run a command on all the servers and get the result returned you can run something like the following, remember to replace root again

# example 1
ansible servers -a "uptime" -u root

# example 2
ansible all -a "df -h" -u root

You can run ansible modules, like so

ansible all -m apt -a "name=vim state=latest" -u root

Selecting Servers with Ansible

Troubleshooting

Sources