BreakingExpress

How to handle your workstation configuration with Ansible

Configuration administration is an important side of each server administration and DevOps. The “infrastructure as code” methodology makes it simple to deploy servers in numerous configurations and dynamically scale a corporation’s assets to maintain up with consumer calls for. But much less consideration is paid to particular person directors who wish to automate the setup of their very own laptops and desktops (workstations).

In this sequence, I am going to present you the way to automate your workstation setup through Ansible, which is able to assist you to simply restore your complete configuration if you’d like or have to reload your machine. In addition, in case you have a number of workstations, you should utilize this identical method to make the configuration an identical on every. In this primary article, we’ll arrange fundamental configuration administration for our private or work computer systems and set the inspiration for the remainder of the sequence. By the top of this text, you will have a working setup to learn from straight away. Each article will automate extra issues and develop in complexity.

Why Ansible?

Many configuration administration options can be found, together with Salt Stack, Chef, and Puppet. I choose Ansible as a result of it is lighter when it comes to useful resource utilization, its syntax is simpler to learn, and when harnessed correctly it could possibly revolutionize your configuration administration. Ansible’s light-weight nature is very related to the subject at hand, as a result of we could not wish to run a whole server simply to automate the setup of our laptops and desktops. Ideally, we wish one thing quick; one thing we are able to use to rise up and operating rapidly ought to we have to restore our workstations or synchronize our configuration between a number of machines. My particular technique for Ansible (which I am going to display on this article) is ideal for this—there is no server to keep up. You simply obtain your configuration and run it.

My method

Typically, Ansible is run from a central server. It makes use of a list file, which is a textual content file that comprises a listing of all of the hosts and their IP addresses or domains we wish Ansible to handle. This is nice for static environments, however it’s not supreme for workstations. The cause being we actually do not know what the standing of our workstations can be at anybody second. Perhaps I powered down my desktop or my laptop computer could also be suspended and stowed in my bag. In both case, the Ansible server would complain, as it could possibly’t attain my machines if they’re offline. We want one thing that is extra of an on-demand method, and the best way we’ll accomplish that’s by using ansible-pull. The ansible-pull command, which is a part of Ansible, permits you to obtain your configuration from a Git repository and apply it instantly. You will not want to keep up a server or a list checklist; you merely run the ansible-pull command, feed it a Git repository URL, and it’ll do the remaining for you.

Getting began

First, set up Ansible on the pc you need it to handle. One downside is that plenty of distributions ship with an older model. I can let you know from expertise you will positively need the most recent model out there. New options are launched into Ansible fairly continuously, and for those who’re operating an older model, instance syntax you discover on-line will not be useful as a result of it is utilizing options that are not applied within the model you’ve gotten put in. Even level releases have fairly a couple of new options. One instance of that is the dconf module, which is new to Ansible as of two.four. If you attempt to make the most of syntax that makes use of this module, except you’ve gotten 2.four or newer it’s going to fail. In Ubuntu and its derivatives, we are able to simply set up the most recent model of Ansible with the official private bundle archive (PPA). The following instructions will do the trick:

sudo apt-get set up software-properties-common
sudo apt-add-repository ppa:ansible/ansible
sudo apt-get replace
sudo apt-get set up ansible

If you are not utilizing Ubuntu, consult Ansible’s documentation on the way to get hold of it in your platform.

Next, we’ll want a Git repository to carry our configuration. The best strategy to fulfill this requirement is to create an empty repository on GitHub, or you may make the most of your personal Git server in case you have one. To maintain issues easy, I am going to assume you are utilizing GitHub, so regulate the instructions for those who’re utilizing one thing else. Create a repository in GitHub; you will find yourself with a repository URL that can be just like this:

git@github.com:<your_user_name>/ansible.git

Clone that repository to your native working listing (ignore any message that complains that the repository is empty):

git clone git@github.com:<your_user_name>/ansible.git

Now we’ve got an empty repository we are able to work with. Change your working listing to be contained in the repository (cd ./ansible for instance) and create a file named native.yml in your favourite textual content editor. Place the next configuration in that file:

- hosts: localhost
  grow to be: true
  duties:
  - title: Install htop
    apt: title=htop

The file you simply created is called a playbook, and the instruction to put in htop (a bundle I arbitrarily picked to serve for instance) is called a play. The playbook itself is a file within the YAML format, which is a straightforward to learn markup language. A full walkthrough of YAML is past the scope of this text, however you need not have an skilled understanding of it to be proficient with Ansible. The configuration is simple to learn; by merely this file, you may simply glean that we’re putting in the htop bundle. Pay particular consideration to the apt module on the final line, which is able to solely work on Debian-based techniques. You can change this to yum as a substitute of apt for those who’re utilizing a Red Hat platform or change it to dnf for those who’re utilizing Fedora. The title line merely provides data relating to our job and can be proven within the output. Therefore, you will wish to be sure the title is descriptive so it is simple to seek out if you’ll want to troubleshoot a number of performs.

Next, let’s commit our new file to our repository:

git add native.yml
git commit -m "initial commit"
git push origin grasp

Now our new playbook ought to be current in our repository on GitHub. We can apply the playbook we created with the next command:

sudo ansible-pull -U https://github.com/<your_user_name>/ansible.git

If executed correctly, the htop bundle ought to be put in in your system. You would possibly’ve seen some warnings close to the start that complain concerning the lack of a list file. This is ok, as we’re not utilizing a list file (nor do we have to for this use). At the top of the output, it provides you with an summary of what it did. If htop was put in correctly, you need to see modified=1 on the final line of the output.

How did this work? The ansible-pull command makes use of the -U possibility, which expects a repository URL. I gave it the https model of the repository URL for safety functions as a result of I do not need any hosts to have write entry again to the repository (https is read-only by default). The native.yml playbook title is assumed, so we did not want to supply a filename for the playbook—it’s going to robotically run a playbook named native.yml if it finds it within the repository’s root. Next, we used sudo in entrance of the command since we’re modifying the system.

Let’s go forward and add extra packages to our playbook. I am going to add two extra packages in order that it seems like this:

- hosts: localhost
  grow to be: true
  duties:
  - title: Install htop
    apt: title=htop

  - title: Install mc
    apt: title=mc
   
  - title: Install tmux
    apt: title=tmux

I added extra performs (duties) for putting in two different packages, mc and tmux. It does not matter what packages you select to have this playbook set up; I simply picked these arbitrarily. You ought to set up whichever packages you need all of your techniques to have. The solely caveat is that it’s important to know that the packages exist within the repository in your distribution forward of time.

Before we commit and apply this up to date playbook, we should always clear it up. It will work wonderful as it’s, however (to be sincere) it seems type of messy. Let’s attempt putting in all three packages in only one play. Replace the contents of your native.yml with this:

- hosts: localhost
  grow to be: true
  duties:
  - title: Install packages
    apt: title=merchandise
    with_items:
      - htop
      - mc
      - tmux

Now that seems cleaner and extra environment friendly. We used with_items to consolidate our bundle checklist into one play. If we wish to add extra packages, we merely add one other line with a hyphen and a bundle title. Consider with_items to be just like a for loop. Every bundle we checklist can be put in.

Commit our new modifications again to the repository:

git add native.yml
git commit -m "added additional packages, cleaned up formatting"
git push origin grasp

Now we are able to run our playbook to learn from the brand new configuration:

sudo ansible-pull -U https://github.com/<your_user_name>/ansible.git

Admittedly, this instance does not do a lot but; all it does is set up a couple of packages. You might’ve put in these packages a lot quicker simply utilizing your bundle supervisor. However, as this sequence continues, these examples will grow to be extra complicated and we’ll automate extra issues. By the top, the Ansible configuration you will create will automate increasingly duties. For instance, the one I exploit automates the set up of a whole lot of packages, units up cron jobs, handles desktop configuration, and extra.

From what we have achieved to date, you may in all probability already see the massive image. All we needed to do was create a repository, put a playbook in that repository, then make the most of the ansible-pull command to tug down that repository and apply it to our machine. We did not have to arrange a server. In the long run, if we wish to change our config, we are able to pull down the repo, replace it, then push it again to our repository and apply it. If we’re establishing a brand new machine, we solely want to put in Ansible and apply the configuration.

In the subsequent article, we’ll automate this even additional through cron and some extra gadgets. In the meantime, I’ve copied the code for this text into my GitHub repository so you may test your syntax towards mine. I am going to replace the code as we go alongside.

Exit mobile version