Ansible : UDMY -- 9. Ansible Modules
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
In this module we will be talking about ansible module called "setup" which is used to gather information about modules,
The file module for a variety of different file based operations. We will look at the color notation used at the ansible execution . And how it relates to the key feature of Ansible called "Idempotency"
We will be using the
We will be using the copy module and the command module,
Ansible.doc to quickly understand the options available while using ansible modules .
Used for gathering facts about playbooks.
$ ansible centos1 -m setup
$ ansible centos1 -m setup | more
We can see information about devices .
loads of information that get gathered here as facts ,
Using the file module using the combination of paths and states , path to the file being managed
https://docs.ansible.com/ansible/2.9/modules/fetch_module.html#fetch-module
$ ansible all -m file -a 'path=/tmp/test state=touch'
This file gets created in all the hosts as we have give [all] group .
Ansible uses the following color schemes . for its commands
- Green indicates success .
- Yellow success with changes
- Red is a failure
And we can see in the command output it is yellow with changes ,
if you run this command once again
it once again has a yellow colour as there is a different timestamp .
we are going to tell it to set the mode to 600 file permissions
read write for user and no permission for the group and others,The first time when you run it will have a yellow output , the next time when you set it it will have a green output. Essentially nothing is changed .
This is the important feature in Ansible know as Idempotentcy
This feature is especially powerful when it comes to ansible.
Let put this to test , if we change the mode of this file
$ chmod 644 /tmp/test
The next module that you are going to look at is the "copy " module .
The copy module uses the checksum module to validate whether the copy was needed and was successful . We can also use copy modules to copy files
Command modules
$ ansible all -m command -a 'hostname' -o
Return Code of 0 , or rc=0 stating that they were successful .
The command module is the default module that you will use for the Ansible Adhoc : commands.
since command is the default command you can skip the command -- parameter and you will receive the same output .
$ ansible all -a 'hostname' -o
or
$ ansible all -m command -a 'hostname -o
You can check if you have the root connectivity using the command module.
$ ansible all -a 'id'
A feature of the command module which is particularly useful for creates and removes variable . which can be used for idempotence .
- creates if the file exist it will not be run.
- removes , if the file exists it will not be run
$ ansible all -a 'touch /tmp/test_copy_module creates=/tmp/test_copy_module'
here it has skipped the file , And now if we re-run this again .
That's because this file already exists and therefore it will not create the file again. In this case to the return code is 0 which is deemed it is successful .
Removing the file .
$ ansible all -a 'rm /tmp/test_copy_module removes=/tmp/test_copy_module'
If you run that command again it skips.
$ ansible all -m file -a 'path=/tmp/test_copy_module state=absent'
All of these must be green since this file does not exist .
There are loads of modules available in Ansible .
fetch Module :
$ ansible all -m file -a 'path=/tmp/test_module.txt state=touch mode=600'
$ ansible centos1 -m file -a 'path=/tmp/test_module.txt state=touch mode=600'
$ ansible all -m fetch -a 'src=/tmp/test_module.txt dest=/tmp/test_module.txt'
You would expect that to come back as a file , but what it actual done is that it has created a directory .
with in which we have a folder centos1 and then the folder structure /tmp
cd /tmp/test_module.txt/centos1/tmp
$ ansible-doc file
Thanks for providing this information
ReplyDeleteBest Power BI Training
Power BI Online Training