Chef Client Policy
Total Page:16
File Type:pdf, Size:1020Kb
Dear Participants, • Chef Mailing List Welcome to the course DevOps with Chef on FreeBSD. In • Git website this course, we’ll cover lot of theory and practical aspects. • Ruby Website We’ll focus on the core topics as mentioned in the agenda, Hardware/Software Requirement and we assume that you already know the prerequisites. If • 2 FreeBSD 11 systems/machines (Virtual or Physical), with not, then you might have to learn additional topics such Internet access and bash as the default shell with root as Git, Ruby and FreeBSD first. login over ssh or a normal user with sudo access. • A free account on Hosted Chef. The learning curve for Chef is a little steep at times. There, fore, just be patient and keep working on it, and you’ll eventually be done with the course. We’ll certainly help you throughout the course. But, be prepared to do a lot of self-study as well. Bear in mind that the best way to learn Chef is to just use it. Prerequisites • Knowledge of how to use version control system, pref, erably Git. • Basic Knowledge of FreeBSD OS Administration. • Basic knowledge of any scripting language, preferably Ruby. • Working knowledge of any text/code editor. Resources • FreeBSD • FreeBSD Documentation • Download FreeBSD • Chef Website • Chef Documentation • Chef IRC Channel TABLE OF CONTENTS Module 1: Introduction to Chef.................................................................................................................................................................3 Module 2: Resources.................................................................................................................................................................................7 Module 3: Setup the workstation............................................................................................................................................................10 Module 4: Bootstrap a node with Chef....................................................................................................................................................11 Module 5: Get started with writing your first cookbook........................................................................................................................14 Module 6: Deep Dive into Chef cookbook development.......................................................................................................................20 Module 7: Advanced chef concepts........................................................................................................................................................26 Module 8: Using syntax and linting tools like: Foodcritic and RuboCop..............................................................................................33 Module 9:Writing unit test and integration tests..................................................................................................................................38 ii MODULE 1 What is automation? Introduction to Chef Its definition depends on the context and situation. In general, automation is a non-manual way of performing What is DevOps? certain set of tasks. In automation of IT infrastructure, According to Wikipedia, “DevOps (a clipped compound of this set of tasks could be: creating new servers and infra, development and operations) is a culture, movement or structure, managing existing servers, deploying applica, practice that emphasizes the collaboration and commu, tions, updating both servers and applications, monitor, nication of both software developers and other ing servers and apps, gathering metrics, graphs, scaling information-technology (IT) professionals while automat, up or down the infrastructure and many more. ing the process of software delivery and infrastructural Why is it needed? changes.” Virtualization and Cloud have forced the need for auto, mation. In the earlier days (The Iron Age), IT infrastruc, ture growth was dependent on the physical hardware pur, chasing cycle. It used to take weeks from ordering an ac, tual server to it being delivered, and then deploying the OS and having the server ready for use. What are the diferent tools/platforms available? The most popular tools/platform available, as of now, for IT infrastructure automation are: ● Chef. ● Puppet. ● Ansible. 3 What is Chef? Overview of Chef. Chef is a powerful automation platform that transforms infrastructure into code. It’s open source. Its DSL is writ, Chef Architecture. ten in Ruby. And it’s supported by almost all major Linux, Unix, Windows, cloud and container platforms. It’s re, leased under Apache2 license. The project was started in 2008, and has been growing ever since. It has a large com, munity and is backed by a commercial for profit company/entity called “Chef”. Chef is being used by startups and Fortune 100 compa, nies, like Facebook, GE Capital and more. 4 Chef Components templates, etc. This repo could be managed by ver, sion control system such as Git. Chef Workstation Knife A workstation is a computer configured to run various chef command line tools, that interact with chef server It’s the command line tool to interact with chef server and nodes. It’s also used to create and maintain cook, as well as nodes. books. Chef DK Chef Server It’s a package that contains everything to get started A chef server acts as a single point of contact for worksta, with chef. Unfortunately, it’s not available for BSD as tion as well as the nodes/client. All the code (cookbooks/ of now. recipes) are uploaded to chef server. Chefspec Chef node It’s the unit test framework for chef. A node is any machine – physical, virtual, cloud, etc. – that Cookbook is managed by chef. A cookbook is a fundamental unit of configuration or Chef client policy. Following are the cookbook components: Chef client is a software or agent that runs on chef •Attributes nodes. It contacts chef server periodically and downloads the code (recipes, etc) and executes them. Attributes can be defined in files and can be overrid, den. They help in making configuration dynamic. Chef Workstation Components •Recipes Chef repo It’s the most fundamental configuration element. It’s It’s a repository structure, used to create and maintain authored in ruby. It must be stored within a cook, chef cookbooks. Cookbooks contains recipes, attributes, book. It’s a collection of resources, and can be in, 5 cluded, and can have dependencies. • Files Files inside the files directory are copied to the target node, just like scp. • Libraries It’s arbitrary ruby code to be included in a cookbook. • Templates A cookbook template is an embedded ruby file that’s used to dynamically generate static text file. • Metadata It’s a file that stores metadata about a cookbook. Typical Chef cookbook Workflow. • Create or Edit a cookbook. • Upload cookbook. • Provision machine. • Bootstrap machine. • Run Chef-client. • Ssh and validate. 6 MODULE 2 Resources Every resource has its own set of actions and properties. Most properties have default values. Some properties are available What is a resource? to all resources, for example, those used to send notifications to other resources and guards that help ensure that some re- A resource is a building block of recipes. They represent a sources are idempotent. piece of a system, e.g.: A package or directory, file, network, service, etc. It describes: For example, a resource that is used to install a tar package for version 1.16.1 may look something like this: • The desired state of the configured element/item. package 'tar' do • Steps needed to achieve that desired state. version '1.16.1' • Additional properties (called resource properties), if neces- sary. action :install End • The type of resource. Resource syntax: Common Functionality A resource is a Ruby block with four components: a type, a All resources (including custom resources) share a set of com- name, one (or more) properties (with values), and one (or mon actions, properties, conditional executions, notifications, more) actions. The syntax for a resource is like this: and relative path options. type 'name' do Actions property 'value' Actions could be :start, :stop, :restart, :reload or :nothing action :type_of_action end 7 Properties RESOURCE DESCRIPTION Following properties are common to every resource: ig- freebsd_package Use this to manage freebsd packages. nore_failure, retries, retry_delay, provider and Use cron resource to manage cron entries supports cron for time based scheduling. Guards Use it to install gems only to the instance of chef_gem ruby that is dedicated to chef-client. The not_if and only_if conditional executions can be used to Use this to execute scripts using the bash put additional guards for certain resources, so that they are bash interpreter. only run when the condition is met. package Use it to manage packages. Notifications Use this resource to add, remove, modify Notifies and subscribes notifications can be used with any re- user users and their attributes, e.g.: password, source. home directory, default shell, etc. Use this resource to add, remove, modify Common or most used resources group group and their attributes, e.g.: group members, etc. RESOURCE DESCRIPTION link Use it to create a symbolic or hard links. directory Use directory resource to manage directory. log Use this resource to create log entries. Use this resource to copy file from local Use it to copy directories from workstation cookbook_file remote_directory workstation to the node. to node. Use this to create and manage files on the Use this to copy/download