Ansible Modules
• Modules are units which actually get the work done in ansible. Ansible’s module are used to control system resources, like services, packages, execute system commands, or files
• There are over 450 in-built modules in ansible
• Modules abstract system tasks, like dealing with packages or handling files from ansible servers
• Users can choose to either use a module from ansible’s in-built library or create their own custom modules
• Almost all of the modules support taking arguments in key-value format
Exercise : Ansible Modules
Run the following command to call the file module, as shown in the following command, to see details about /etc/fstab
$ ansible localhost -u root -k -m file -a 'path=/etc/fstab'
Run following command to create a new test directory in /tmp
$ ansible localhost -m file -a "dest=/tmp/test mode=644 state=directory"
To copy the /etc/fstab file to /tmp on the managed machine, you will use the following command:
$ ansible localhost -m copy -a 'src=/etc/fstab dest=/tmp/fstab'
There is also a module named command that will run any arbitrary command on the managed machine. You can run the command as follows:
$ ansible localhost -m command -a 'rm -rf /tmp/testing removes=/tmp/testing'
If you give a creates argument, the command will not run if the filename argument exists. The opposite is true of the removes argument; if the filename exists, the command will run.
It would be much less work for Ansible and also the person writing the configurations to use the file module in this instance, since the file module will recursively delete something if the state is set to absent. So, the preceding command would be equivalent to the following command:
$ ansible localhost -m file -a 'path=/tmp/testing state=absent'
If you need to use features usually available in a shell while running your command, you will need the shell module. This way you can use redirection, pipes, or job back grounding. You can pick which shell to use with the executable argument. You can use the shell module as follows:
$ ansible localhost -s -m shell -a "tail /var/log/messages | \grep ansible-command | wc -l"
The ansible-doc command also allows you to see a list of all modules available to you. To get a list of all the modules that are available to you along with a short description of each type, use the following command:
$ ansible-doc -l
To see the help file for a particular module, you supply it as the single argument to ansible-doc. To see the help information for the file module, for example, use the following command:
$ ansible-doc file
Comments
Post a Comment