Ansible
This is about ansible standalone, you could also run this via vagrant (provision), see vagrant doc for that. This can also be done with python itself. That actually gives you much more control but of course less pre-cooked installation tasks. See the lang repository for a complete install script using python only.
installation
| install | |
|---|---|
osx installation
Since there are plenty of problems occurring in various versions it would be easy to switch between versions rather quickly. Therefore installation with pip looks like the easiest way. Also install yolk to see which versions are available:
if you get this error while running yolk :
| error | |
|---|---|
It seems to be a bug, upgrading yolk might help :
| upgrade | |
|---|---|
The last command will show something like :
| list versions | |
|---|---|
Sadly at the time of writing yolk crashed on execution, but you could get a similar list if you invoke pip with a non-existent version of the package you want :
So take you pick.. AND skip 2.2.0.0 , it does not work correctly.
| install with pip | |
|---|---|
yaml
The base language used : see http://www.yaml.org/spec/1.2/spec.html
The yaml syntax is quite simple, here is a quick cheatsheet found here http://cheat.readthedocs.io/en/latest/yaml.html
A value:
| value | |
|---|---|
value named "foo":
| value | |
|---|---|
List:
A list named "bar":
Alternate list syntax ("Flow style"):
| array form list | |
|---|---|
Dictionary:
A dictionary named "joe":
Dictionary in flow style:
| flow style dictionary | |
|---|---|
A list of dictionaries:
inventory
Also known as hosts file, it is advertised to be /etc/ansible/hosts, but on my mac it just can't find that, so use the -i option. It contains the ip's of all hosts you added with ssh-copy-id above.
| inventory | |
|---|---|
You can add a line to the ansible.cfg file to run without -i hosts. I added some more useful directives as well :
| ansible.cfg | |
|---|---|
debugging
Some tricks to use when debugging ansible scripts.
dumping the Facts
A very neat one, also to see if you might want to use some variables there:
| host info | |
|---|---|
Or any host instead of all, it will dump the complete Facts db so better redirect it into some file.
debug messages
These can be used throughout the playbooks and roles, simply to display a message or dump a variable, this next example gets the result of a shell command and displays it :
| debugging | |
|---|---|
output (when resolv.conf is empty)
You could also shorten it to :
| debug | |
|---|---|
keeping the scripts on the target
To keep the command files that ansible creates on the remote host, set this environment variable:
| inspect scripts | |
|---|---|
The files will be places in /root/.ansible/tmp, and you can view what is attempted to execute. Also use -vvvv option of ansible-playbook to see which files are created.
speed up development playbooks
Mostly the scripts at RINIS require double login for each run: one for the user and one for the vault. At least one of these can be skipped when developing. We use vagrant to create the base VM's so we can use the insecure_private_key it uses by setting insert_key off :
| use insecure key | |
|---|---|
If this setting is true (the default) vagrant will try to replace the insecure_private_key with a less insecure one and use that for login. That's exactly what we don't want, so set it to false.
The key is normally stored in your home directory : ~/.vagrant.d/insecure_private_key To use it ansible use this section in you playbook :
This will cause ansible to login as vagrant and after that become root (don't forget become or you will just stay 'vagrant' !)
Now you could run with the --private-key option :
| provide private key | |
|---|---|
But that's actually more typing than the password, so place it in ansible.cfg :
| ansible.cfg | |
|---|---|
Of course editing the Playbook and replacing {{target}} with development will leave you with :
| passwordless call | |
|---|---|
To also have the vault password filled in for you, place it in a plain-text file and then add that filename to ansible.cfg
| ansible.cfg | |
|---|---|
detecting system
Sometimes you want to know what system you are running on, for instance detecting if this is a VM or a real machine. You can use this command to get some info about your system.
| sys_vendor | |
|---|---|
On my workstation this says :
Note
Gigabyte Technology Co., Ltd.
On a Virtualbox VM it will say :
Note
innotek GmbH
Innotek was probably the creator of VirtualBox since it was acquired in 2008 by SUN Microsystems which in turn was acquired by Oracle in 2010. To test these values with ansible you need the variable "ansible_system_vendor" for instance look for it in the output of :
| host info | |
|---|---|
Shell vs. Command
A typical example are the Ansible modules Shell and Command. In the most use cases both modules lead to the same goal. Here are the main differences between these modules.
- With the Command module the command will be executed without being proceeded through a shell. As a consequence some variables like $HOME are not available. And also stream operations like <, >, | and & will not work.
- The Shell module runs a command through a shell, by default /bin/sh. This can be changed with the option executable. Piping and redirection are here therefor available.
- The command module is more secure, because it will not be affected by the user's environment.
Important
Command is more secure.. Shell will work more often. I would say start with command, and revert to shell when it does not work.