One of the ways Ansible executes specific tasks is through the use of modules. Modules are discrete units of code that can be used in an Ansible playbook or directly from a command-line interface. By leveraging Ansible modules you can easily manage and maintain your Ansible deployments. In this article, we will outline how Ansible Modules are used as well as provide some examples of modules and their various functionalities.
Start using Ansible modules with Ansible today!
Using Ansible Modules from Command-Line
As previously mentioned, Ansible modules can be used from the command-line or in an Ansible playbook. When executing an Ansible module from the command-line, you can sometimes supply additional arguments to further modify how the module is executed by Ansible. For example, if you want to use the Commands Module “command” to output a simple text message on your server, you would use the following command:
ansible localhost -m command -a “/bin/echo hello, world!”
In this example, -m is a command-line argument that specifies that a module is being called. The argument -a simply passes along the other arguments specified in the command.
Additionally, most Ansible modules support space
delimited key=value arguments. For example, if you want to use the system module service to start the HTTP service on your server, you can use the following command:
ansible webservers -i inventory.yml -m service -a "name=httpd state=started"
As you can see in this example, you can specify the name of the service as well as the desired state of that service using the
key=value argument format. The argument -i specifies the inventory file being used to determine which servers are included in the webservers group.
Using Ansible Modules in Playbooks
Using modules in Ansible playbooks largely functions the same way as it does in the command-line, with the main difference being that the modules will be stored in the playbook file rather than executed directly on the command-line. For example, if we wish to reboot a server immediately just as we did from the command-line, we would include the following lines in our playbook file:
-name: reboot the servers
command: /sbin/reboot -t now
While you can reboot your server using the command module, there is also a specific module that will allow you to reboot your server, called the reboot module. This can be used in a playbook the same as the command module:
-name: reboot the servers
Even though both modules accomplish the same goal, the reboot module is specific while the command module is more generalized.
As all modules return JSON format data, you can write modules in any programming language, allowing for greater flexibility in development and deployment. To review a list of all available Ansible Modules, you can use the following command:
Now that you know how to use Ansible modules, you will likely want to familiarize yourself with some of the existing modules on this list. By integrating these modules into your workflow you can further enhance your Ansible deployment and automation strategies.