How to use your workstation configuration management Ansible

In the first article of this series, learn about the basics of managing laptops and desktop configurations.

Configuration management is a very important aspect of server management and DevOps. "Infrastructure as Code infrastructure as code" method can easily deploy servers in various configurations, and dynamically expand the organization's resources in order to meet customer needs. However, for the less you want to automatically set up your laptop and desktop (workstation) administrator of personal attention.

In this series, I will show you how to Ansible automate your workstation setup, if you want or need to reinstall your machine, it lets you easily restore the entire configuration. Also, if you have multiple workstations, you can use the same method in the same configuration on each workstation. In the first article, we will set up a personal or work computer basic configuration management, and lay the foundation for the rest of the family-oriented. By the end of this article, you will therefore get a working environment. After each article of this series will automate more and increase the complexity.

Why Ansible?

There are many configuration management solutions, including Salt Stack, Chef and Puppet. I prefer Ansible, because it is more lightweight in terms of resource use, syntax easier to read, and if used correctly it can completely change your configuration management. Ansible lightweight nature of particular relevance to this topic, because we may not want to run an entire server just for automated setup our laptops and desktops. Generally, we always want faster; something we can use to quickly get up and running, we need to restore order at the workstation or our configuration synchronization between multiple machines. I use a specific method of Ansible (I will demonstrate in this article) is very suitable for this, without the need to maintain the server. You simply download the configuration and run it.

My approach

Usually, Ansible running on a central server. It uses an inventory inventory file, the file is a text file that contains a list of all we want to host their IP address or domain name of Ansible management. This is useful for static environment, but not ideal for workstations. The reason is that we do not really know our workstation at a time of state. Maybe I closed the desktop, or notebook computer may be suspended and placed my bag. In either case, Ansible server will complain because if they are offline, Ansible will not be able to contact my machine. We need more of on-demand way, we have to achieve this goal through the use of ansible-pull. ansible-pull Ansible command is a command that allows you to configure Git repository to download from and apply it immediately. You do not need to maintain servers or inventory; you just run ansible-pull command, give it a Git repository URL, it will do the rest for you.

Started

  1. sudoapt-get install software-properties-common
  2. sudo apt-add-repository ppa:ansible/ansible
  3. sudoapt-get update
  4. sudoapt-get install ansible

If you do not use Ubuntu, please refer Ansible documentation to know how to get it for your platform.

Next, we need a Git repository to store our configuration. The easiest way to meet this requirement is to create an empty warehouse on GitHub, or if available, can also use your own Git server. For simplicity, I assume you are using GitHub, so if you are using another warehouse, adjust the command accordingly. Create a repository on GitHub in; you'll end up with a warehouse similar to this URL:

  1. [email protected]:<your_user_name>/ansible.git

The warehouse was cloned into your local working directory (ignoring any complaints warehouse is empty message):

  1. [email protected]:<your_user_name>/ansible.git

Now that we have a can use empty warehouse. Your working directory to the repository (eg cd ./ansible), and create a file named local.yml in your favorite text editor. The following configuration on the file:

  1. - hosts: localhost
  2.   become:true
  3.   tasks:
  4.   - name:Installhtop
  5.     apt: name=htop

Next, let us submit new documents to our warehouse:

  1. git add local.yml
  2. git commit -m "initial commit"
  3. git push origin master

Now our new script should appear on our warehouse in GitHub. We can use the following command to apply the script we created:

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

If executed correctly, htop package should be installed on your system. You may see some warning near the beginning, complaining about the lack of inventory file. This is good because we did not use the inventory file (and we need not do so). At the end of the output, it will do an overview of its contents. If htop installed correctly, you should see the last line of the output changed = 1.

How does it work? ansible-pull command uses the -U option, which requires a repository URL. For security reasons, I give it the URL https version of the warehouse, because I do not want any hosts have write access to the repository (default https is read-only). local.yml is the default script name, so we do not need to provide a file name for the script: If it finds a script called local.yml in the root directory of the warehouse, it will automatically run it. Next, we use the sudo command at the front, because we are modifying the system.

Let us continue to add more packages to our script. I will add two packages, to make it look like this:

  1. - hosts: localhost
  2.   become:true
  3.   tasks:
  4.   - name:Installhtop
  5.     apt: name=htop
  6.   - name:Install mc
  7.     apt: name=mc
  8.    
  9.   - name:Installtmux
  10.     apt: name=tmux

I added more action (task) to install two additional packages, mc and tmux. Choose to install this script in which packages are not important; I just have to pick them. You should install all the systems you want to have a package. The only caveat is that before you distribute, you must know that the package exists in the depot.

Before we submit and apply this update script, we should tidy it. It can work very well, but (be honest) It looks a bit confusing. Let us try to install all three packages in one action. Replace this with the following contents of your local.yml:

  1. - hosts: localhost
  2.   become:true
  3.   tasks:
  4.   - name:Install packages
  5.     apt: name={{item}}
  6.     with_items:
  7.       -htop
  8.       - mc
  9.       -tmux

Now it looks cleaner and more efficient. We use with_items our list of packages into one action. If we want to add another package, we simply add another line with a hyphen and the package name. With_items can be seen as similar to the for loop. We list each package will be installed.

Our new changes will be submitted back to the warehouse:

  1. git add local.yml
  2. git commit -m "added additional packages, cleaned up formatting"
  3. git push origin master

Now we can run our script to accept the new the new configuration:

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

Admittedly, this example there were not many things to do; it does is install some packages. You can use the package manager to install these packages more quickly. However, as this series continues, these examples will become more complex, we will automate more things. Finally, Ansible configuration you create will automatically perform more and more tasks. For example, I use that configuration can be automatically installed hundreds of packages, set up a cron job, handling desktop configurations, and so on.

From what we have achieved so far point of view, you may already have a general idea. We have to do is create a warehouse, place a script in the repository, then use the ansible-pull command to pull in the warehouse and apply it to our machines. We do not need to set up the server. In the future, if we want to change the configuration, we can pull the warehouse, update it, and then push it back to our warehouse and apply it. If we want to set up a new machine, we only need to install Ansible and application configuration.

In the next article, we will further automation through cron and other projects. At the same time, I have to copy the code in this article to my GitHub repository so that you can compare your grammar me. As we progress, I will continue to update the code.

via: https://opensource.com/article/18/3/manage-workstation-ansible

Guess you like

Origin www.linuxidc.com/Linux/2019-07/159436.htm