Ansible:14

DevOps Classroom Series – 03/Jan/2021

Ansible Tower Setup

Scenario: We have two environments

  • We have two environments
    • Dev
    • Test
  • In the dev environment we are asked to install java 11
    • command is sudo apt-get install openjdk-11-jdk
  • In the test environment we are asked to install java 8
    • command is sudo apt-get install openjdk-8-jdk
  • How can we do this by writing one playbook
  • Refer Here for the changeset Preview
  • Now lets try to run ansible playbook on dev environment Preview
  • Now lets try to run playbook on test environment Preview

Scenario: Sensitive Content

  • We have a variable file in ansible which has usernames and passwords stored in plain text
  • Displaying sensitive content in the form of plain text in ansible playbooks is not secure, so we need some way of encrypting this content and that is where Ansible vault comes into play
  • We can use ansible vault to create variables file with three options
    • prompt for password
    • password in text
    • password from script
  • Lets create a sensitive variable file using prompt Refer Here
ansible-vault create --vault-id @prompt vars/secretbyprompt.yaml

ansible-vault create --vault-id ~/passwords.sh  vars/even_more_secure.yaml

  • Now lets apply encryption to existing file. Refer Here
ansible-vault encrypt --vault-id ~/passwords.sh vars/sensitive.yaml

  • So now lets use vars/sensitive in ansible playbook

Setting up windows hosts for Ansible Control

  • Lets create a windows server Preview
  • To set up ansible to run with windows server, we need pywinrm module on the Ansible control server
  • Windows Server should have atleast
    • Powershell 3.0
    • .NET 4.0
  • In the windows server we would be configuring winrm listener
    • So login into windows server and launch powershell as admin
    • create an Self Certificate
    New-SelfSignedCertificate -CertStoreLocation Cert:\LocalMachine\My -DnsName "$env:COMPUTERNAME" -FriendlyName "WinRM HTTPS Certificate" -NotAfter (Get-Date).AddYears(5) Preview
    • store the thumbprint
    • With the generated certificate create a new winrm listener
    New-Item -Path WSMan:\localhost\Listener -Transport HTTPS -Address * -CertificateThumbPrint <your-thumbprint> Preview
    • Now Lets add a firewall rule to allow https communication
    New-NetFirewallRule -DisplayName 'WinRM Mgmt' -Profile Domain,Private,Public -Direction Inbound -Action Allow -Protocol TCP -LocalPort 5986 Set-Item -Path "WSMan:\localhost\Service\Auth\Basic" -Value $true Preview
    • Now install chocolatey on windows Refer Here
    • Now lets move to ansible control server
    • check if the winrm module is install in python Preview
    • install pip3 on control server
    sudo apt install python3-pip sudo pip3 install pywinrm Preview

Parallelism in Ansible

  • Controlling ansible playbook execution strategies Refer Here
  • ansible by default can run parallely on 5 nodes which is referred as 5 forks.
  • To run the ansible playbooks on one node at a time, set the fork value to 1
  • In ansible we can set batch size for executing on nodes in the batches
---
- name: test playbook
  hosts: all
  serial: 2
  tasks:
    - name: test task
      debug:
        msg: "this is test task"

Ansible Collections

  • In Ansible the major unit of work is modules. There are lots of community modules which are developed.
  • Earlier to make these modules available to the users it had to wait till the ansible release.
  • Since this process is not upto the mark ansible has introduced collections.
  • Collections is collection of
    • modules
    • roles
  • Ansible-Galaxy had undergone changes to include collections
  • lets review one ansible collection Preview Preview Preview Preview

Exercises

  1. Create an ansible role to deploy openmrs Refer Here Preview
  2. Write an ansible playbook to install nop commerce on linux Refer Here
  3. Try this if possible Refer Here