Ansible – How to Install Ansible (Part-3)

Posted by

Ansible Installation and Configuration Guide

How to install ansible in Ubuntu?

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

How to install ansible in Centos 7 / RHEL 7?


# Upgrade Python to 3.11
$ yup update
$ yum install openssl-devel bzip2-devel libffi-devel
$ yum groupinstall "Development Tools"
$ wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0a4.tgz
$ tar -xzf Python-3.11.0a4.tgz
$ cd Python-3.11.0a4
$ ./configure --enable-optimizations
$ make altinstall
$ python3.11 -V

$ sudo ln -fs /usr/local/bin/python3.11 /usr/bin/python
$ sudo ln -fs /usr/local/bin/python3.11 /usr/bin/python3

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
$ python get-pip.py
$ pip install ansible

How to install ansible in Centos 8 / RHEL 8?


# On RHEL and CentOS:

$ sudo yum update
$ sudo yum install ansible

# To enable the Ansible Engine repository for RHEL 8, run the following command:
$ sudo subscription-manager repos --enable ansible-2.9-for-rhel-8-x86_64-rpms

# To enable the Ansible Engine repository for RHEL 7, run the following command:
$ sudo subscription-manager repos --enable rhel-7-server-ansible-2.9-rpms

If in ubuntu below steps failing , then please follow below steps

 sudo add-apt-repository --yes --update ppa:ansible/ansible

Disable IPv6 Temporarily:

sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1

Enable IPv6 After Installation:

sudo sysctl -w net.ipv6.conf.all.disable_ipv6=0
sudo sysctl -w net.ipv6.conf.default.disable_ipv6=0

After installation

check version:

Ansible Environment Summary

ansible [core 2.16.7]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/usr/bin/python3)
  jinja version = 3.0.3
  libyaml = True

  • Ansible Version: 2.16.7
  • Config File Location: /etc/ansible/ansible.cfg
  • Module Search Path:
    • /root/.ansible/plugins/modules
    • /usr/share/ansible/plugins/modules
  • Python Module Location: /usr/lib/python3/dist-packages/ansible
  • Ansible Collection Location:
    • /root/.ansible/collections
    • /usr/share/ansible/collections
  • Executable Location: /usr/bin/ansible
  • Python Version: 3.10.12
  • Jinja Version: 3.0.3
  • LibYAML: Enabled

To find executable file

root@Jami2:/home/jami# which ansible
/usr/bin/ansible

/usr/bin/ansible            /usr/bin/ansible-config      /usr/bin/ansible-console  /usr/bin/ansible-galaxy     /usr/bin/ansible-playbook  /usr/bin/ansible-test
/usr/bin/ansible-community  /usr/bin/ansible-connection  /usr/bin/ansible-doc      /usr/bin/ansible-inventory  /usr/bin/ansible-pull      /usr/bin/ansible-vault

Components of Ansible after Installation

  1. Executables
  2. Modules
  3. Plugins
  4. Config file

. Executables

  • These are command-line tools that come with Ansible.

Examples:

  • ansible: Run a single task on a group of hosts.
$ ansible all -m ping
  • This command pings all hosts in the inventory to check connectivity.
  • ansible-playbook: Run playbooks which are YAML files containing multiple tasks.
$ ansible-playbook site.yml
  • This command runs the site.yml playbook.
  • ansible-galaxy: Manage Ansible roles.
$ ansible-galaxy install geerlingguy.apache
  • This command installs the Apache role from Ansible Galaxy.
  • ansible-inventory: Manage and display the inventory.
$ ansible-inventory --list
  • This command lists all hosts and groups in the inventory.

2. Modules

  • Modules are units of work executed on the remote hosts. They are written in Python but called within playbooks.

Examples:

  • Installing a package:
- name: Install nginx
  hosts: webservers
  tasks:
    - name: Install nginx package
      ansible.builtin.yum:
        name: nginx
        state: present

Copying a file:

- name: Copy a file to the remote host
  hosts: webservers
  tasks:
    - name: Copy file
      ansible.builtin.copy:
        src: /local/path/to/file
        dest: /remote/path/to/file

3. Plugins

  • Plugins extend the core functionality of Ansible.

Examples:

  • Callback Plugin: To change the output format.
[defaults]
callback_whitelist = json

Connection Plugin: For managing SSH connections.

- name: Ensure SSH connection is used
  hosts: webservers
  tasks:
    - name: Gather facts
      ansible.builtin.setup:
        filter: ansible_connection

4. Config file

  • The ansible.cfg file configures Ansible’s behavior.

Example Configuration File (ansible.cfg):

[defaults]
inventory = ./inventory
remote_user = ansible
host_key_checking = False
retry_files_enabled = False

[privilege_escalation]
become = True
become_method = sudo
become_user = root
become_ask_pass = False

Additional Concepts

1. Playbook

  • Playbooks are YAML files that define a series of tasks to be executed on remote hosts.

Example Playbook (site.yml):

- name: Configure web servers
  hosts: webservers
  tasks:
    - name: Install NGINX
      ansible.builtin.yum:
        name: nginx
        state: present

    - name: Start NGINX service
      ansible.builtin.service:
        name: nginx
        state: started

2. Inventory

  • The inventory is a file that lists the hosts and groups of hosts Ansible manages.

Example Inventory File (inventory):

[webservers]
webserver1.example.com
webserver2.example.com

[databases]
dbserver1.example.com
dbserver2.example.com

3. Ad-hoc Commands vs. Playbooks

  • Ad-hoc Commands: Used for quick, one-off tasks.
$ ansible all -m copy -a "src=/local/file dest=/remote/file"
  • This command copies a file to all hosts in the inventory.
  • Playbooks: Used for more complex, repeatable tasks that involve multiple steps.

Example Playbook:

- name: Configure multiple settings
  hosts: all
  tasks:
    - name: Copy file to remote host
      ansible.builtin.copy:
        src: /local/file
        dest: /remote/file

    - name: Install a package
      ansible.builtin.package:
        name: tree
        state: present
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x