What do we know about ansible - lastkrick/b25 GitHub Wiki
What ansible is
Ansible initially is a SCM(software configuration management) tool. It helps us to run some commands, install and configure software on hardware(any linux or windows machines or even some routers and switches and so on).
Ansible has a lot of very good doc.
Here is all ansible concepts and terminology: https://docs.ansible.com/ansible/latest/user_guide/basic_concepts.html#basic-concepts
You can use three different ways to run ansible:
- ad-hoc commands. It helps run any ansible tasks as oneliner. Usually used to run some command on a few remote nodes.
- playbooks. It's a list of ansible tasks, which run one after one. It could be used for fast and simple configuration of nodes.
- playbooks with roles. Roles is like a package for tasks/files/templates/variabes/handlers/etc. It's a way to simple share ansible code and organise common tasks in one package. For example, OpenVPN role is a role which stores all required tasks for install and configure OpenVPN server and agent, you just need to use this role in playbook and set required variables.
Every ansible task should run some module. Modules are the main magic of ansible. It has a lot of modules. Really a lot! Modules are like commands to run. But, it does a lot more than just one command.
Few versions ago they moved all modules and plugins to different collections just because it was hard to maintain all modules in one place. Here is a list of all collections: https://docs.ansible.com/ansible/latest/collections/index.html Check all interesting for you. But anyway you should read all modules and plugins in this two collections:
- https://docs.ansible.com/ansible/latest/collections/ansible/builtin/index.html#plugins-in-ansible-builtin
- https://docs.ansible.com/ansible/latest/collections/ansible/posix/index.html#plugins-in-ansible-posix
How ansible works
Ansible architecture is very simple. When you asks it to run something on remote machine, it will dynamically prepare python python script(one or few) on your local machine. Then it will transfer it to remote machine(usually via ssh), run it on remote machine and gather output.
Lets run some real example
simplest way to run ansible is to use ad-hoc commands. You just need setup inventory and run ansible command. Inventory is a thing which tells all required info about your hosts, it could be ini/yaml/toml file or even some script for dynamic environments. Here is more on ini inventories: https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#intro-inventory Simplest ini inventory are like those:
first tells ansible about 100 rpis in your infra and sets common user and password for all nodes.
with second inventory ansible will try to connect to all hundred hosts using keys, also it declares groups for main and slave nodes.
This command will run ansible ad-hoc command with simple module 'ping' which just check if remote node is accessible:
ansible -i inventory-password -m ping all
This command will run ansible ad-hoc command on all nodes defined in inventory 'inventory-password' and run module 'command' with argument 'free -m'(which actually will run this command)
ansible -i inventory-password -m command -a "free -m" all