com.github.woostju:ansible-client

ansible client with ssh

License

License

Categories

Categories

CLI User Interface
GroupId

GroupId

com.github.woostju
ArtifactId

ArtifactId

ansible-client
Last Version

Last Version

1.0.0-RELEASE
Release Date

Release Date

Type

Type

jar
Description

Description

com.github.woostju:ansible-client
ansible client with ssh
Project URL

Project URL

https://github.com/woostju/ansible-client
Project Organization

Project Organization

Pivotal Software, Inc.
Source Code Management

Source Code Management

https://github.com/woostju/ansible-client.git

Download ansible-client

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.woostju/ansible-client/ -->
<dependency>
    <groupId>com.github.woostju</groupId>
    <artifactId>ansible-client</artifactId>
    <version>1.0.0-RELEASE</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.woostju/ansible-client/
implementation 'com.github.woostju:ansible-client:1.0.0-RELEASE'
// https://jarcasting.com/artifacts/com.github.woostju/ansible-client/
implementation ("com.github.woostju:ansible-client:1.0.0-RELEASE")
'com.github.woostju:ansible-client:jar:1.0.0-RELEASE'
<dependency org="com.github.woostju" name="ansible-client" rev="1.0.0-RELEASE">
  <artifact name="ansible-client" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.woostju', module='ansible-client', version='1.0.0-RELEASE')
)
libraryDependencies += "com.github.woostju" % "ansible-client" % "1.0.0-RELEASE"
[com.github.woostju/ansible-client "1.0.0-RELEASE"]

Dependencies

compile (6)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-test jar 1.5.10.RELEASE
ch.qos.logback : logback-classic jar 1.1.11
com.github.woostju : ssh-client-pool jar 1.0.1-RELEASE
com.fasterxml.jackson.core : jackson-databind jar
com.fasterxml.jackson.core : jackson-annotations jar
commons-io : commons-io jar 2.4

Project Modules

There are no modules declared in this project.

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

Versions

Version
1.0.0-RELEASE
0.9.0-RELEASE