The Install
My main source of research that I used was Ansible's own documenation located at their site. It has everything you need from installing to ad-hoc commands to creating playbooks. My testing server's OS was CentOS/RHEL 7 64 bit. The server didn't have direct access to the Internet so installing Ansible and all of it's dependencies had to be done manually. If you are ever in that situation, here is a list of dependencies that I copied over to the server to get Ansible installed and the order they need to be installed. The version of Ansible I am working with is 2.1.0.0.
- python-devel
- python-setuptools
- python-pycrypto
- yaml (pyyaml pre-req)
- python-pyyaml
- python-paramiko
- python-markupsafe (python-jinja2 pre-req)
- python-babel (python-jinja2 pre-req)
- python-jinja2
- sshpass (Needed if you are going to use SSH username and password with Ansible)
Now that all of the pre-reqs are installed, just run the following command from the Ansible install directory.
[user@linux_prompt]$ python setup.py install
The installer will see that all of the required software is installed and complete the install. You can test that it has been installed by typing in the command: ansible. A list of commands will appear if the install completed successfully.
The Initial Configuration
There are a couple of steps that need to be completed before you can initiate your first Ansible command. You will need to create a directory to store your host inventory file and your config file. The default spot for the Linux OS is /etc/ansible. This folder is not created as a part of the install so you need to create it. Once you create that folder, you will need to create two files, hosts and ansible.cfg, inside of there. Both files are simple text files in .INI formatting.
The hosts file has all of the servers that you want to manage using Ansible. The documentation site I talked about previously has a better explanation on the different variations on writing out the hosts file, but below is an example of a simple hosts file.
[test_servers]
10.10.10.10
10.10.10.20
This example has a server group called test_servers and has two IPs that are apart of that group. So if you input the name test_servers in your Ansible command, the command will run on both of those test servers.
The ansible.cfg file is like any other configuration file. It contains variables that the Ansible installation uses. Below is a simple example of the ansible.cfg file.
[defaults]
inventory = /etc/ansible/hosts
The configuration file is calling out where your hosts file is located so the installation can lookup the managed nodes.
The First Command
Now that the two configuration files have been created, you can run your first ad-hoc command. The "Hello World" example I am going to use is pinging a server group inside of the hosts file. I know it is not an awesome use case, but it will test connectivity among all of the nodes. Below is what you would have to enter if you were going to ping all of the test_servers from the sample hosts file.
[user@linux_prompt]$ ansible test_servers -m ping
Now you will see a response that both of the servers were contacted successfully. If the connection failed, you might need to use a different user to connect to those servers. When you run the command, it will use the same credentials you are signed in with on the Ansible server. If you need to use different credentials, you can run the command this way.
[user@linux_prompt]$ ansible test_servers -m ping -k -u random_username
The command will ask you for the username's password and run the ping command as that user.
The First Playbook
With the first command successful, the real power of this tool is in the playbooks. A playbook is a list of commands that can be ran against your nodes simultaneously. A playbook is in YAML format and they are really easy to create. Below is a sample playbook that replicates the ad-hoc ping command with username above. Just create a file called test-playbook.yml.
---
- hosts: test_servers
remote_user: random_username
tasks:
- name: test connection
ping:
The most important thing about creating these .yml files is spacing and indentation. Make sure to use spaces and not Tabs when indenting new lines. The file is not read correctly with Tabs in there. Also, you need to make sure you have the initial "---" at the beginning of the file. To run the playbook, go to where you saved the playbook file and run the following command.
[user@linux_prompt]$ ansible-playbook test-playbook.yml --ask-pass
You will be prompted for the password of random_username and the ping command will run against the test_servers called out in the hosts file.
Summary
I have had a lot of initial success with Ansible automation. It is easy to setup and configure. Running ad-hoc commands or playbooks is straight forward. I have just scratched the surface of what Ansible can do and the opportunities that it can solve. Hopefully, you will have the same success as I have had.
~RRRII
No comments:
Post a Comment