|# CentOS/RPM based distro
|yum install ansible
|
|# Ubuntu via PPA
|apt-get install apt-add-repository
|apt-add-repository ppa:rquillo/ansible
|apt-get update
|apt-get install ansible
|# Homebrew
|brew install ansible
|# or via PIP (includes development deps)
|pip install ansible
NOPE.JPG
|# Internal PING (not actual ICMP)
|ansible -m ping all
|
|# Uptime of all your inventory
|ansible -m command -a "uptime"
|module_name: argument=value argument=value argument=value
|# Add the PPA
|- apt_repository: repo="ppa:chris-lea/node.js"
|
|# Install the package
|- apt: pkg=nodejs state=present
|
|# Pinned versions
|- apt: pkg=php=5.6 state=present
|- authorized_key: user=deployer key="{{item}}"
| with_file:
| - keys/adam.pub
| - keys/otherUser.pub
|- file: dest=/home/someUser/.ssh state=directory mode=0700 owner=someUser group=someGroup
|
|- copy: src="{{item}}" dest=/home/someUser/.ssh/ owner=someUser mode=0600
| with_items:
| - id_rsa
| - id_rsa.pub
|# Ensure a job that runs at 2 and 5 exists.
|# Creates an entry like "* 5,2 * * ls -alh > /dev/null"
|- cron: name="check dirs" hour="5,2" job="ls -alh > /dev/null"
|
|# Ensure an old job is no longer present. Removes any job that is
|# prefixed by "#Ansible: an old job" from the crontab
|- cron: name="an old job" state=absent
|# Search and replace
|- replace: >
| dest=/etc/php5/fpm/pool.d/www.conf
| regexp='^listen = /var/run/php5-fpm.sock'
| replace='listen = 127.0.0.1:9000'
|
|# Ensure line doesn't exist in file
|- lineinfile: dest=/etc/sudoers state=absent regexp="^%wheel"
|# Ensure the python dependency exists for Ansible to manage mySQL
|- apt: pkg=python-mysqldb state=present
|
|# Create our deployer user
|- mysql_user: name=deployer password=deployer state=present priv=*.*:ALL
|
|# Create our database
|- mysql_db: name=some_db state=present
Templates use the Jinga2 library, which is covered in the next section.
|# Upload template somefile.j2 to /home/someUser/someFile
|# Replacing variables/expressions
|- template: src=somefile.j2 dest=/home/someUser/someFile
|# Service must be running
|- service: name=apache2 state=running
|
|# Restart service if running, start if not
|- service: name=ntpd state=restarted
|
|# Stop service if running
|- service: name=samba state=stopped
If Ansible modules are the tools in your workshop, playbooks are your design plans.
|---
|# Start your YAML file with 3 dashes to signify start
|
|# Define a value
|php_version: 5.6
|
|# Create an array
|create_users:
| - adam
| - admin
| - deployer
|
|# Multi-dimensional hash/dict/whatever name you are familiar with
|virtual_hosts:
| production:
| - web1.somehost.com
| - web2.somehost.com
| staging:
| - test.somehost.com
Not covering much YAML here - check the docs!
|ansible_distribution # Ubuntu
|ansible_distribution_release # precise
|ansible_distribution_version # 12.04
|<VirtualHost *:80>
| # Expand docroot variable
| DocumentRoot {{ docroot }}
|
| # Expand serveradmin variable with default filter
| ServerAdmin {{ serveradmin | default("admin@admin.com") }}
|</VirtualHost>
|- name: Install Apache2
| apt: pkg=apache2 state=present
|
|- name: Install php5 v{{ php5_version }}
| apt: pkg=php5={{ php5_version }} state=present
|# Task
|- apt: pkg=php5 state=present
| notify: restart Apache
|
|# Handler
|- name: restart Apache
| service: name=apache2 state=restarted
/