Ansible - 3
Ad-hoc Commands, Modules & Playbook (YAML means yet another markup language)
--------------------------------------------
1) Ad-hoc Commands :-
simple linux commands, Temporary command, there is No idempotency in adhoc commands it means it will again and again do the same task without checking that it is already exist. if we have created one file with command touch file1 then it will again run the same command and overwrite the file1 which is not good for prod environment. so it is not always good to use adhoc commands in ansible.
2) Modules :-
We use YAML in module. It is used for running single work or command.
3) Playbooks:-
we use YAML in playbook too but if we want to run multiple command and want to excute multiple task or work then we call it playbook. We use more than one module in it.
============================================================================================================================================================
Ad-hoc Commands
--------------
-> Ad-hoc commands are commands which can be run individually to perform quick functions.
-> These ad-hoc commands are not used for configuration management and deployement, because these commands are of One time usage.
-> The ansible ad-hoc commands uses the /usr/bin/ansible command line tool to automate a single task.
Go to ansible server
su - ansible
[ansible@ip]$ ansible demo -a "ls" (it will give you output of ls)
[ansible@ip]$ ansible demo[0] -a "touch files" (it will create a file)
[ansible@ip]$ ansible all -a "touch files4" (it will create a file)
[ansible@ip]$ ansible demo -a "ls -al" (it will give you output of ls -al)
[ansible@ip]$ ansible demo -a "sudo yum install httpd -y" (it will install the package)
or
[ansible@ip]$ ansible demo -ba "yum install httpd -y" (it will install the package)
[ansible@ip]$ ansible demo -ba "yum remove httpd -y" (it will remove the package)
======================================================
Ansible Modules
==============
-> Ansible ships with a number of modules (called module library) that can be executed directly on remote hosts or through playbooks.
-> Your library of modules can reside on any machine, and there are no servers, daemons, or databases required.
Q ->where ansible modules on stored?
the default location for the inventory file is /etc/ansible/hosts
Modules commands
--------------
[ansible@ip]$ ansible demo -b -m yum -a "pkg=httpd state=present" (it will install the package)
[ansible@ip]$ ansible demo -b -m yum -a "pkg=httpd state=latest" (it will update the package)
[ansible@ip]$ ansible demo -b -m yum -a "pkg=httpd state=absent" (it will uninstall the package)
[ansible@ip]$ ansible demo -b -m service -a "name=httpd state=started" (it will start the http process)
[ansible@ip]$ ansible demo -b -m service -a "name=httpd state=stopped" (it will stop the http process)
[ansible@ip]$ ansible demo -b -m user -a "name=raj" (it will create the raj user on server)
[ansible@ip]$ ansible demo -b -m copy -a "src=file4 dest=/tmp" (it will copy the file from ansible server to nodes)
[ansible@ip]$ ansible demo -b -m copy -a "src=/tmp/preeti dest=/tmp" (it will start the http process)
if i want to copy the file only at one server.
[ansible@ip]$ ansible demo[-1] -b -m copy -a "src=preeti dest=/tmp" (it will start the http process)
present means - install
absent means - remove or uninstall
update means - latest
-b = sudo privelage
-m = module
you do not need to create a yaml file as it is already there by ansible.
important point - module and playbook is having idempotency present. it will not again do the same task again and again.
important module in point of view of interview
stepup module - it check the existing configuration or files on nodes and after that it will work accordingly.
[ansible@ip]$ ansible demo -m setup
[ansible@ip]$ ansible demo -m setup -a "filter=*ipv4*" (it will give you information about ipv4 the server)
Comments
Post a Comment