+ All Categories
Home > Documents > Configuration management with Ansible and GitConfiguration management with Ansible and Git Author...

Configuration management with Ansible and GitConfiguration management with Ansible and Git Author...

Date post: 15-Aug-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
23
Configuration management with Ansible and Git Paul Waring ([email protected], @pwaring) March 16, 2016
Transcript
Page 1: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Configuration management with Ansible and Git

Paul Waring ([email protected], @pwaring)

March 16, 2016

Page 2: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Topics

I Configuration managementI Version controlI FirewallI ApacheI Git HooksI Bringing it all togetherI Live demo

Page 3: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Configuration management

I Old days: edit files on each server, manual package installationI Boring, repetitive, error-proneI Computers are good at this sort of thingI Write a playbook/manifest and let software do the restI Less firefighting, more tea-drinking

Page 4: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Ansible

I One of several optionsI Free and open source software - GPLv3I Developed by the community and Ansible Inc.I Ansible Inc now part of RedHat

Page 5: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Alternatives to Ansible

I CfEngineI Puppet, ChefI SaltStack

Page 6: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Why Ansible?

I Minimal dependencies: SSH and Python 2I Many major distros ship with bothI No agents/daemons (except SSH)I Supports really old versions of Python (2.5 / RHEL 5)I Linux, *BSD, OS X and Windows

Page 7: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Why Ansible?

I Scales up and downI But. . . no killer featuresI A bit like: vim vs emacs

Page 8: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Configuration file

I Global options which apply to all nodesI INI formatI Write once, then leave

Page 9: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Configuration file

[defaults]hostfile = hosts

Page 10: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Inventory file

I List of managed nodesI Allows overriding of global options on per-node basisI Group similar nodes, e.g. web servers

Page 11: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Inventory file

[staging]testvm ansible_ssh_host=127.0.0.1

ansible_ssh_port=2222ansible_ssh_user=vagrantansible_ssh_private_key_file=

~/.vagrant.d/insecure_private_key

[production]bigv ansible_ssh_host=bigv.ukuug.org

ansible_ssh_user=rootansible_ssh_private_key_file=~/id_rsa

Page 12: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Modules

I Abstraction of functionality, e.g. create accountsI Core, Extras and Third PartyI Mostly Python, can use other languages too

Page 13: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Playbooks

I List of tasks to run on nodesI Imperative vs declarativeI Can be idempotentI Yet Another Markup Language (YAML)

Page 14: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Firewall playbook

- name: Security playbookhosts: vagrantsudo: True

tasks:- name: enable incoming ssh

ufw:rule: allowto_port: ssh

Page 15: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Firewall playbook

- name: allow all outgoing trafficufw:

direction: outgoingpolicy: allow

- name: deny all incoming trafficufw:

direction: incomingpolicy: denylog: yes

Page 16: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Web playbook

vars:install_packages:

- apache2- libapache2-mod-php5- php5-mysql

tasks:- name: Install Apache

with_items: "{{ install_packages }}"apt:

name: "{{ item }}"update_cache: yescache_valid_time: 3600

Page 17: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Web playbook

- name: Start Apacheservice:

name: apache2state: started

Page 18: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Handlers

- name: enable vhost configuration fileswith_items: vhosts_filesfile:

src: "{{ vhosts_available_dir }}/{{ item }}"dest: "{{ vhosts_enabled_dir }}/{{ item }}"state: link

notify: reload apache

handlers:- name: reload apache

service: name=apache2 state=reloaded

Page 19: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Git

I Written for Linux kernel developmentI Distributed - each copy is a repositoryI Alternatives: Mercurial (Mozilla), GNU Bazaar (Ubuntu)I Git has won the DVCS wars

Page 20: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Git features

I Rollback/undo changes, e.g. git checkout -- <file>I View full history to the beginning of time: git logI Branching is cheap

Page 21: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Git hooks

I Perform actions at given points in workflowI Example: pre-commit (unit tests)I Example: post-commit (deployment)

Page 22: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Pre-commit

#!/bin/bash

files=$(git diff --staged --name-only --diff-filter=MA \| grep -E "ansible/[^/]*\.yml")

for filepath in $files; doansible-playbook --syntax-check $filepath -i localhoststatus=$?

if [ $status != 0 ]; thenecho "Syntax check failed on: ${filepath}"exit $status

fidone

exit 0

Page 23: Configuration management with Ansible and GitConfiguration management with Ansible and Git Author Paul Waring (paul@xk7.net, @pwaring) Created Date 20160320110710Z ...

Post-commit

#!/bin/bash

export ANSIBLE_CONFIG="${PWD}/ansible/ansible.cfg"export HOSTS_FILE="${PWD}/ansible/hosts"

files=$(git log --name-only --pretty=format: \--diff-filter=MA -n 1 \

| grep -E "ansible/[^/]*\.yml")

for filepath in $files; doansible-playbook ${filepath} -i ${HOSTS_FILE}

done


Recommended