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 ansible to install and configure a new MySQL Master and Slave. It will be configured to stop if any installation exists and report and error.
- Other things about Ansible.
- Links
- Architecture, Install, and general configuration
- Basic commands and configuration
- Custom installation with MySQL Master and Slave
- Other things about Ansible
- Playbooks: making and downloading
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
For architecture, please read the online docs. In general:
- 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.
- 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.
- Modules
- 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.
- 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.
- 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"
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
Configuration
- Inventory
- Roles
- Modules
- Playbook
Commands
Read
- https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html
- https://docs.ansible.com/ansible/latest/network/getting_started/first_playbook.html
- https://www.redhat.com/sysadmin/ansible-updates1
- https://docs.ansible.com/ansible/latest/reference_appendices/module_utils.html
- https://docs.ansible.com/ansible/latest/dev_guide/developing_program_flow_modules.html
- 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