Ansible Modules

Ansible Modules


Ansible modules are discrete units of code which can be used from the command line or in a playbook task. The modules also referred to as task plugins or library plugins in Ansible.

Ansible ships with several modules that are called module libraries, which can be executed directly or remote hosts through the playbook.




Users can also write their modules. These modules can control services, system resources, files, or

packages, etc. and handle executing system commands.





Ansible Modules Syntax


Syntax

# ansible <hosts> -b [sudo] -m <module_name> -a <"arguments"> -u <username>

Explanation

Hosts: It can be an entry in the inventory file. For specifying all hosts in the inventory, use all or "*".

Modules: There are hundreds of modules available in the Ansible, such as shell, yum, apt, file, and copy. By default, it is the command.

Arguments: We should pass values that are required by the module. It can change according to the module used.

Username: It specifies the user account in which Ansible can execute commands.

Become: It's an optional parameter specified when we want to run operations that need sudo privilege. By default, it becomes false.

Ansible Modules Commands



State use:
installed - present
remove - absent
update - latest


To install httpd on demo group with sudo permission :

[user@ip]#ansible demo -b -m yum -a “pkg=httpd state=present”


To update httpd on demo group with sudo permission :

[user@ip]#ansible demo -b -m yum -a “pkg=httpd state=latest”


To remove httpd on demo group with sudo permission :

[user@ip]#ansible demo -b -m yum -a “pkg=httpd state=absent”


To restart httpd service on demo group with sudo permission :

[user@ip]#ansible demo -b -m service -a “pkg=httpd state=started”


To create user on demo group with sudo permission:

[user@ip]#ansible demo -b -m user -a “name=raj”


To copy file on demo group with sudo permission:

[user@ip]#ansible demo -b -m copy -a “src=file1 dest=/tmp”




Share:

0 comments

Please leave your comments...... Thanks