Ansible: Installation and Use

by Mark Nielsen
Copyright January 2022


The purpose for this document is to:
  1. Links
  2. Architecture, Install, and general configuration
  3. Basic commands and configuration
  4. Custom installation with MySQL Master and Slave
  5. Other things about Ansible
  6. Playbooks: making and downloading

Links



Architecture, Install, and General Configuration

Please read:
  1. https://docs.ansible.com/ansible/latest/dev_guide/overview_architecture.html
  2. https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#installing-ansible-on-specific-operating-systems
For architecture, please read the online docs. In general:
  1. Inventory : "Names" for groups of servers. Like Database, webserver, etc. Variables for the servers can be defined within the inventory file or in other sub-directories.
  2. Roles : What you define the "Groups" you defined in inventory. For example, the database group in the inventory should have a database role. Based on roles, different modules may be used or behave differently.
  3. Modules
  4. Playbooks : The roles define with modules to use and how, then get the list of commands from modules, and apply to the servers in a group from the inventory.
  5. Tasks : Stuff you do on servers. This can be be by applying modules to servers from the roles defined for groups. You can issue commands manually.
  6. Variables : Variables are defined for hosts which can affect how modules do stuff to those servers.
For example you might have db1.server.local and db2.server.local are in a Group called "databases". Their roles include : mysql_database, Apache_webserver, ssh. And the modules tell you how those roles get executed on those servers. You want you databases to have a database, a webserver, ssh, and maybe other stuff like monitoring, backups, etc.

Install

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

  cd /root

  # make an ssh key and make it so we can login in as root to localhost. 
  ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa
  cat .ssh/id_rsa.pub >> .ssh/authorized_keys
  chmod 644 .ssh/authorized_keys
  ssh -o "StrictHostKeyChecking no" 127.0.0.1 echo "done"



Initial self test.

  cd /etc/ansible

  touch /etc/ansible/hosts
  echo "[self]"    >> hosts
  echo "127.0.0.1" >> hosts

  echo "" >> hosts
  echo "[self:vars]
ansible_connection=ssh  

   " >> hosts

  mv -f ansible.cfg anisble.cfg_initial

  echo "[defaults]
  inventory = hosts
  host_key_checking = False
        " >> ansible.cfg
  

Verify the initial "self" commands"

    ansible -m ping self               # using the ping module
    ansible self -a " echo 'hello'"    # and ad-hoc command
    ansible self -a " date"            # get date
    ansible self -a " uptime"          # Get the uptime for this server


Initial playbook

  cd /etc/anisble
  mkdir -p playbooks

  echo "
  - hosts: all

    tasks:
      - name: Ensure a list of packages installed
        apt:
          name: htop
          state: present
  " >> playbooks/test-package.yml

Now run the playbook.

    ansible-playbook -i "127.0.0.1," playbooks/test-package.yml
  

Re test the playbook.

    apt-get -y remove htop
    # Make sure it doesn't exist
    htop
    
    # Then rerun the playbook
    ansible-playbook -i "127.0.0.1," playbooks/test-package.yml

    # Then see if it exists
    htop
    


Basic commands and configuration

Configuration

  1. Inventory
  2. Roles
  3. Modules
  4. Playbook

Commands

Read
  1. https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html
  2. https://docs.ansible.com/ansible/latest/network/getting_started/first_playbook.html
  3. https://www.redhat.com/sysadmin/ansible-updates1
  4. https://docs.ansible.com/ansible/latest/reference_appendices/module_utils.html
  5. https://docs.ansible.com/ansible/latest/dev_guide/developing_program_flow_modules.html
  6. https://blog.toast38coza.me/custom-ansible-module-hello-world/

Make and Run simple playbook

Make your playbook, very simple. This is The Info Module.
cd /usr/lib/python3/dist-packages/ansible/modules/
echo "#!/usr/bin/python

from ansible.module_utils.basic import Ansible Module

def run_module():
# This gets the args for the module.
# supports_check_mode=True makes sure nothing gets changed.
    module_args = dict(name=dict(type='str', required=True) , supports_check_mode=True

    
def main():
    run_module()


if __name__ == '__main__':
    main()


" > TEST1.py
chmod 755 TEST1.py

      

This is your facts module.

	


MySQL Master and Slave setup

Modules

The purpose of the modules is to setup MySQL configuration and setup replication. It also checks there is no mysql installation installed on either box, or aborts.

Other things with ansible