This page was exported from phaq
[ http://phaq.phunsites.net ] Export date: Sun Feb 9 5:10:19 2025 / +0000 GMT |
I just remember a recent argument I had with someone about automation. It's unbelievable, how many things are still done manually on a widespread scale, not leveraging the possibilities at all. Especially with so many frameworks available to help out, sticking to "the old way" ain't just cool any more. So let's quickly look at Ansible, and how we can be up and running for even simple task automation in 10 minutes or less. While ansible has just recently be acquired by RedHat, it is definitely not a Linux-only tool and can be used as well to manage BSDs or even network devices. One can use Ansible standalone or in combination with Ansible Tower, which just adds more bells and whistles, some APIs and inventory management. But for simple tasks, the standalone will do just fine. All you need is a management station, from where Ansible can take off. On FreeBSD, these packages are recommended to install: pkg install sshpass python python27 ansible py27-paramiko redis py27-redis Enable redis (a caching engine, somewhat similar to memcached): echo 'redis_enable="YES"' >> /etc/rc.conf Add a directory which will hold your ansible scripts, so called playbooks: mkdir -p /var/ansible/playbooks/roles Edit /usr/local/etc/ansible/ansible.cfg and change the settings as shown below. Change the roles_path directory as needed. transport = paramiko Now create a new file at /usr/local/etc/ansible/hosts and add some hosts. You can start off with a very simple host group, where you just add some hosts. [servers] That's it. Now that the hosts are registered in the inventory, we're almost ready to get off the ground. But first, there's one important thing: All hosts must be reachable and have SSH enabled, also, a remote-login using a (preferrably) unprivileged account with sudo-privileges must exist. For this example, a remote account called sulogin is assumed. Another important thing is, that Python2.7 must be installed on the target host as well. While a RedHat-based system typically has Python installed, FreeBSD has not. Luckily Ansible allows to also send raw commands to a target host, this way it can be used to bootstrap itself like so: ansible servers -l fbsdhost1.localdomain.local --user=sulogin -k --ask-become-pass -b -m raw -a "pkg install -y -f python27 python" The command above will select the servers host group, but limit itself to given hostname. Additionally, it'll ask for the remote user's password, and the sudo password as well. The raw module is selected, scheduled to run the given pkg command. The result of which looks somewhat like this: As soon as Python is installed, you may us the ping module to see if everything is fine.
This will instruct Ansible to run a simple check on each target host, which result in something like this:
Now, let's put this to some good use. The key to leverage Ansible is to use so-called playbooks, which implement the actual automation logic. But first, some directories need to be created on the management station.
Now, let's look at a simple use-case: We want Ansible to take care for installing some ssh keys, disable remote root login, add an ssh banner, and make sure that the SSH service is actually restarted if a change occured. In an ordinary world, this means creating some directories, copying files to their destination, set some permissions and change config files. So, let's start by adding two ssh public key files to the new /var/ansible/playbooks/common/files/ssh/ directory: Also create a new ssh banner file:
The directory should now look somewhat like this:
Now go to /var/ansible/playbooks/common/tasks and create a new file called sshdsetup.yml with this contents:
The file, a structured YAML file, is actually easy to read, and implements just the steps as outlined before. Each section implements a single step. The first loops through the key files provided, and registers them with the ~/.ssh/authorized_keys file of user sulogin. The command will make sure that the directory is create if missing, and proper permissions are assigned. Also, the key will be registered just once. The second step just copies the banner file to the new location. Ownership and permissions are provided, so no guesswork must be made. Next is to actually enable the banner. The replace command knows how to apply dynamic rewriting using regurlar expressions to a text file, including creating a backup copy. The register keyword registers a variable if (and only if) the file has been changed. The root lookdown will also do some on-the-fly changing of the sshd_config file. It will effectively disable RootLogin, which is enabled on RedHat/CentOS flavors. The when condition clearly indicates, that this steps is to be carried on a host running CentOS (RedHat users should use the appropriate distro name here, of course). Again, a variable is registered if the change has been carried out. Last but not least, the sshd daemon is restarted if one of the previously mentioned variables exists. Such a playbook is run like this:
As you can see, it's dead simple to to write a playbook which does just plain simple automation, and works fine cross-platform in different *nix flavours. And the best: You don't need to bother too much about the scripting logic. Proof: Up and running in 10 minutes. Now head out and look for more in the Ansible documentation. |
Powered by [ Universal Post Manager ] plugin. HTML saving format developed by gVectors Team www.gVectors.com |