Ansible: Basic Setup

This basic setup is for another linux machine on-premise

Update and install ansible.

sudo apt update
sudo apt install ansible -y

First, create our inventory.

mkdir ansible
cd ansible
sudo nano inventory.yml

Write this to inventory.yml

[vps]
vps_ip_address ansible_user=user ansible_port=port_number

Test this inventory config.

ansible -i inventory.yml vps -m ping

The output should be like this:

IP address VPS | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

You are done for inventory. Next, you should create playbook.

Playbook have many things to do, for this example this playbook is for update and upgrade apt(s).

sudo nano playbook.yml

Write this to playbook.yml

---
- name: Update and Upgrade APT packages
  hosts: all
  become: true
  tasks:
    - name: Update APT package cache
      apt:
        update_cache: yes

    - name: Upgrade all APT packages
      apt:
        upgrade: dist
        autoremove: yes
        autoclean: yes
  • hosts: all - Specifies that the playbook will be run on all hosts listed in the inventory. You can replace all with a specific group, such as vps.

  • become: true - Instructs Ansible to use superuser (root) privileges when running the task.

  • Tasks:

    Update APT package cache: Runs apt-get update to update the package index.

    Upgrade all APT packages: Runs apt-get dist-upgrade to safely upgrade all packages, remove unnecessary ones, and flush the cache.


How to run ansible playbook.

ansible-playbook -i inventory.yml playbook.yml

Last updated