Giter Site home page Giter Site logo

tncyclk / ansible-client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from woostju/ansible-client

0.0 0.0 0.0 68 KB

A java client connect to Ansible server through ssh, and run Ansible command

License: Apache License 2.0

Shell 0.04% Java 99.96%

ansible-client's Introduction

ansible-client

A java implemented client connect to Ansible servers through ssh, and run Ansible command.

usage

ansible-client is available from Maven Central

<dependency>
  <groupId>com.github.woostju</groupId>
  <artifactId>ansible-client</artifactId>
  <version>1.0.0-RELEASE</version>
</dependency>

Who is this for?

Anyone who want to execute commands on your Ansible server in java code, instead of logging into server and execute manually.

With this, you can build an automation tool yourself working with Ansible in java.

How do I use this?

Use it in your class:

import com.github.woostju.ssh.SshClientConfig;
import com.github.woostju.ansible.AnsibleClient;
import com.github.woostju.ansible.ReturnValue;
import com.github.woostju.ansible.ReturnValue.Result;

//---------------------------------------

public void execute() {
	AnsibleClient client = new AnsibleClient(new SshClientConfig("hostIp", "sshPort", "username", "password", null));
	Map<String, ReturnValue> result =client.execute(new PingCommand(Lists.newArrayList(host_inner_ip)), 1000);
}

It is recommended to use AnsibleClient with SshClientPool, so that ansibleClient borrows sshClient from the pool to execute command, to avoid create ssh connection each time:

import com.github.woostju.ssh.SshClientConfig;
import com.github.woostju.ansible.AnsibleClient;
import com.github.woostju.ansible.ReturnValue;
import com.github.woostju.ansible.ReturnValue.Result;
import com.github.woostju.ssh.pool.SshClientsPool;

//---------------------------------------

// inject the auto-configured one
@Autowired
SshClientsPool pool;

//---------------------------------------
public void execute() {
	AnsibleClient client = new AnsibleClient(new SshClientConfig("hostIp", "sshPort", "username", "password", null), pool);
	Map<String, ReturnValue> result =client.execute(new PingCommand(Lists.newArrayList(host_inner_ip)), 1000);
}

To get more usage, please refer to the unit test code and java docs.

How does it work?

AnsibleClient connects to Ansible server with ssh, and sends an Ansible adhoc command to server, and parses the output into ReturnValue.

How many Ansible module does ansible-client support?

Until this release, ansible-client supports modules below:

module name module class description official link
command CmdCommand The command will be executed on hosts link
copy CopyCommand Copies a file from the local or remote machine to a location on the remote machine link
file FileCommand Manage files and file properties link
git GitCommand Deploy software (or files) from git checkouts link
ping PingCommand Try to connect to host, verify a usable python and return pong on success link
playbook PlaybookCommand Run playbook with ansible-playbook executable link
script ScriptCommand Runs a local script on a remote node after transferring it link

Since Ansible itself has dozens of modules, you can also define your custom command class to work with AnsibleClient.

import java.util.List;

import com.github.woostju.ansible.Module;

/**
* Add or remove MSSQL databases from a remote host.
*/
public class Mssql_dbCommand extends Command{

	/**
	 * @param hosts target hosts
	 */
	public Mssql_dbCommand(List<String> hosts, String login_host, String login_password, int login_port, String login_user, String name, String target, String state) {
		this(hosts, Lists.newArrayList("login_host="+login_host,
		"login_password="+login_password,
		"login_port="+login_port,
		"login_user="+login_user,
		"name="+name,
		"target="+target,
		"state="+state), null);
	}
	
	public Mssql_dbCommand(List<String> hosts, List<String> moduleArgs, List<String> options) {
		super(hosts, Module.ping.toString(), moduleArgs, options);
	}
}

License

This code is under the Apache Licence v2.

Additional Resources

  • SshClientPool a java implementation of ssh clients object pool with sshj, apache common pool2, expectIt

ansible-client's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.