Skip to content

AnsibleΒΆ

InstallationΒΆ

UbuntuΒΆ

Install ansible on ubuntu servers

sudo apt update --yes
sudo apt install --yes software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install --yes ansible

DebianΒΆ

Signing key

UBUNTU_CODENAME=jammy
wget -O- "https://keyserver.ubuntu.com/pks/lookup?fingerprint=on&op=get&search=0x6125E2A8C77F2818FB7BD15B93C4A3FD7BB9C367" | sudo gpg --dearmour -o /usr/share/keyrings/ansible-archive-keyring.gpg

Add an entry to apt’s sources pointing to the PPA

echo "deb [signed-by=/usr/share/keyrings/ansible-archive-keyring.gpg] http://ppa.launchpad.net/ansible/ansible/ubuntu $UBUNTU_CODENAME main" | sudo tee /etc/apt/sources.list.d/ansible.list

Update and install from apt repo

sudo apt update && sudo apt install ansible

Arch LinuxΒΆ

To install the full ansible package run

sudo pacman -S ansible

To install the minimal ansible-core package run

sudo pacman -S ansible-core

Test for Correct InstallationΒΆ

PingPong with ansible

ansible localhost -m ping -u root

Show file systems

ansible localhost -a "df -h" -u root

Putting arguments into your command

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

Install vim with ansible

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

We can specify multiple hosts by separating them with colons

ansible server1:server2 -m ping -u root

Best Practice Ansible Project StructreΒΆ

.
β”œβ”€β”€ inventory
β”‚   └── hosts
β”œβ”€β”€ prjct-name.yml
└── roles
    └── prjct-name
        β”œβ”€β”€ defaults
        β”‚   └── main.yml
        β”œβ”€β”€ files
        β”œβ”€β”€ handlers
        β”‚   └── main.yml
        β”œβ”€β”€ meta
        β”‚   └── main.yml
        β”œβ”€β”€ tasks
        β”‚   └── main.yml
        β”œβ”€β”€ templates
        └── vars
            └── main.yml
  • inventories: Contains information about your managed hosts and groups.
  • hosts: Defines host names, IP addresses, and group memberships.
  • group_vars: Holds variables shared by groups of hosts.

  • prjct-name.yml: Your main playbook, orchestrating the execution of roles.
  • defaults: Contains default variables for the role.
  • files: Stores static files to be copied to remote hosts.
  • handlers: Defines actions to be taken in response to certain changes.
  • meta: Contains metadata about the role, including dependencies.
  • tasks: Contains the main tasks to be executed by the role.
  • templates: Stores Jinja2 templates for dynamic file generation.
  • vars: Contains additional variables specific to the role.