Ansible Lab - Zacham17/my-tech-journal GitHub Wiki

Brief Summary

In this lab, I installed Ansible on a linux workstation and used it to establish an SSH connection to a CentOS server and install nginx onto it.

How to install and configure Ansible

  • Before using Ansible, you must generate an SSH key using the command, ssh-keygen -t rsa -b 4096
  • You can copy the key to the remote systems by typing a command with the following syntax ssh-copy-id DOMAINUSER@FQDN_OF_REMOTESYSTEM
  • To install Ansible, run the command sudo apt install ansible
  • You can edit the contents of your Ansible inventory by editing the /etc/ansible/hosts file.
  • In the /etc/ansible/hosts file, I added the two CentOS7 servers I would be using Ansible to SSH onto.
  • After adding to the hosts file, you can test that ansible can connect to the remote systems by running the following command to ping the system, ansible all -m ping -u DOMAINUSER

Installing nginx with Ansible

  • Add the following to the /etc/ansible/hosts file:
    ansible_become=yes  # use sudo 
    ansible_become_method=sudo 
    ansible_become_pass='{{ my_cluser_sudo_pass }}'
    
  • Use the following playbook file to install nginx, located here
  • Create an index.html file in the same directory and add some html code to it.
  • After downloading that file, you can use the command ansible-playbook -u DOMAINUSER --ask-become-pass PLAYBOOK_FILENAME to install nginx
  • On the remote system, start the nginx service using systemctl start nginx
  • You can now browse to the web page hosted on the server

Performing updates using Ansible

  • You can use a playbook file update the remote CentOS7
  • The following is an example of a playbook file that does this
    ---
    - name: Update Server
    hosts: mailServer,webServer
    
    tasks:
    - name: Update CentOS7
    - raw: yum update -y
    
  • Type the following command to execute the playbook file: ansible-playbook --ask-become-pass PLAYBOOK_FILENAME
  • This will update your remote systems
  • TIP: You can use --verbose with the command to see the background processes that the command runs.