Ansible, Puppet and Terraform codes
Ansible, Puppet and Terraform codes
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:
Basic Steps:
1. 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:
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:
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:
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
handlers:
- name: start nginx
service:
name: nginx
state: restarted
3.
Run the Playbook: Execute the playbook with the ansible-playbook command:
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:
1.
provider "aws" {
region = "us-west-2"
}
2.
terraform init
3.
Plan: Terraform generates an execution plan, showing which resources it will create, update, or
delete.
terraform plan
4.
terraform apply
5.
terraform destroy
6.
For example:
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:
tasks:
- name: Install Apache package
apt:
name: apache2
state: present
when: ansible_os_family == "Debian"
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
simple Puppet manifest to install and ensure Apache HTTP Server is running
on a server:
Copy code
# apache.pp
class apache {
package { 'apache2':
ensure => installed,
service { 'apache2':
node default {
include apache
Explanation:
Steps to Run:
2.
This code assumes you're using a Debian-based OS where the Apache package is named
apache2.