Introduction to Ansible Part 3

Hi there! Its been a long time since i posted continuation series on Ansible . if you have not read part one and two  it can be found here .

Now lets quickly define our objectives.

  1. Remotely manage ( Start/Stop ) the JBoss server with the help of a custom SystemD initialization script which is in turn called using Ansible remotely.
  2. Deploy a war file on JBoss application server which is placed at a remote machine.
  3. Undeploy a war file on JBoss server remotely.

As explained in our previous article, we can define static variables and defaults inside roles/webserver/defaults/main.yml file. So lets define our defaults variables. place these in the specified file(on ansible server machine) :

name : test

Username: DBuser

Password: DBPass

driver : <drivername>

connection_url : <connection_url>

pool_name: <pool_name>

JNDI_HOME: <jndi_home>

filter : “<user-name>{ { Username} }</user-name>”

deployname: warfile.war

deploy_path: /path/to/deployment/scanner/path

DeploySrcTemp: /remotepath/to/deployment

Now lets create a systemd service to control Jboss service. let us name it as   JbossControl.service with the following content.for tutorial on systemD refer my previous article :

 

[unit]

Description= Jboss Application server control script

After = network.target

[Service]

Type=idle

ExecStart=/path/to/jboss/bin/standalone.sh

ExecStop=/path/to/jboss/bin/jboss-cli.sh –connect command=:shutdown

TimeoutStartSec=400

TimeoutStopSec=400

[install]

WantedBy=multi-user.target

save the file on remote machine and move it to roles/webserver/templates/ JbossControl.service.

Now we have required resources which are required for managing a Jboss app server remotely.

Now on our ansible server machine, we can update our roles/webserver/tasks/main.yml file with following content.

 —

 – name: copy jboss service file to systemd folder

   copy:

     src : “/path/to/ansible/roles/server/templates/JbossControl.service”

    dest: /etc/systemd/system/jbossControl.service

tags: [copyService]

 

 – name: start JBoss service

     systemd:

          state: started

          name: jbossControl

    tags: [StartJboss]

 

 – name: stop JBoss service

     systemd:

          state: stopped

          name: jbossControl

    tags: [StopJboss]

 

As we see in the tasks file, we have  used systemd module of ansible to invoke systemd services and targets . We have also utilized copy module to copy files from one location to another on remote system .now we can create a play.yml in the project root directory. with the following content:

 -hosts : RemoteMachines

  sudo : yes

  roles:

   – webserver 

now we can invoke this playbook to start jboss/stop jboss on remote machine as follows :

to start Jboss application server on remote machines defined in RemoteMachines file :

> ansible-playbook play.yml –tags “copyService,StartJboss”

to stop Jboss application server on remote machines defined in RemoteMachines file :

> ansible-playbook play.yml –tags “StopJboss”

here , tags attribute while calling the playbook helps us to specify which services to run from the role. if we do not specify the tags, every task on the specified role runs and returns the result.

We have successfully completed the first objective of managing the remote JBoss application server! In the next tutorial we will see how to deploy and un deploy the war file remotely which also in turn involves managing data sources and config files. Stay Tuned !