Ansible: Installation and Use
by Mark Nielsen
Copyright January 2022
The purpose for this document is to:
- Install server and client on the same computer.
- Run a command for ansible to do basic commands.
- Run a very simple playbook.
- Links
- Install ansible (Ubuntu)
- Initial self test
- Initial playbook
Links
- https://docs.ansible.com/ansible/latest/dev_guide/overview_architecture.html
- https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
- https://adamtheautomator.com/install-ansible/
- https://www.howtoforge.com/how-to-install-and-test-ansible-on-linux/
- https://docs.ansible.com/ansible/latest/network/getting_started/first_playbook.html
- https://www.softwaretestinghelp.com/ansible-tutorial-1/
- https://rdr-it.com/en/ansible-installation-configuration-and-use-with-windows-and-linux/
- https://docs.openstack.org/project-deploy-guide/openstack-ansible/latest/
- https://docs.alfresco.com/content-services/latest/install/ansible/
- https://blog.risingstack.com/getting-started-with-ansible-infrastructure-automation/
- https://www.ntweekly.com/2020/06/14/use-passwordless-ssh-keys-with-ansible-to-manage-machine/
Please read:
- https://docs.ansible.com/ansible/latest/dev_guide/overview_architecture.html
- 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"
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
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