0% found this document useful (0 votes)
4 views

Ansible, Puppet and Terraform codes

Uploaded by

knikisha77
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Ansible, Puppet and Terraform codes

Uploaded by

knikisha77
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Ansible, Puppet and Terraform

Puppet, Ansible, and Terraform are all popular tools used in infrastructure automation,
configuration management, and provisioning. Each tool has its unique strengths and use cases,
but they often complement each other in modern DevOps workflows. Here’s an overview of how
to use each of them:

1. Puppet
Puppet is a configuration management tool that automates the management of system
configurations across your infrastructure. It ensures that systems are in the desired state by
managing their configuration files, services, packages, and more.

Key Concepts:

● Manifests: Puppet code written in the Puppet DSL (domain-specific language).


Manifests define the desired state of a system.
● Modules: Pre-packaged collections of manifests that define configurations for particular
applications or services.
● Puppet Master and Agent: Puppet uses a client-server architecture, with a Puppet
master server and multiple Puppet agents running on the nodes you want to manage.

Basic Steps:

1. Install Puppet:

On the Puppet server (Master):


sudo apt install puppetserver

On the Puppet agent nodes:


sudo apt install puppet

Write a Puppet Manifest (example: install and configure Nginx): Create a .pp file (e.g.,
nginx.pp) on the Puppet master.

package { 'nginx':
ensure => installed,
}

service { 'nginx':
ensure => running,
enable => true,
require => Package['nginx'],
}

file { '/etc/nginx/nginx.conf':
ensure => file,
content => template('nginx/nginx.conf.erb'),
notify => Service['nginx'],
}

2.

Apply the Manifest: Run the puppet apply command on the node:

sudo puppet apply nginx.pp

3.

Manage via Puppet Agent: After setting up the master-agent architecture, you can apply
configurations automatically by running the Puppet agent on the nodes:

sudo puppet agent -t

4.

2. Ansible
Ansible is an agentless, open-source IT automation tool used for configuration management,
application deployment, and task automation. Ansible uses YAML-based playbooks to define
configurations.

Key Concepts:

● Playbooks: YAML files where you define tasks to be executed on remote machines.
● Inventory: A list of hosts or machines that Ansible manages.
● Modules: Reusable units of code that Ansible uses to perform tasks (e.g., apt, yum,
copy, service).

Basic Steps:

1. Install Ansible:

On a control machine (where you'll run Ansible commands):


sudo apt install ansible

Create an Inventory File: An inventory file (e.g., hosts) contains a list of target machines.

[web_servers]
192.168.1.10
192.168.1.11

[db_servers]
192.168.1.12

2.

Write a Playbook (example: install and start Nginx): Create a playbook file (e.g., nginx.yml):

---
- name: Install and start Nginx
hosts: web_servers
become: yes

tasks:
- name: Install Nginx
apt:
name: nginx
state: present
notify:
- start nginx

- name: Ensure nginx is running


service:
name: nginx
state: started
enabled: yes

handlers:
- name: start nginx
service:
name: nginx
state: restarted

3.

Run the Playbook: Execute the playbook with the ansible-playbook command:

ansible-playbook -i hosts nginx.yml


4.

3. Terraform
Terraform is an Infrastructure as Code (IaC) tool that allows you to define and provision
infrastructure in a declarative way using HashiCorp Configuration Language (HCL).

Key Concepts:

● Providers: Terraform uses providers (e.g., AWS, Azure, Google Cloud) to interact with
APIs and manage resources.
● Resources: Define infrastructure components like servers, databases, networking, etc.
● State: Terraform maintains a state file to track the current infrastructure state.
● Modules: Reusable collections of Terraform code.

Basic Steps:
Install Terraform: Download and install Terraform from the official website:

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -


sudo apt-get update && sudo apt-get install terraform

1.

Create a Terraform Configuration (example: creating an EC2 instance on AWS): Create a


.tf file (e.g., main.tf):

provider "aws" {
region = "us-west-2"
}

resource "aws_instance" "web" {


ami = "ami-12345678"
instance_type = "t2.micro"
}

2.

Initialize Terraform: Initialize your Terraform working directory:

terraform init

3.

Plan: Terraform generates an execution plan, showing which resources it will create, update, or
delete.
terraform plan

4.

Apply: Apply the configuration to provision the resources:

terraform apply

5.

Destroy: If you want to tear down the resources:

terraform destroy

6.

When to Use Each Tool:


● Puppet: Best for configuration management where you need to enforce the desired
state of systems over time, with robust reporting and an agent-based architecture.
● Ansible: Ideal for simpler configuration management and task automation where you
want agentless operation and easy-to-write playbooks.
● Terraform: Perfect for Infrastructure as Code (IaC) to provision, manage, and version
control cloud infrastructure.

How They Complement Each Other:


● Terraform can provision your infrastructure (e.g., virtual machines, networks).
● Puppet/Ansible can be used to configure the machines and install software once
they’re provisioned.
● You might use Terraform for creating your cloud resources, then use Ansible or
Puppet to configure the software and services running on those resources.

For example:

1. Use Terraform to create an EC2 instance.


2. Use Ansible to configure Nginx and deploy an application on the EC2 instance.

By combining these tools, you can create a fully automated infrastructure pipeline that handles
everything from provisioning to configuration.

If you need further help with specific configurations or examples, feel free to ask!
Here’s an example of a simple Ansible playbook that installs and starts Apache
HTTP Server on a target server:

Simple Ansible Playbook:


Yaml file:

- name: Install and start Apache


hosts: webservers
become: yes

tasks:
- name: Install Apache package
apt:
name: apache2
state: present
when: ansible_os_family == "Debian"

- name: Start Apache service


service:
name: apache2
state: started
enabled: true

Explanation:
1. Playbook Header:
○ name: A descriptive name for the playbook.
○ hosts: Specifies the group of target servers defined in the inventory file.
○ become: yes: Enables privilege escalation (like sudo).
2. Tasks:
○ Task 1: Install Apache
■ Uses the apt module to install apache2 on Debian-based systems.
○ Task 2: Start Apache
■ Uses the service module to start and enable the Apache service.
3. Condition:
○ when: ansible_os_family == "Debian" ensures the task runs only on
Debian-based systems.

Inventory File:

[webservers]
192.168.56.101 ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa

Run the Playbook:


1. Save the playbook as apache.yml.

2. Run the playbook:

ansible-playbook -i inventory apache.yml

simple Puppet manifest to install and ensure Apache HTTP Server is running
on a server:

Simple Puppet Code:


puppet

Copy code

# apache.pp

class apache {

package { 'apache2':
ensure => installed,

service { 'apache2':

ensure => running,

enable => true,

require => Package['apache2'],

# Apply the apache class to the node

node default {

include apache

Explanation:

1. Class Definition (apache):


○ Manages the installation of the Apache package and ensures the service is
running and enabled.
○ package: Ensures that the apache2 package is installed.
○ service: Starts and enables the apache2 service and requires the apache2
package.
2. Node Declaration (default):
○ Applies the apache class to the default node.

Steps to Run:

1. Save the manifest to a file, e.g., apache.pp.


Apply the manifest using the Puppet command:
bash
Copy code
puppet apply apache.pp

2.

This code assumes you're using a Debian-based OS where the Apache package is named
apache2.

You might also like