Unit 3
Unit 3
Terraform – Getting
      Started
 Provisioning infrastructure
through software to achieve
 consistent and predictable
       environments.
                  Core Concepts
                    Stored in               Declarative
Defined in code
                        source               or
                        control              imperative
          Idempotent and
                                  Push or pull
             consistent
Infrastructure as Code Benefits
          Automated
          deployment
          Consistent
          environments
          Repeatable process
Reusable
components
          Documented
          architecture
         Automating Infrastructure Deployment
It’s not a magic wand that gives you power over all clouds and systems.
It embraces all major Cloud Providers and provides common language to orchestrate your infrastructure
                                              resources.
Architecture
Architecture
Architecture
Architecture
Architecture
                 Terraform
                Components
Terraform                Terraform File
Executable               Terraform
Terraform
                         Statefile
Providers API
Terraform config
                         file
Terraform Executable
Terraform Providers
Terraform Providers
                                 Major Cloud
                                 Partners
   Terraform: Providers
Type       Name
Terraform: Example (Simple
      local resource)
variable "aws_access_key" {}    Variables
variable "aws_secret_key" {}
}
resource "aws_instance"
"ex"{                                Resource
  ami = "ami-c58c1dd3"
  instance_type =
  "t2.micro"
}
output "aws_public_ip" {
                                     Outpu
    value =                          t
    "${aws_instance.ex.public_dns}
    "
}
                    Code Example
provider “azurerm” {
  subscription_id = “subscription-id”
 client_id =
 “principal-used-for-access”
 client_secret =
 “password-of-principal”     tenant_id =
 “tenant-id”
 alias = “arm-1”
}
resource
 “azurerm_resource_group”{    name
 = “resource-group-name”
 location = “East US”
                      Terraform Syntax
#Create a variable
variable var_name {
key = value #type, default,
description
}
#Use a variable
${var.name} #get string
${var.map[“key”]} #get map element
${var.list[idx]} #get list element
                        Terraform Syntax
#Create provider
provider provider_name {
key = value #depends on resource, use alias as
needed
}
#Create data object
data data_type data_name
${data_type.data_name.attribute(args)}
                      Terraform Syntax
#Create resource
resource resource_type resource_name {
key = value #depends on resource
}
#Reference resource
${resource_type.resource_name.attribute(args)
}
Terraform Workflow
Workflow: Adoption
stages
          Single
        contributor
     Terraform Core: Init
  Locking
  Local / remote
  Environments
     Terraform state file
      Play along!
       -    AWS account
       -    Demo files
            Examine the Terraform file
      Deploy theconfiguration
      Review theresults Play along!
Dem     -   AWS account
o       -   Azure subscription
        -   DNS domain
        -   Terraform software
            (terraform.io)
        -   Demo files
      Examine the Terraform file Deploy
       Play along!
         -   AWS account
         -   Terraform software
             (terraform.io)
         -   Demo files
      Examine the Terraform file Deploy
      Play along!
        -   AWS account
        -   Terraform software
            (terraform.io)
        -   Demo files
•Ansible
                                   Why Ansible?
Capture all the servers 100% of the time, regardless of infrastructure, location, etc.
ORCHESTRATION THAT PLAYS WELL WITH OTHERS – HP SA, Puppet, Jenkins, RHNSS, etc.
 USER
 S
                                                      HOSTS
                       INVENTORY         CLI
                        MODULES        PLUGINS
                                                    NETWORK
 ANSIBLE                                             DEVICES
PLAYBOOK
         PUBLIC / PRIVATE
              CLOUD                                     PUBLIC / PRIVATE
                                                             CLOUD
                                    CMDB
                             MODULES          PLUGINS
                                                           NETWORK
 ANSIBLE                                                    DEVICES
PLAYBOOK
        PUBLIC /
            PRIVATE                                               PUBLIC /
            CLOUD                                                     PRIVATE
                                 CMDB
                                                                      CLOUD
                      ANSIBLE
                                                              S IN THE
                      AU    T OMPython,
                                  A T IOPowershell,    E TOOLKIT”
                                          N E NG orINany language Extend
                           M O    D U L E S A R E
                                  Ansible simplicity to the entire stack
 USER                 “TOOL
 S
                                                                       HOSTS
                       INVENTORY               CLI
                       MODULES              PLUGINS
                                                                     NETWORK
 ANSIBLE                                                              DEVICES
PLAYBOOK
 USER
 S
                                                                 HOSTS
                          INVENTORY         CLI
                          MODULES         PLUGINS
                                                               NETWORK
 ANSIBLE                                                        DEVICES
PLAYBOOK
                      {{ some_variable | to_nice_yaml }}
                             PUBLIC /
                                 PRIVATE                                          PUBLIC / PRIVATE
                                 CLOUD                                                 CLOUD
                                                   CMDB
                                                              INVENTORY
                                                              List of systems in your infrastructure that
                                                              automation is executed against
[web]                                      ANSIBLE AUTOMATION ENGINE
webserver1.example.com
webserver2.example.com
                     USERS
[db]
dbserver1.example.com                                                                  HOSTS
                                            INVENTORY          CLI
[switches]
leaf01.internal.com
leaf02.internal.com
                                            MODULES          PLUGINS
                                                                                     NETWORK
[firewalls]                                                                           DEVICES
                  ANSIBLE
checkpoint01.internal.com
                 PLAYBOOK
[lb]
f5-01.internal.com
        PUBLIC /
            PRIVATE                                               PUBLIC /
            CLOUD                                                     PRIVATE
                                  CMDB
                                                                      CLOUD
           CLOUD
 USER
 S         Red Hat Openstack, Red Hat Satellite, VMware,
                                                                      HOSTS
           AWS EC2, RackspaINcVeE,NGTOoRoYgle ComputCeLIEngine,
           Azure
                            MODULES          PLUGINS
                                                                    NETWORK
 ANSIBLE                                                             DEVICES
PLAYBOOK
        PUBLIC /
            PRIVATE                                      PUBLIC /
            CLOUD             CMDB                           PRIVATE
                                                             CLOUD
 USER                                 CMDB
 S
                                      ServiceNow, Cobbler, BMHCO,SCTSustom
                       INVENTORY          CLI
                                      cmdb
                       MODULES          PLUGINS
                                                            NETWORK
 ANSIBLE                                                     DEVICES
PLAYBOOK
        PUBLIC /
            PRIVATE                                           PUBLIC /
            CLOUD                                                 PRIVATE
                                     CMDB
                                                                  CLOUD
 USER
 S
                                                                  HOSTS
                             INVENTORY                CLI
                                                    PLUGINS
  AUTOMATE                                                      NETWORK
                M ODULES                                         DEVICES
 AEVERYTHIN
  RNSeIBdLHE at Enterprise
PLAYBOOK
                 G          Linux, Cisco routers,
  switches, Juniper routers, Windows hosts,
 Arista
  Checkpoint firewalls, NetApp storage, F5 load
  balancers and more
    Using Ansible
1
3
 Ad-hoc commands
# check all my inventory hosts are ready to be
# managed by Ansible
$ ansible all -m ping
   • Hosts
   • Groups
   • Inventory-specific data (variables)
   • Static or dynamic sources
    Ansible Playbooks
1
7
---
- name: install and start apache
  hosts: web
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
 tasks:
 - name: install httpd
   yum: pkg=httpd state=latest
 - name: write the apache config file
   template: src=/srv/httpd.j2 dest=/etc/httpd.conf
 - name: start httpd
   service: name=httpd state=started
---
- name: install and start apache
  hosts: web
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
 tasks:
 - name: install httpd
     yum: pkg=httpd state=latest
 - name: write the apache config file
     template: src=/srv/httpd.j2 dest=/etc/httpd.conf
 - name: start httpd
     service: name=httpd state=started
---
- name: install and start apache
  hosts: web
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
 tasks:
 - name: install httpd
   yum: pkg=httpd state=latest
 - name: write the apache config file
   template: src=/srv/httpd.j2 dest=/etc/httpd.conf
 - name: start httpd
   service: name=httpd state=started
---
- name: install and start apache
  hosts: web
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
 tasks:
 - name: install httpd
   yum: pkg=httpd state=latest
 - name: write the apache config file
   template: src=/srv/httpd.j2 dest=/etc/httpd.conf
 - name: start httpd
   service: name=httpd state=started
---
- name: install and start apache
  hosts: web
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
 tasks:
 - name: install httpd
   yum: pkg=httpd state=latest
 - name: write the apache config file
   template: src=/srv/httpd.j2 dest=/etc/httpd.conf
 - name: start httpd
   service: name=httpd state=started
---
- name: install and start apache
  hosts: web
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
 tasks:
 - name: install httpd
   yum: pkg=httpd state=latest
 - name: write the apache config file
   template: src=/srv/httpd.j2 dest=/etc/httpd.conf
 - name: start httpd
   service: name=httpd state=started
tasks:
  - name: add cache
    dir   file:
     path: /opt/cache
     state: directory
handlers:
  - name: restart nginx
    service:
      name: nginx
      state: restarted
 Variables
Ansible can work with metadata from various
sources and manage their context in the form of
variables.
  • Command line parameters
  • Plays and tasks
  • Files
  • Inventory
  • Discovered facts
  • Roles
     Tips/Best Practices
26
     Simplicity
27
Simplicity
        - hosts: web
          tasks:
          - yum:
              name: httpd
              state: latest
             - service:
                 name: httpd
                 state: started
                 enabled: yes
Simplicity
         - hosts: web
           name: install and start
           apache tasks:
             - name: install apache
               packages yum:
                 name: httpd
                 state: latest
30
Inventory
            10.1.2.75
            10.1.5.45
            10.1.4.5
            10.1.0.40
            w14301.example.com
            w17802.example.com
            w19203.example.com
            w19304.example.com
Inventory
            db1    ansible_host=10.1.2.75
            db2    ansible_host=10.1.5.45
            db3    ansible_host=10.1.4.5
            db4    ansible_host=10.1.0.40
            web1   ansible_host=w14301.example.com
            web2   ansible_host=w17802.example.com
            web3   ansible_host=w19203.example.com
            web4   ansible_host=w19203.example.com
    Dynamic Inventories
●    Stay in sync automatically
●    Reduce human error
CMDB
                                         PUBLIC /
                                             PRIVATE
                                             CLOUD
     YAML Syntax
34
YAML and Syntax
39
Roles
• Think about the full life-cycle of a service, microservice or
  container — not a whole stack or environment
• Keep provisioning separate from configuration and app
  deployment
• Roles are not classes or object or libraries – those are
  programming constructs
• Keep roles loosely-coupled — limit hard dependencies on
  other roles or external variables
      Variable
     Precedence
41
The order in which the same variable from
different sources will override each other.
         Variable
       Precedence                   13. Playbook host_vars
  1.   Extra vars
  2.   Include params               14. Inventory host_vars
  3.   Role (and include_role)      15. Inventory file/script host vars
       params
                                    16. Playbook group_vars
  4.   Set_facts / registered
       vars                         17. Inventory group_vars
  5.   Include_vars                 18. Playbook group_vars/all
  6.   Task vars (only for the
       task)                        19. Inventory group_vars/all
  7.   Block vars (only for tasks   20. Inventory file or script group vars
       in the block)                21. Role defaults
  8.   Role vars
  9.   Play vars_files              22. Command line values (e.g., -u user)
10.    Play vars_prompt
 11.   Play vars
12.    Host facts / Cached
       set_facts
     Things to Avoid
44
Things to Avoid
● Using command modules
  ○ Things like shell, raw, command etc.
● Complex tasks...at first
  ○ Start small
● Not using source control
  ○ But no really...
      Ansible
      Content
     Collections
46
  Collections Q and A
What are they?
 ● Collections are a distribution format for Ansible content that can include playbooks, roles,
     modules, and plugins. You can install and use collections through Ansible Galaxy and
     Automation Hub
How do I get them?
●ansible-galaxy collection install namespace.collection -p /path Where
 can I get them?
 ● Today
      ○ Galaxy
      ○ Automation Hub
Collection Directory Structure
●   docs/: local documentation for the collection
●   galaxy.yml: source data for the MANIFEST.json that will be part of the collection package
●   playbooks/: playbook snippets
    ○    tasks/: holds 'task list files' for include_tasks/import_tasks usage
●   plugins/: all ansible plugins and modules go here, each in its own subdir
    ○    modules/: ansible modules
    ○    lookups/: lookup plugins
    ○    filters/: Jinja2 filter plugins
    ○    connection/: connection plugins required if not using default
●   roles/: directory for ansible roles
●   tests/: tests for the collection's content
Collections: Let’s Go!
1. Init collection: ansible-galaxy collection init foo.bar
2.   Sanity testing: ansible-test sanity
3.   Unit tests: ansible-test units
4.   Integration tests: ansible-test integration
5.   Build the collection: ansible-galaxy collection build
6. Publish the collection: ansible-galaxy collection publish
7. Install the collection: ansible-galaxy collection install
     foo.bar
      Resource Link Index
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable
https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#using-variables
https://docs.ansible.com/ansible/latest/user_guide/playbooks_intro.html
https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
https://docs.ansible.com/ansible/latest/user_guide/intro_getting_started.html#getting-started
https://docs.ansible.com/ansible/latest/user_guide/intro_adhoc.html
https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html
https://docs.ansible.com/ansible/latest/index.html
https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html
https://docs.ansible.com/ansible/latest/user_guide/intro_dynamic_inventory.htm
l https://docs.ansible.com/ansible-lint/
https://github.com/ansible/ansible
https://github.com/ansible/ansible-lint
https://ansible.github.io/workshops/
https://www.ansible.com/resources/ebooks/get-started-with-red-hat-ansible-towe
r https://docs.ansible.com/ansible/latest/user_guide/collections_using.html
https://docs.ansible.com/ansible/latest/dev_guide/developing_collections.html