Ansible: Installation

by Mark Nielsen
Copyright January 2022


The purpose for this document is to:
  1. Links
  2. Install ansible (Ubuntu)
  3. Initial self test
  4. Initial playbook

Links



Install ansible

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

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