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