Ansible Installation: From Beginner to Abandonment (Part 2)

Ansible Installation

There are three installation methods for Ansible: Source Installation, Distribution Installation, and Python Installation.

When using either Distribution Installation or Python Installation, there are two installation packages for Ansible, which are as follows:

  • <span>ansible-core</span>: A minimal language and runtime package that includes a set of built-in modules and plugins.
  • <span>ansible</span>: A larger “batteries-included” package that adds community-selected Ansible collections for automating various devices.

When installing Ansible using Source or Python, the <span>sshpass</span> package is not installed by default. This package is used to provide password authentication for the controlled end, so if you need to enter the SSH password when executing Ansible commands, this package is required. It can be installed using <span>dnf install -y sshpass</span>.

[root@ansible ansible]# ansible servera -m ping
servera | FAILED! => {
    "msg": "to use the 'ssh' connection type with passwords or pkcs11_provider, you must install the sshpass program"
}

This installation uses the Rocky 8 Linux system.

Source Installation

[root@ansible ~]# dnf install python3.12 python3.12-pip sshpass
[root@ansible ~]# tar xf ansible-2.16.3.tar.gz
[root@ansible ~]# cd ansible-2.16.3/
[root@ansible ansible-2.16.3]# python3 -m pip install -r ./requirements.txt
[root@ansible ansible-2.16.3]# python3 setup.py install

Source installation can only install <span>Ansible-core</span>.

Distribution Installation

[root@ansible ~]# dnf install -y epel-release

# Install the minimal Ansible
[root@ansible ~]# dnf install ansible-core

# Install Ansible with common modules
[root@ansible ~]# dnf install ansible

Python Installation

# Install Python3 and pip
[root@ansible ~]# dnf install python3.12 python3.12-pip sshpass

# Install Ansible-core
[root@ansible ~]# python3.12 -m pip install ansible-core==2.16.3

# Install Ansible
[root@ansible ~]# python3.12 -m pip install ansible

Setting Up Ansible Parameter Autocompletion

[root@ansible ~]# python3 -m pip install argcomplete
[root@ansible ~]# activate-global-python-argcomplete --user

Re-login to the command line to load the environment variables, and you will see the autocompletion feature.

Quick Configuration and Use of Ansible

There is a controlled node with the address <span>192.168.221.131</span> and the hostname <span>servera</span>.

[root@ansible ~]# mkdir ansible
[root@ansible ~]# cd ansible
[root@ansible ansible]# sed -i 's/;inventory=.*/inventory\ =\ .\/inventory/' ansible.cfg
[root@ansible ansible]# cat <<-EOF > ansible.cfg
[defaults]
inventory      = ./inventory
ask_pass       = false
remote_user    =  root
log_path       = /var/log/ansible.log
host_key_checking = False
[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False
[ssh_connection]
ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no
EOF
[root@ansible ansible]# echo servera > inventory
[root@ansible ansible]# tail -n1 /etc/hosts
192.168.221.131 servera

# Test network connectivity using the ping module
[root@ansible ansible]# ansible all -k -m ping
SSH password:
servera | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

Leave a Comment