This page looks best with JavaScript enabled

How to install and configure ansible, create playbook and run it on remote servers

 ·  ☕ 1 min read  ·  🐧 sysadmin
  1. Here is a video tutorial

Scripts and configuration files are available here:

  1. Install ansible

SLES | openSUSE Leap 15.4

1
sudo zypper install ansible

Debian

1
sudo apt install ansible

Red Hat

1
sudo dnf install ansible
  1. Create ansible directory on /etc
1
sudo mkdir /etc/ansible
  1. Create hosts file inside the /etc/ansible directory
1
sudo vim /etc/ansible/hosts

and put the below content into the file

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
servers:
  hosts:
    worker1:
      ansible_host: 10.10.0.3
      ansible_user: ansible 
    worker2:
      ansible_host: 10.10.0.4
      ansible_user: ansible   
    master:
      ansible_host: 10.10.0.2
      ansible_user: ansible
  1. Check ansible version
1
ansible --version
  1. Make sure that PasswordAuthentication has set boolean value to yes in /etc/ssh/sshd_config file on remote servers.

  2. Create two playbooks. See examples below:

1
vim ssh-session.yaml
1
2
3
4
- hosts: servers
  tasks:
  - name: "connect via ssh"
    shell: for i in $(cat servers); do ssh ansible@$i; done
1
vim passwd-auth.yaml
1
2
3
4
5
6
7
- hosts: servers 
  become: true
  tasks:
  - name: "replace on sshd_config"
    shell: sed -i 's/PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config
  - name: "Restart ssh"
    shell: systemctl restart sshd
  1. Run these playbooks like below:
1
2
3
ansible-playbook ssh-session.yaml
ansible-playbook passwd-auth.yaml
ansible-playbook ssh-session.yaml
Share on

sysadmin
WRITTEN BY
sysadmin
QA & Linux Specialist