How to perform Fleet pacthing on Cloud using Ansible?
Ansible is a popular automation tool that can be used for fleet patching in a cloud environment.
With Ansible, you can create playbooks that specify which instances to patch, which patches to apply, and how to handle errors or rollbacks. Ansible can also be used in combination with other automation tools such as AWS Systems Manager, Azure Update Management, OCI System management Service, Google Cloud Operations
Here are the high level steps to use Ansible for patching instances:
Install Ansible
Configure Cloud Authentication
Install Ansible Collection
Create Inventory File --> you can use pythin to automate the file
Create a Playbook using Yaml code
run the Playbook
Verify the patching
eg: patching for AWS
Install Ansible
Configure Cloud Authentication
Install Ansible Collection
[ec2_instances]
10.0.0.1
10.0.0.2
10.0.0.3
...
python script to automate inventory file
import boto3
# Create an EC2 client
ec2 = boto3.client('ec2')
# Get a list of all running instances
instances = ec2.describe_instances(
Filters=[{'Name': 'instance-state-name', 'Values': ['running']}]
)['Reservations']
# Write the inventory file
with open('inventory.ini', 'w') as f:
f.write('[ec2_instances]\n')
for reservation in instances:
for instance in reservation['Instances']:
f.write('{} ansible_host={} ansible_user=ec2-user\n'.format(
instance['InstanceId'], instance['PublicIpAddress']))
===> create a playbook
- name: Apply security patches to EC2 instances
hosts: ec2_instances
become: yes
gather_facts: yes
tasks:
- name: Update package cache
yum:
update_cache: yes
- name: Upgrade all packages
yum:
name: '*'
state: latest
@@@@@@@@@Playbook for RDS instances
- name: Patch RDS instance
hosts: localhost
gather_facts: false
tasks:
- name: Get RDS instance details
rds_instance_info:
db_instance_identifier: my-rds-instance
region: us-east-1
register: rds_instance
- name: Apply patch to RDS instance
rds_instance:
db_instance_identifier: "{{ rds_instance.instance.db_instance_identifier }}"
region: us-east-1
apply_immediately: true
when: rds_instance.instance.db_instance_status == "available" and rds_instance.instance.pending_modified_values is not defined
@@@@@@@@@@@@@@ Playbook for AWS system manager
- name: Patch EC2 instances using AWS Systems Manager
hosts: tag_Name_my_instance_group
gather_facts: yes
become: yes
tasks:
- name: Run AWS Systems Manager document
aws_ssm:
name: AWS-RunPatchBaseline
parameters:
'Operation': 'Scan'
'PatchGroups': 'my_patch_group'
document_version: '$LATEST'
wait: yes
register: patch_scan
- name: Get list of missing patches
set_fact:
missing_patches: "{{ patch_scan | json_query('patch_baseline_report.' + inventory_hostname + '.MissingPatches') }}"
- name: Install missing patches
yum:
name: "{{ item }}"
state: latest
loop: "{{ missing_patches }}"
@@@@@@@@@@@@@Playbook for Postgre DB patching
- name: Patch PostgreSQL database
hosts: dbserver
become: yes
vars:
db_name: mydb
db_user: myuser
db_password: mypassword
patch_sql: "ALTER TABLE mytable ADD COLUMN mycolumn TEXT;"
tasks:
- name: Install PostgreSQL client package
package:
name: postgresql-client
- name: Apply patch to database
postgresql_db:
name: "{{ db_name }}"
user: "{{ db_user }}"
password: "{{ db_password }}"
login_host: "{{ inventory_hostname }}"
login_user: "{{ db_user }}"
login_password: "{{ db_password }}"
sql: "{{ patch_sql }}"
==> run the playbook
ansible-playbook -i inventory.ini patch_ec2_instances.yml
ansible-playbook patch_database.yml -i inventory.ini --limit dbserver
ansible-playbook patch_ec2_instances.yml
ansible-playbook patch_oci_instances.yml
Comments
Post a Comment