Ways to Push Code From Ansible Server to Hosts

Share via:

Dear Readers.

In this article,we will see Ways to Push Code From Ansible Server to Hosts.

Generally we push the code from ansible server to hosts by using following ways.

  1. Ad-Hoc Commands
  2. Single Module
  3. Modules (More than one module)

 

1. Ad-Hoc Commands

  • The Ad-Hoc Commands are simple linux commands.
  • The Ad-Hoc command is the one-liner ansible command that performs one task on the target host.
  • Use ad-hoc tasks really quick & don’t want to save for later
  • These are quick one-liner without writing a playbook.
  • Use ad-hoc tasks really quick & don’t want to save for later.
  • These are quick one-liner without writing a playbook.
  • It allows you to execute simple one-line task against one or group of hosts defined on the inventory  file                          configuration.
  • An Ad-Hoc command will only have two parameters, the group of a host that you want to perform the task and the        Ansible module to run.
  • The Ad-Hoc command gives you more advantage for exploring ansible itself.

Syntax for Ad-Hoc

To run an arbitrary, we need to mention -a in the command

To run anything with sudo, we need to mention -b in the command

Examples

See the list of all hosts and all groups of inventory

See the list of all hosts inside the group “Ktexperts-Group”

See the list of files/directories in 2 hosts

Here, we need to use “-a” to run an arbitrary cmd.

Install the package “httpd” in 2 hosts

We need to run with sudo to install the package.

Use -b in the following command to get sudo privileges.

2. Single Module

Single Module
Ansible modules are discrete units of code which can be used from the command line.
You are able to perform tasks without creating a playbook first, such as rebooting servers, managing services, editing the line configuration, copy a file to only one host, install only one package.
By using single module,we can perform only one task at a time.
If you want to execute more than one module at a time,we need to execute modules.

Syntex for Single Module

To run an arbitrary cmd use -a & use -m to run a module

Examples

Verify the host are pinging or not

See the hostname of 2 hosts

3. Modules (Run more than one Module)

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.

Playbook Structure
Each playbook is a collection of one or more plays.
Playbooks are structured by using Plays. There can be more than one play inside a playbook.


The function of the play is to map a set of instructions which is defined against a particular host.
A YAML starts with — (3 hyphens) always.

Syntex for running the playbook

We write the code in a file then we execute the file by using below command.

Create a playbook “kt-target.yml”

Note
:wq! —– to quit.

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

Thank you for giving your valuable time to read the above information.
Follow us on 
Website  www.ktexperts.com
Facebook Page KTexperts
Linkedin Page : KT EXPERTS

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

Add Comment