ansible Getting started with ansible Provisioning remote machines with Ansible


Example

We can provision remote systems with Ansible. You should have an SSH key-pair and you should take your SSH public key to the machine ~/.ssh/authorized_keys file. The porpuse is you can login without any authorization.

Prerequisites:

  • Ansible

You need an Inventory file (for ex.: development.ini) where you determine the host what you want to use:

[MACHINE_NAME]
MACHINE_NAME hostname=MACHINE_NAME ansible_ssh_host=IP_ADDRESS ansible_port=SSH_PORT ansible_connection=ssh ansible_user=USER ansible_ssh_extra_args="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
  • hostname - the hostname of the remote machine
  • ansible_ssh_host - the ip or domain of the remote host
  • ansible_port - the port of the remote host which is usually 22
  • ansible_connection - the connection where we set, we want to connect with ssh
  • ansible_user - the ssh user
  • ansible_ssh_extra_args - extra argumentums what you want to specify for the ssh connection

Required extra args for ssh:

  • StrictHostKeyChecking - It can ask a key checking what waiting for a yes or no. The Ansible can't answer this question then throw an error, the host not available.
  • UserKnownHostsFile - Needed for StrictHostKeyChecking option.

If you have this inventory file you can write a test playbook.yml:

---
- hosts: MACHINE_NAME
  tasks:
    - name: Say hello
      debug:
        msg: 'Hello, World'

then you can start the provision:

ansible-playbook -i development.ini playbook.yml