Push Code From Ansible Server to Hosts by using Modules (Method-3)

Share via:

In this article,we will see Push Code From Ansible Server to Hosts by using Modules.

We push the code from ansible server to hosts by using modules.
We execute the modules by running playbooks.

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 the Ansible.
  • Ansible ships with several modules that are called module library, which can be executed directly or remote       hosts through the playbook.
  • We can execute the modules from the command line.
  • Ansible executes each module, usually on the remote target node, and collects return values.
  • Each module supports taking arguments.
  • Nearly all modules take key=value arguments, space delimited.
  • All modules return JSON format data.
  • This means modules can be written in any programming language.
  • Modules should be idempotent, and should avoid making any changes if they detect that the current state         matches the desired final state.
  • we can run mutiple modules by using playbooks.

Playbooks

  • We execute the multiple modules by using playbooks.
  • Playbooks are the files where the Ansible code is written.
  • Playbooks are written in YAML format.
  • YAML means “Yet Another Markup Language,” so there is not much syntax needed.
  • Playbooks are one of the core features of Ansible and tell Ansible what to execute, and it is used in                     complex scenarios.
  • They offer increased flexibility.
  • Each playbook is composed of one or more ‘plays’ in a list.
  • Through a playbook, you can designate specific roles to some of the hosts and other roles to other hosts.
  • By doing this, you can orchestrate multiple servers in very different scenarios, all in one playbook.
  • In the playbook we have many sections are included as show below.

Sections in Ansible Playbooks

  • Target Section
  • Task Section
  • Variables Section
  • Handler Section
  • Loops Section
  • Conditionals Section

Target Section

Gathering Facts

We can collect the information about remoting hosts by using gather_facts module.

Create a playbook “kt-target.yml”

Note
:wq! —– to quit.

Run ansible-playbook to call the playbook “kt-target.yml”

Task Section

Install the package “httpd”

Create a playbook “kt-tasks.yml”

Note
:wq! —– to quit.

Run ansible-playbook to call the playbook “kt-tasks.yml”

Verify the package inside 2 hosts(Nodes)

Note
:wq! —– to quit.

Remove package “httpd” in 2 hosts
Modify the playbook “kt-tasks.yml”

Note
:wq! —– to quit.

Run ansible-playbook to call the playbook “kt-tasks.yml”

Verify the package inside 2 hosts

Variables Section

Install the package “httpd” by using variables

Create a cookbook “kt-variables.yml”

Note
:wq! —– to quit.

Run ansible-playbook to call the playbook “kt-variables.yml”

Verify the package inside 2 hosts(Nodes)

Remove package “httpd”

Modify the playbook “kt-variables.yml”

Note
:wq! —– to quit.

Run ansible-playbook to call the playbook “kt-variables.yml”

Verify the package inside 2 hosts

Handler Section

Install the package “httpd’ and Start the service “httpd”

Create a playbook “kt-handlers.yml”

Note
:wq! —– to quit.

Run ansible-playbook to call the playbook “kt-handlers.yml”

Verify the package “httpd’ and status of the service “httpd”

Verify the package “httpd’

Verify the status of service “httpd”

Stop the service “httpd” inside 2 hosts

Again Run ansible-playbook to call the playbook “kt-handlers.yml”
It won’t start service

Verify the status of service “httpd”

Remove the package “httpd” inside 2 hosts

Modify playbook “kt-handlers.yml”

Note
:wq! —– to quit.

Run ansible-playbook to call the playbook “kt-handlers.yml”

Verity the package “httpd” inside 2 hosts

Dry Run

Check whether the playbook is formatted correctly.
Test how the playbook is going to behave without running the tasks.

Create a playbook “kt-dry.yml”

Note
:wq! —– to quit.

Run ansible-playbook to call the playbook “kt-dry.yml”

Note
It will be used to test the playbook before the run.

Verify files/directories (ktexperts ,ktexperts-directory) inside 2 hosts

Verify user “Ramesh” inside 2 hosts

Verify group”ktexperts” inside 2 hosts

Loops Section

Create files, directories, users and groups by using loops section

Create a playbook “kt-loops.yml”

Note
:wq! —– to quit.

Run ansible-playbook to call the playbook “kt-loops.yml”

Verify all modules inside the 2 hosts

Verify files/directories inside the 2 hosts

Verify users inside the 2 hosts

Verify groups inside the 2 hosts

Conditionals Section

Install the package “httpd” in Redhat family by using conditionals section

Create a playbook “kt-conditionals.yml”

Note
:wq! —– to quit.

Run ansible-playbook to call the playbook “kt-conditionals.yml”

Verify the package “httpd” inside 2 nodes

See the OS Information of 2 Hosts

Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates

KTEXPERTS is always active on below social media platforms.

Facebook : https://www.facebook.com/ktexperts/
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform

 

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Add Comment