Friday, May 6, 2016

Automate All the Things With Ansible: Part Two

Overview

This is part two of a two-part tutorial on Ansible. Part one is here. In this part, you will learn about roles (Ansible's building blocks), variables, loops, how to use roles in playbooks, and how to organize roles into a directory structure.

Roles

When you manage tens, hundreds or more servers, probably many of them need to be configured similarly. Different groups of servers like web servers or database servers will require their own special configuration, but also may share some other common functionality. It is of course possible to just copy tasks around, but this gets old really fast when dealing with a complicated infrastructure.

Ansible roles are the ticket. Playbooks can include roles. Roles can depend on other roles, and Ansible best practices recommend grouping hosts in your inventory file based on their roles. Roles are the backbone of serious Ansible-managed infrastructure. As usual, I'll start with an example and introduce many of the capabilities of roles through the example.

I like aliases and shell functions a lot because I can't remember all the arcane switches and options for each command, and also because it saves a lot of typing. I also like to have some tools like htop and tmux on every server I log in to.

Here is a file that contains some of my favorite aliases and functions. I'll call it '.gigirc'. By the way, if you ever wondered what the 'rc' suffix stands for in all those rc files, then it stands for 'Run Commands'.

Let's define a role called 'common' that creates a user called 'gigi', adds a public ssh key, copies the '.gigirc' file and adds a line at the end of '~/.bashrc' that runs this file and finally installs the common packages vim, htop and tmux (defined in the 'vars/main.yml file'). 

I will introduce a lot of new stuff here: four different modules, variables, and loops. Also, roles are typically spread across multiple files in a standard directory structure. I'll show you a couple of files and then explain about the directory structure. Here is the 'tasks/main.yml' file:

And here is the vars/main.yml file that contains the definition of the 'COMMON_PACKAGES' variable used to specify which common packages to install.

Modules

The user module can manage user accounts. Here I use it to create the user 'gigi'.

The authorized_key module is for adding/removing SSH authorized keys. Here I use it to add my public key for the 'gigi' user.

The lineinfile module can be used to replace or add single lines to a file. In this case, I use it to source the '.gigirc file' from '.bashrc', so all the cool aliases and functions in '.gigirc' are always available in any interactive session.

Finally, the apt module has tons of options for managing apt packages. Here I just install some common packages.

Variables

The COMMON_PACKAGES you see in the last task for installing common packages is a variable. Ansible lets you use variables defined almost anywhere: playbooks, inventory, roles, dedicated files, and even environment variables. There is a lot more information about variables in the documentation.

Loops

Ansible is declarative, so it doesn't support explicit loops. But there is a plethora of with_xxx that allows you to perform repeated operations on some structure like a list of users, packages. or lines in a file. You can also repeat operations until some condition is true or get the index of the current item. Additional information can be found in the documentation

Role Directory Structure

Here is what a typical role directory structure may look like:

common

├── handlers

│   └── main.yml

├── meta

│   └── main.yml

├── tasks

│   └── main.yml

├── templates

└── vars

    ├── Debian.yml

    ├── Ubuntu.yml

    └── main.yml

The 'tasks/main.yml' file is the where all the tasks are defined. Each task corresponds to an Ansible command that typically uses a module.

The 'meta/main.yml' file will contain a list of other roles that the current role depends on. Those roles' tasks will be executed before the current role, so it can be sure all its prerequisites are met.

The 'handlers/main.yml' file is where you keep your handlers, like the handler you saw earlier that starts Nginx after installation.

The templates directory is where you keep Jinja2 templates of configuration and other files that you want to populate and copy to the target system.

The vars directory contains various variables and can conditionally contain different values for different operating systems (very common use case).

It's important to note that Ansible is very flexible and you can put anything almost anywhere. This is just one possible structure that makes sense to me. If you look at other people's directory structures, you may see something completely different. That's totally fine. Don't be alarmed. Ansible is not prescriptive, although it does provide guidance for best practices.

Using Roles

Roles do the heavy lifting, but playbooks are how you actually do work. The playbooks marry the inventory and the roles and specify what roles to play on which host. Here is what a playbook with roles looks like:

Running the playbook produces the following output:

Conclusion

Ansible is a great tool. It is lightweight. It can be used interactively with ad-hoc commands, and it scales very well to massive systems. It also has a lot of momentum and a great community. If you manage or even just work with remote servers, you want Ansible.


by Gigi Sayfan via Envato Tuts+ Code

Automate All the Things With Ansible: Part One

Overview

This is part one of a two-part tutorial on Ansible. In this part you will learn what Ansible is, how to install and configure it, and how to install a local Vagrant cluster to test it. Then, you'll discover the inventory, modules, ad-hoc commands, playbooks, run strategies, blocks and the vault.

What Is Ansible?

Ansible is a configuration management and orchestration tool. It operates in the same domain as Puppet, Chef, and Saltstack. This means that with Ansible you can remotely provision a whole fleet of remote servers, install and deploy software on them, and track them remotely. 

Ansible is an open-source project implemented in Python and has a pluggable architecture with modules that can manage pretty much any operating system, cloud environment, and system administration tool or framework. You can also pretty easily extend it with your own plugins if you want to do something special.

One of the unique features of Ansible is that it doesn't install any software on managed machines. It manages the machines remotely over SSH. To manage a remote machine, you just need to make sure that your public SSH key is in the authorized_keys file of that machine.

Getting Started With Ansible

Ansible runs on a control machine and can manage servers running any operating system, but the control machine can't be a Windows machine at the moment. I'll use Mac OS X in this tutorial as the control machine.

Installation

Ansible requires Python 2.6 or 2.7. To install it, type:

pip install ansible

On Mac OS X, it is recommended to increase the number of file handles:

sudo launchctl limit maxfiles 1024 unlimited

If you see an error like "Too many open files" you probably need to do that.

To verify Ansible was installed properly, type ansible --version. You should see:

The version number may be different, of course.

The Ansible Configuration File

Ansible has a configuration file that lets you control many options. The search order is:

  • ANSIBLE_CONFIG (an environment variable)
  • ansible.cfg (in the current directory)
  • .ansible.cfg (in the home directory)
  • /etc/ansible/ansible.cfg

You can also override specific settings using individual environment variables, which take precedence over the configuration file.

Check out the Ansible documentation to learn about all the options.

Set Up the Vagrant Cluster

To really understand the power of Ansible, you need a bunch of servers to manage. For the purpose of this tutorial I'll use a Vagrant cluster of 3 VMs, but as far as Ansible is concerned those are just some hosts it needs to manage. To learn more about Vagrant, check out Introduction to Vagrant.

First, install VirtualBox and Vagrant. Then put the following in a file called 'Vagrantfile' in a work directory

Then type vagrant up. Vagrant will create three virtual machines for you, available as larry, curly and moe. To verify, type vagrant status. You should see:

To make sure you can SSH into your cluster hosts, type: vagrant ss >> ~/.ssh/config.

Now you can SSH into any of your virtual servers using their hostname. For example: ssh curly. This will allow Ansible to connect to your cluster hosts over SSH without any issues with usernames, passwords, or keys.

Inventory

Now that we have a cluster, we need to tell Ansible about it. This is done using an inventory file. The inventory file is a list of host names organized in groups using an INI file format. Put the following in a file called 'hosts' in your work directory. 

I put 'larry' in a group called 'funny' and the other hosts in a group called 'funnier'. That organization will allow us to perform actions on these groups. You can also perform actions on individual hosts and on all the hosts.

Modules

Ansible has a very modular and extensible architecture. All its capabilities are organized in modules. There are core modules and extra modules. Each module represents a command, and most take arguments. You can use modules directly in ad-hoc commands or in playbooks. You can read about all modules in the documentation.

Ad-Hoc Commands

It's time to get hands-on. The simplest way to use Ansible is to run ad-hoc commands. Ad-hoc commands use modules. The format of an ad-hoc command is:

ansible <host group> -i <inventory file> -m <module> [-a <argument 1>, ... <argument N>]

For example, to see if all the hosts in your inventory are up, you can use the ping module (without arguments):

Ansible has many modules for all common system administration tasks like file management, user management, and package management, as well as many uncommon tasks. But if you don't find what you need or just feel more comfortable with plain shell commands, you can use the shell module directly including pipes. The following command extracts the internal and external IP addresses of all hosts:

Playbooks

Ad-hoc commands are nice when you want to quickly do something on a bunch of hosts, but the real power of Ansible is in its playbooks. Playbooks are YAML files where you define collections of tasks to accomplish goals like provisioning, configuring, deploying and orchestrating your infrastructure. 

Example Playbook

Let's take a look at what a typical playbook looks like before we get down to the details.

The playbook has a hosts section where you specify hosts from the inventory file. In this case, the group name is "funnier". Then there is a tasks section with two tasks that install Nginx and Python 3. Finally, there is a handlers section where Nginx is started after its installation.

Running Playbooks

You run playbooks with the ansible-playbook command. You still need to provide an inventory file and the playbook you want to run. Save the playbook into a file called "playbook.yml" in your working directory. Let's give it a try:

Oh, no. What happened? Ansible gives a decent error message here: "Failed to lock apt for exclusive operation". Many playbooks will require sudo privileges. This playbook is not an exception. To run the playbook with sudo privileges, you just add the --sudo flag:

Ansible is idempotent, which means if something is already in the desired state then Ansible will leave it alone. In the output of ansible-playbook, you can see which tasks succeeded or failed and which hosts were changed.

Let's run the same playbook again. Nothing is supposed to be changed:

Run Strategies

Prior to Ansible 2.0, plays executed in a linear fashion, task by task. All the target hosts executed the first task. Only when all the hosts were done with the first task could they start on the second task. 

Ansible 2.0 added the concept of run strategies. There are currently two strategies: the "linear" strategy I described above, which is the default strategy, and the "free" strategy where hosts are free to execute the tasks in the playbook still in order, but not in lockstep with other hosts. 

This could be useful if hundreds of hosts need to download several files from some FTP servers. The first host may finish downloading the first file and move on to the next one, while other hosts are still busy downloading the first file. By the time the other hosts get to download the next file, the first host is done already, and there is less contention.

The free strategy seems superior in most situations. You just add a strategy: free key-value pair to the playbook.

Blocks

Another new Ansible 2.0 feature is blocks. Blocks let you group tasks together. This is very useful if you have tasks that need to execute only under a certain condition. Previously, you had to do it for every task separately.

With blocks, you can group all these debug tasks together and put the "when" condition at the block level.

The Vault

Ansible communicates with remote machines over SSH, but the playbooks may contain secrets like username, passwords and API keys. Since you typically store playbooks in source control systems like git, this information will be visible to anyone who has read access. 

Ansible helps with the ansible-vault program that lets you create, edit and rekey encrypted files. These files can be decrypted on the fly when running the playbook by providing a password. If you add the --vault-ask-pass flag to ansible-playbook then it will prompt you for the vault password. 

Alternatively, you may add --vault-password-file <password file> and Ansible will read the password from your file. If you use the password file, don't store it in source control!

Now, you can safely store the encrypted files in source control and not worry about anyone finding your secrets. You need to manage your vault password carefully. If you lose it, you won't be able to decrypt the files in the vault.

Conclusion

Ansible is a great tool. It is lightweight. It can be used interactively with ad-hoc commands, and it scales very well to massive systems. It also has a lot of momentum and a great community. If you manage or even just work with remote servers, you want Ansible.

Stay tuned for part two.


by Gigi Sayfan via Envato Tuts+ Code

Alto

opl-small

'Alto' is a One Page WordPress theme featuring a gorgeous header slideshow with a clear content overlay area below. This mobile-friendly theme is perfect to tell your story, beautifully. Features include a gorgeous responsive fluid design, unlimited slideshow background images, a neat customisable call-to-action button, 23 SVG social network icons and an additional "page template" for legal/impressum text. What's great to know is Alto has such an easy-to-use setup all within the WordPress theme customizer including live editing preview.

It's worth mentioning 'Alto' is included in Currl's $29 Personal Theme Bundle deal - so that's 3 One Page WordPress themes at a 50% saving.

by Rob Hope via One Page Love

Social Media Visuals: How to Easily Create Visuals Without a Designer

ms-podcast196-donna-moritz-560

Do you use visuals in your social media? Want tools and tips to help you create images? To discover how to create great social media visuals when you’re not a designer, I interview Donna Moritz. More About This Show The Social Media Marketing podcast is an on-demand talk radio show from Social Media Examiner. It’s [...]

This post Social Media Visuals: How to Easily Create Visuals Without a Designer first appeared on .
- Your Guide to the Social Media Jungle


by Michael Stelzner via

BEO Play A1

An interactive launch experience for the sleek and sonorous new A1 bluetooth speaker from BEO Play
by via Awwwards - Sites of the day

Paradise Slider : jQuery Responsive Bootstrap Carousel Plugin

We have prepared 100+ Pre-made layouts of bootstrap carousel. it is a responsive slider with touch enabled function. This slider is easy to customize and you can create new layouts by combining these pre-made layouts.

Features:

  • More Than 100 Documentation Pages Includes
  • More Than 100 Pre-made Layouts
  • Touch Enabled
  • 100% Responsive
  • Beautiful Animation
  • Built With Bootstrap
  • Smooth Sliding Effects
  • Cross Browser Compatibility
  • YouTube, Vimeo & Self Hosted videos in slides.
  • Bootstrap Carousel Fade, Slide, Rotate and Zoom effects

The post Paradise Slider : jQuery Responsive Bootstrap Carousel Plugin appeared first on jQuery Rain.


by Admin via jQuery Rain

rsLiteGrid : jQuery Easly input Tabular data using Keyboard

Easly input tabular data using only your keyboard.

Features:

  • Optional minimum and/or maximum number of rows;
  • Configurable markup and tabstop for each column;
  • Use cursor keys, Tab, Shift+Tab to navigate across all the columns and rows;
  • Data can be imported/exported from/to Json;
  • Strong event driven support;
  • Rows can be added or removed asynchronously after an ellapsed time. Ideal for CSS3 animations;
  • Small footprint.

The post rsLiteGrid : jQuery Easly input Tabular data using Keyboard appeared first on jQuery Rain.


by Admin via jQuery Rain