Lecture Notes

Server Setup and Configuration

Initial Server Setup

  • Master node is on AWS, using Red Hat.
  • Node one is Ubuntu.
  • Node two is Red Hat (CentOS/Linux).

Master Node Configuration

  • Set the hostname:
    • sudo hostnamectl set-hostname master
    • Update the system: sudo apt update.

User Creation

  • Create a new user:
    • sudo useradd asadmin
    • Set the password: sudo passwd asadmin
    • Add the user to the sudoers file (sudo visudo):
      • Add the line: asadmin ALL=(ALL:ALL) ALL

SSH Configuration

  • Enable password authentication in SSH:
    • Edit the SSH configuration file: sudo vi /etc/ssh/sshd_config
    • Uncomment PasswordAuthentication yes
    • Add/modify PermitEmptyPasswords no.
    • Add/modify Copy this line with the cloud in it, paste it, and say yes.
  • Restart SSH service:
    • sudo service sshd restart (old version) or sudo systemctl restart sshd

Worker Node One (Ubuntu) Configuration

  • Create a user:
    • sudo adduser asadmin
    • Set the password.
    • Add user to sudoers: sudo vi /etc/sudoers
      • Add the line: asadmin ALL=(ALL:ALL) ALL
  • Configure SSH:
    • sudo vi /etc/ssh/sshd_config
      • Enable password authentication: PasswordAuthentication yes
    • Modify cloud init: sudo vi /etc/cloud/cloud.cfg.d/99-disable-password-auth.cfg
      • Change ssh_pwauth: no to ssh_pwauth: yes
  • Restart SSH: sudo systemctl restart ssh

Worker Node Two (Red Hat/CentOS) Configuration

  • Set hostname:
    • sudo hostnamectl set-hostname node1
    • sudo hostnamectl set-hostname node2
  • Create user:
    • sudo useradd asadmin
    • sudo passwd asadmin
  • Add user to sudoers: sudo vi /etc/sudoers
    * Add the line: asadmin ALL=(ALL:ALL) ALL
  • Configure SSH:
    • sudo vi /etc/ssh/sshd_config
      • Enable password authentication: PasswordAuthentication yes
    • Modify cloud init: sudo vi /etc/cloud/cloud.cfg.d/99-disable-password-auth.cfg
      • Change ssh_pwauth: no to ssh_pwauth: yes
  • Restart SSH: sudo systemctl restart sshd

Testing SSH Access

  • Log in as the new user:
    • sudo su - asadmin
  • SSH from master to worker nodes:
    • ssh asadmin@<public_ip>

SSH Key Generation and Distribution

  • Generate SSH key on the master node:
    • ssh-keygen
  • Copy the public key to worker nodes:
    • ssh-copy-id asadmin@<worker_node_public_ip>

Ansible Installation and Configuration

  • Install pip:
    • sudo apt install python3-pip (Ubuntu)
  • Install Ansible:
    • pip3 install ansible==2.15.13
  • Verify Ansible installation:
    • ansible --version

Ansible Project Setup

  • Create a project directory:
    • mkdir projects
    • cd projects
  • Create an inventory file (inventory):
    • Define groups (dev, qa) with server IP addresses.
  • Create an Ansible configuration file (ansible.cfg):
    • ini [defaults] inventory = ./inventory host_key_checking = False
  • Set environment variables:
    • export ANSIBLE_HOST_KEY_CHECKING=False

Testing Ansible Setup

  • Ping the dev environment:
    • ansible dev -m ping
  • Ping the qa environment:
    • ansible qa -m ping
  • Ping all servers:
    • ansible all -m ping

Ansible Modules

  • Command Module: Executes shell commands.
  • Shell Module: Executes shell commands (more powerful than command module).
  • File Module: Manages files and directories.
  • YUM Module: Installs, updates, and removes packages on Red Hat-based systems.
  • Setup Module: Gathers facts about the managed hosts.

Gathering Facts

  • Use the setup module to gather information about the servers.
    • ansible all -m setup
  • Filter facts to get specific information:
    • `ansible dev -m setup -a