Skip to content

Ansible installation

Ansible version need above 2.12+

1. User creation

  • 1.1 User config (Rocky)

Login with root, and check whether the ansible user exists.

id ansible

image-20250501183756254

If user does not exist, please create it

useradd -u 1000 -c "Ansible maintain only" ansible
password ansible

Grant sudo permission to ansible user

echo 'ansible   ALL=(ALL)      NOPASSWD:ALL' >> /etc/sudoers
  • 1.2 User config (Ubuntu)

Login with root, and check whether the ansible user exists.

id ansible
useradd -m -u 1000 -U -d /home/ansible -s /bin/bash ansible
passwd ansible

Grant sudo permission to ansible user

echo 'ansible   ALL=(ALL)      NOPASSWD:ALL' >> /etc/sudoers

2. Ansible install

  • 2.1 Install ansible (Rocky)

Config proxy to install ansible (if need)

export http_proxy="http://10.xx.xx.xx:xx"
export https_proxy="http://10.xx.xx.xx:xx"

Install ansible

dnf clean all
dnf makecache
dnf install -y epel-release
dnf install -y ansible

Install ansible extension plug-in

su - ansible
export http_proxy="http://10.xx.xx.xx:xx"
export https_proxy="http://10.xx.xx.xx:xx"
ansible-galaxy collection install ansible.posix
ansible-galaxy collection install community.general
  • 2.2 Install ansible (Ubuntu)

Config proxy to install ansible (if need)

export http_proxy="http://10.xx.xx.xx:xx"
export https_proxy="http://10.xx.xx.xx:xx"

Install ansible

apt update
apt install -y pip
python3 -m pip install ansible
apt install sshpass

Install ansible extension plug-in

su - ansible
export http_proxy="http://10.xx.xx.xx:xx"
export https_proxy="http://10.xx.xx.xx:xx"
ansible-galaxy collection install ansible.posix
ansible-galaxy collection install community.general

3. Ansible config

  • 2.1 Create config file
cd ~
vim ansible.cfg

Add below parameters to ansible.cfg file (check/change remote user to your remote maintain user)

[defaults]
inventory = ./hosts
roles_path = ./cog-ansible/roles
remote_user = ansible
host_key_checking = False
forks = 20

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
  • 2.2 Create inventory file
vim hosts

Add the agent host info into hosts file

[group1]
apserver    ansible_ssh_host=10.41.xx.xx

[group2]
dbserver    ansible_ssh_host=10.41.xx.xx

[group:children]
group1
group2

[all:vars]
ansible_ssh_pass="remote_user_password"
ansible_sudo_pass="remote_user_password"
  • 2.3 Check ansible version
ansible --version

image-20250310174338004

  • 2.4 Prepare ssh key

Create public and private key in remote user home folder

cd ~ && ssh-keygen -t rsa
ll .ssh/

image-20250501191259259