Installing and Configuring Ansible on CentOS 7

Installing and Configuring Ansible on CentOS 7

Ansible Is a configuration management system written in Python using a declarative markup language to describe configurations. Used to automate software configuration and deployment. Usually used to manage Linux nodes, but Windows is also supported. Supports work with network devices on which Python 2.4 and higher is installed via SSH or WinRM connection.

Installing Ansible

Add EPEL repository to the system and install the program

[[email protected]]# yum install epel-release
[[email protected]]# yum install ansible

The settings for remote hosts are stored in the file / etc / ansible / hosts. Let’s make a backup copy of this file.

[[email protected]]# mv /etc/ansible/hosts /etc/ansible/hosts.default

Connection setup

On the server with Ansible, we will generate an RSA key

[[email protected]]# ssh-keygen -t rsa

On the remote server (which we will administer using Ansible), enable SSH key authorization by editing the sshd_config configuration file and restarting the service

[[email protected]]# nano /etc/ssh/sshd_config
PubkeyAuthentication yes 
AuthorizedKeysFile .ssh/authorized_keys

[[email protected]]# service sshd restart

On the server with Ansible, copy to the remote server using the ssh-copy-id utility

[[email protected]]# ssh-copy-id -i ~/.ssh/ [email protected]

We test the connection, while the password should not be requested

[[email protected]]# ssh

Let’s add our test server to the hosts file for further testing Ansible capabilities

[[email protected]]# nano /etc/ansible/hosts
centos ansible_ssh_host=

Checking the work of Ansible

[[email protected]]# ansible test -m ping
centos | SUCCESS => {
    "changed": false, 
    "ping": "pong"

where test is the PC group (see file / etc / ansible / hosts)

If you don’t want to enable SSH authorization by file, you can install the sshpass utility, and look at Ansible’s answer

[[email protected]]# yum install sshpass
[[email protected]]# ansible test -m ping -k -u root
SSH password:
centos | SUCCESS => {
    "changed": false, 
    "ping": "pong"

Some commands

ansible -m ping all    - пинг
ansible test -m shell -a 'cat /etc/redhat-release'     - Версия Centos
ansible test -m shell -a 'uname -a'     - Выполнить команду uname -a
ansible test  -m setup - узнать информацию о сервере
ansible -i hosts -m setup -a 'filter=ansible_memtotal_mb' all      - сколько памяти доступно на всех хостах
ansible -i hosts -m setup -a 'filter=ansible_all_ipv4_addresses' all      - узнать ip-адреса всех хостов
ansible -i hosts -m setup -a 'filter=ansible_default_ipv4' all
ansible -i hosts -m setup -a 'filter=ansible_distribution' all
ansible -i hosts -m setup -a 'filter=ansible_distribution_version' all
ansible -i hosts -m setup -a 'filter=ansible_memory_mb' all
ansible -i hosts -m setup -a 'filter=ansible_processor' all