If you’re brand new to Ansible but have some vRealize Automation and Orchestration experience, this post will get you started with a configuration management tool.
The goal in this example is to deploy a CentOS server from vRealize Automation and then have Ansible configure Apache and deploy a web page. It assumes that you have no Ansible server setup, but do have a working vRealize Automation instance. If you need help with setting up vRealize Automation 7 take a look at the guide here.
To get started, we need Ansible setup. So I deployed a CentOS server and performed my necessary environment configurations like IP Addressing, adding SSH, disabling my firewall (uh, yeah its a lab). Next I SSH into the ansible server and run the following two commands.
sudo easy_install pip sudo pip install ansible
To use ansible, we need to create an inventory of the servers that the Ansible server should be going out execute commands on. Create a file called inventory and if you want to run a test, add a DNS name or IP address of a machine to configure Apache on.
Now we need to create a playbook. I’ve named a file playbook1.yml. This file will be the desired configuration state of the machines listed in inventory. The exact code that I used is listed below. Even without knowing anything about Ansible code, you can probably guess that its going to ensure the machine will install httpd via yum and then copy some files of mine from hollowweb directory over to the html directory and lastly start the httpd service.
--- - hosts: all tasks: - name: Install Apache Web Server yum: pkg=httpd state=installed notify: - start httpd # Copy website files - name: Upload Hollow Web Files -index copy: src=hollowweb/index.html dest=/var/www/html - name: Upload Hollow Web Files -image001.png copy: src=hollowweb/image001.png dest=/var/www/html handlers: - name: start httpd service: name=httpd state=started
When you’re done, your directory should have an inventory file, and playbook1.yml file. My example also has a hollowweb directory which is where I’m storing my web server files in my vSphere templates. Your actual web server files could reside in GIT, Artifactory or a file share.
If you want to test out your configuration now, simple run the following command on your Ansible server.
ansible-playbook -i inventory playbook1.yml
The server listed in inventory should install your apache server and run all the commands listed earlier.
NOTE: Ansible is agentless and requires SSH access to the guest machines. This may mean that you need to setup your SSH keys and knownhosts files to allow the communication. We won’t want to have to put in passwords or anything when we start to automate the process.
Setup vRO workflows
Now, all we’re going to do is add a vRO workflow that will add machines to inventory and execute the playbook commands that we did earlier. I added a workflow that formats a command, and then passes that command over to a workflow that executes it on my Ansible server.
Now a second workflow to again, format a script and pass it to the Ansible server to run via SSH. This time the script runs the Ansible playbook. The script I used is below.
Lastly, create a third script (well, that’s what happens when you build modular scripts to be reused). This script will gather the variables passed over from vRA, and then execute the two workflows we build above.
We’ll assume for now that you can publish a new server in the catalog and have setup event subscriptions so vRO workflows can be executed during provisioning. Point the MachineProvisioned Event subscription to the third workflow we built above.
If you want to dive deeper into Ansible and figure out what else you can do with it, please check out the Ansible guides.
I’d love to hear what kind of things you created with your own Ansible instances. Post them in the comments!