Ansible Deployment Of Splunk

Automating An AWS Splunk Install With Ansible

Vince Sesto
Splunk User Developer Administrator
6 min readJan 8, 2019

--

I know, we’ve been very quiet lately, but we’ve been working hard to get our third book released and out to the wild. But we’re back now and you should be hearing a lot more from us in the coming weeks and months. We thought we would get back into it by showing you how simple it is to get a Splunk server running on AWS using Ansible.

This is a small snapshot of work from our latest book, Practical Ansible, where the main project is a Splunk Server installed onto an Amazon EC2 instance using Ansible.

This post does get a little advanced, so if you are new to Ansible, probably go through our earlier posts on Playbooks to get up to speed first:

What You Need To Get Started

  • A Linux host with Ansible installed
  • An AWS account with administrator access to set up EC2 instances, etc
  • Credentials for this AWS account on your Linux host
  • AWSCLI installed on the Linux host
  • A valid Key Pair, added to AWS, so you can SSH to your new server

Get Our Ansible On

1.Start with a new directory to store everything in, with the first file you need to make is you host file. It will simply be called host, and add the following details.

2.Create the new role for our server. By using the ansible-galaxy command, we can save time setting up the skeleton of the role, so run the following command:

If you haven’t used roles before, that’s all good. We are not going to do anything too complex here. In brief roles allow you to package up your tasks in a dedicated structure that Ansible already understands. It also allows you to the reuse and share you roles with others, if you like.

3.As we are creating a specific role, lets clean up a little first, let’s make a dedicated roles directory to hold our new role and move the newly created splunk_server role there:

If you run the tree command over the directory, you will see the folder structure already set up for you.

This is not a post of roles, but for now just keep in mind that the structure above allows us to seperate our code and place everything in a neat order compared to all our tasks in one playbook.

4.We can start by creating our tasks, that need to be run. This will be in the roles/splunk_server/tasks/main.yml file. So let’s start by opening that up and adding in the security group details for our new instance:

If you have worked with AWS before, you will see the above code is simply adding in access to ports 22 for SSH, 443 for https access and 8000 for the Splunk web port.

5.We can now work further on our tasks file, now add the following details to launch the EC2 instance:

We first refer to the security group we created in line 27, then the values in lines 28, 29, 31 and 32 are variables we will be setting up shortly. They are all properties we need to specify to set up our instance. Line 34 then lets AWS know to use the user_data file we will create shortly to install our Splunk server.

6.The tasks that we have performed so far will take a little while, so we will now add in a wait, to allow everything to install and make sure we can SSH to the host:

7.Finally the following tasks give the EC2 instance a tag so we can identify it, when it is set up:

This is the end of all our tasks, so make sure you save the file before closing.

8.We can now create the user_data file we referred to in our tasks. Start by creating the following file:

9.Open the file with your text editor and add in the following 6 lines.

It’s basically a script that will download the version of Splunk you need(line 3), install if using RPM(line 4), then at the end it will start Splunk accepting user licenses and creating your admin password as newpassword. Feel free to change anything in this, for example Splunk versions, if you prefer a Deb package and if you want to use a more complex password.

10.We can now populate our variables file. So open the roles/splunk_server/vars/main.yml file with your text editor and add in the following details:

This is all pretty self explanatory, but our tasks need to have variables listed, all the region’s, instance type, and image type will work with the code I have provided in this post. Make sure, you have you key pair added in line 6.

10.We can now create the playbook to set up our new server. Start by creating our playbook in the base directory:

11.Now open the playbook and add in the following details to run our newly created role:

12.It’s now time to run the playbook. From the command line, you can now use the ansible-playbook command to run the new role, you have created:

Maybe go get a coffee, as Splunk does take a little while to install and start up, but if all goes well, in a few moments you should have a new server installed, running on an EC2 instance on your AWS account. You will now be able to use the Public IP address provided by AWS to then access the web interface. Simply go to your web browser and add in <PublicIP>:8000 and hopefully you will now be able to log in as the admin user with the password you specified in the user_data file.

This has been a pretty quick and dirty deployment but hopefully gives you a good idea of what you can do with Ansible. Remember, if you would like more details on working with Ansible, feel free to check out our new book:

Found this post useful? Kindly tap the clap button below! :)

About The Author

DevOps Engineer, Endurance Athlete and Author. As a DevOps Engineer I specialize in Linux and Open Source Applications. Particularly interested in Search Marketing and Analytic’s, and is currently developing my skills in devops, continuous integration, security, Splunk(UI and Reporting) and development(Java).

--

--

Vince Sesto
Splunk User Developer Administrator

Vincent Sesto is a DevOps Engineer, Endurance Athlete, Coach and Author. One of his passion’s in life is endurance sports as both an athlete, coach and author.