I figured since the Ansible roles post got so many views that I would write for those who are just getting into this product. I wanted to go over some basic commands and some error messages that I encountered while learning how this software worked, And hopefully help you over come any issues. Again if you have problems shoot me an email (my email is on the About page) and I would be happy to help when I can.
Firstly lets start with some real basic stuff. If you have been on over to their website and watched their little training modules you know that there are a few different basic ways to use this product:
A. Raw module (using the ansible command)
I will start by explaining the Raw module and although its cumbersome why you may want/need to use this in the beginning.. Its basic functionality from what I can see is to just pass off shell commands to the nodes(hosts) using ssh (or shared keys if you have those setup). Now some of you may be asking “Well wait where does it get the user and password from?” … The answer to this is, it will use the user you are logged into (root in my case) and the password you have in your Ansible host file (the one in your working directory.)… What I did in my host file for a password is I parented all my linux servers and defined a password for them all.
This allowed ansible to use my password foobar while running any ansible function whether it was a playbook/role/or just using the ansible command. Now one of the reasons I use this module often is because of error messages like this:
In a massive environment like the one I work in you may run into this due to old outdated operating systems (I.E. RHEL4/5) that don’t have the required python modules installed on it. So to fix an error message like that you would use the raw module like this:
Assuming I had added a list of hosts that were failing to my hosts file in my working directory (/etc/ansible/ for me) and labeled them [broken-hosts]
Another error message that confused me to no end at first was :
If you encounter this go ahead and check the dns name of the host you threw in your host file. Typically from what I have found I made a typo in the host file or someone shut the machine off.
So again… Why would you use the raw command?
1. Resolve error messages related to dependencies
2. Running simple commands you DON’T want a playbook for i.e. “shutdown -h now” (mistakes were made xD)
The command syntax (and there are more than just the raw module) looks like this:
Playbooks are a great way to automate simple tasks… Using the YAML wrapper they just make life easy if you have a simple task you need to take care of.. l personally use roles which uses the same command to execute its just broken down and a little more modular but for the sake of explanation I will show you how to setup playbooks and use them properly…. The first thing you want to do is make sure you have a nice and tidy place to organize any files your playbook may need in my example we will be use /ds1/ my datastore directory inside of this directory I would put two more directories /ds1/scripts/, /ds1/software/
In my example playbook we will be copying a script to oracle production servers and adding it to chkconfig (autostart for everyone who doesn’t know)
You can probably see now why I said it was a nice YAML wrapper earlier … Everything is laid out nice and cleanly and its all labeled. One thing I will tell you on this is to pay attention very closely to white space because your script wont run and it will give you error messages referencing space issues like this:
In the above error message you can see that I made a mistake by accidentally indenting the “- name:” portion of my playbook and ansible points that out to me… Alignment is key here. Lets go back and just take a look at how we have it laid out.. Starting out you see that I have a “- hosts:” section this will be where you define a group of hosts within you ansible host file.
The remote_user section is obvious for most… But there were some instances where we could not use root to ssh (PermitRootlogin=no in the sshd_config).. Which AGAIN ansible has an answer to… There are two ways to go about this.. You could do it on the command line in combination with your ansible-playbook command OR simply add it to your playbook and have it ask you for credentials like so:
And it will prompt you for your password. Which gets you around that issue.
Now we will go into the tasks section… There is a lot you can do here but the basic structure of a playbook is to tell it what servers it needs to run on , who to run it as, and tasks which consist of, a name that will be echoed in the execution of the playbook and then tell it what to do.. In my example above you see I’m telling it to copy: and then give it a source and destination. In other tasks I’m simply executing shell commands to complete my tasks list…. All good stuff right ? But how do you execute the playbook? Here is the basic command structure:
Note that there ARE extra switches you can use… The most common one that I find myself using is the –forks because I want lots of processes running in parallel . By default this is set to 5 and the most I have ever attempted was 50 due to resource constraints but it made for a fairly quick run of a script on 1000 servers. or –retry @/root/playbook.retry (ansible keeps track of the hosts that the playbook failed on) so I can rerun my playbook on ONLY the hosts that failed later after I address their issues.
Please refer to my previous post here Ansible Roles Explained in Practice