Ansible Playbook and Ansible Modules
An Ansible playbook is an organized unit of scripts that defines work for a server configuration managed by the automation tool Ansible. Ansible is a configuration management tool that automates the configuration of multiple servers by the use of Ansible playbooks.
Playbook: A single yaml file
Play: Defines a set of activities (Tasks) to be run on the hosts
Task: An action to be performed on the hosts
- Execute a command
- Run a script
- Install a package
- Shutdown/Restart
Playbook is a list of dictionaries in YAML terms. Each play is a dictionary that has a set of properties called name, hosts, and tasks. Remember, these properties of a dictionary and so the order doesn’t really matter.Even if you swap the position of name host, it’s still a valid play.However, this is not the same for tasks.
The tasks, as you can see is a list or an array as denoted by the dashes. Lists are ordered collections. The position of entries matter. If you swap the position of entries here while instructing Ansible to start the web service first, before installing the HTTPD service, which is not desired.
The YAML format is key while developing playbooks, you must pay extra attention to the indentation and structure of the file. The host parameter indicates which hosts you want these operations to run on. Remember, the host you want to perform these operations against is always set at a play level.
Basic Architecture of Distributed Ansible Cluster :
- Before installing software we need to understand why and how we use Ansible.
Normally in industry we have thousands of servers and manually configuring those servers is a very haptic and time consuming task.
Here the play of Ansible come. We can configure any kind of server using Ansible in just a few minutes and also We can configure thousands of servers in parallel using Ansible.
Let’s understand the below shown architecture of Ansible…
Let’s have a look at some of the terminology used in ansible:
- Controller Machine: Machine where Ansible is installed
- Inventory: Information regarding servers to be managed
- Playbook: Automation is defined using tasks defined in YAML format
- Task: Procedure to be executed
- Module: Predefined commands executed directly on remote hosts
- Play: Execution of a playbook
- Role: a Pre-defined way for organizing playbooks
- Handlers: Tasks with unique names that will only be executed if notified by another task
Currently, in the below Yaml this is set to localhost, which means that all these actions listed under tasks are going to be performed on the localhost. You could have any host group specified here, you must ensure that the host group is first defined in the inventory file we created earlier. The host defined in the inventory file must match the host used in the playbooks, and all connection information for the host is retrieved from the inventory file. If you specify a group instead, the listed tasks will be executed on all the hosts listed under that group simultaneously.
Modules
Modules: The different actions run by tasks are called modules. In this case, command script,YAML, and service are Ansible modules. There are hundreds of other modules available out of the box. Information about these modules is available in the Ansible documentation website, you could simply run the Ansible-doc-L command.
Finally, once you successfully build the Ansible playbook, how do you run it?
It’s very simple. Execute the Ansible-playbook command specify the name of the Ansible playbook you just created, and that’s it. If you do Ansible playbook-help command, you will get to know more about some additional parameters available for this command.
We are going to see two ways of running Ansible. First, using the Ansible command and then using the Ansible playbook command.Sometimes you may want to use Ansible for a one-off task such as to test connectivity between the Ansible controller and the targets or to run the command, say for example, to shut down a set of servers.In that case, you can get away without writing a playbook by running the Ansible command followed by the host and the command to reboot the host.
The real usage of Ansible is with playbooks. We will now use the Ansible-playbook command to execute those playbooks. That is a declarative approach. The playbooks can now be saved on source code repositories like GitHub and managed centrally.
Remember the Ansible-playbook command requires you to develop a playbook.
We will use the -M parameter and specify the module that we’re going to use and the module is Ping in this case. That is followed by -I parameter to specify the inventory file.
Now, remember both the Ansible and the Ansible-playbook commands require the inventory file specified through the -I parameter.
In the inventory.txt please add your hosts and targets
Playbook yaml
Types of Modules
We will now look at Modules in Ansible. Ansible modules are categorized into various groups based on their functionality; some of them are listed here.
System modules are actions to be performed at a system level such as modifying the users and groups on the system, modifying iptables, firewall configurations on the system, working with logical volume groups, mounting operations, or working with services.
For example, starting, stopping, or restarting services in a system.
Command Module: Command modules are used to execute commands or scripts on a host. These could be simple commands using the command module or an interactiveexecution using the expect module by responding to prompts. You could also run a script on the host using the script module.
File Module: File modules help work with files.
For example, use the ACL module to set and retrieve ACL information on files. Use the archive and unarchive modules to compress and unpack files. Use find, lineinfile, and replace modules to modify the contents of an existing file.
Database Module: Database modules help in working with databases such asMongoDB, MySQL, MSSQL, or PostgreSQL to add or remove databases or modify database configurations.
CLoud Module: The cloud section has a vast collection of modules for various different cloud providers like Amazon, Azure, Docker, Google, OpenStack, and VMware being just a few of them. There are a number of modules available for each of these that allow you to perform various tasks such as creating and destroying instances, performing configuration changes in networking & security, managing containers, datacenters, clusters, virtual networking, VSAN and many more
These are just a few modules in a few categories. There are a lot more and a comprehensive list can be found at docs.ansible.com along with detailed instructions on each of them.
Script Module:
Service Module:
That’s why we are using started not start.
Stay Tuned for Ansible Roles
Hope it Helps :)