Port to 1.2 variable syntax and real roles, minor best-practice updates

pull/63/head
Tim Gerla 11 years ago
parent 43c05aae55
commit 6896742fd1
  1. 43
      lamp_simple/README.md
  2. 11
      lamp_simple/playbooks/db.yml
  3. 11
      lamp_simple/playbooks/web.yml
  4. 2
      lamp_simple/roles/common/tasks/main.yml
  5. 2
      lamp_simple/roles/db/handlers/main.yml
  6. 12
      lamp_simple/roles/db/tasks/main.yml
  7. 10
      lamp_simple/roles/web/tasks/copy_code.yml
  8. 14
      lamp_simple/roles/web/tasks/install_httpd.yml
  9. 3
      lamp_simple/roles/web/tasks/main.yml
  10. 24
      lamp_simple/site.yml

@ -1,37 +1,14 @@
Building a simple LAMP stack and deploying Application using Ansible Playbooks.
-------------------------------------------
These playbooks are meant to be a reference and starter's guide to building Ansible Playbooks. These playbooks were tested on CentOS 6.x so we recommend that you use CentOS or RHEL to test these modules.
These playbooks require Ansible 1.2.
### Installing Ansible
These playbooks are meant to be a reference and starter's guide to building
Ansible Playbooks. These playbooks were tested on CentOS 6.x so we recommend
that you use CentOS or RHEL to test these modules.
Running this playbook requires setting up Ansible first. Luckily this is a very simple process on CentOS 6.x:
yum install http://epel.mirrors.arminco.com/6/x86_64/epel-release-6-8.noarch.rpm
yum install python PyYAML python-paramiko python-jinja2
git clone git://github.com/ansible/ansible.git
cd ansible
source hacking/env-setup
Generate/synchronize your SSH keys (optional you can pass -k parameter to prompt for password)
ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
Create a sample inventory file. The inventory file contains a grouped list of hostnames that are managed by Ansible. The command below will just add "localhost" to the host list.
echo "localhost" > ansible_hosts
Test if we are setup properly:
ansible -i ansible_hosts localhost -m ping
localhost | success >> {
"changed": false,
"ping": "pong"
}
Now we set up our LAMP stack. The stack can be on a single node or multiple nodes. The inventory file 'hosts' defines the nodes in which the stacks should be configured.
This LAMP stack can be on a single node or multiple nodes. The inventory file
'hosts' defines the nodes in which the stacks should be configured.
[webservers]
localhost
@ -39,8 +16,12 @@ Now we set up our LAMP stack. The stack can be on a single node or multiple node
[dbservers]
bensible
Here the webserver would be configured on the local host and the dbserver on a server called "bensible". The stack can be deployed using the following command:
Here the webserver would be configured on the local host and the dbserver on a
server called "bensible". The stack can be deployed using the following
command:
ansible-playbook -i hosts site.yml
Once done, you can check the results by browsing to http://localhost/index.php. You should see a simple test page and a list of databases retrieved from the database server.
Once done, you can check the results by browsing to http://localhost/index.php.
You should see a simple test page and a list of databases retrieved from the
database server.

@ -1,11 +0,0 @@
---
# This playbook deploys MySQL and configures database on the db node(s)
- hosts: dbservers
user: root
tasks:
- include: ../roles/common/tasks/main.yml
- include: ../roles/db/tasks/main.yml
handlers:
- include: ../roles/db/handlers/main.yml
- include: ../roles/common/handlers/main.yml

@ -1,11 +0,0 @@
---
# This Playbook deploys the WebServers with httpd and the code.
- hosts: webservers
user: root
tasks:
- include: ../roles/common/tasks/main.yml
- include: ../roles/web/tasks/install_httpd.yml
- include: ../roles/web/tasks/copy_code.yml
handlers:
- include: ../roles/web/handlers/main.yml

@ -6,7 +6,7 @@
tags: ntp
- name: Configure ntp file
template: src=../roles/common/templates/ntp.conf.j2 dest=/etc/ntp.conf
template: src=ntp.conf.j2 dest=/etc/ntp.conf
tags: ntp
notify: restart ntp

@ -4,3 +4,5 @@
- name: restart mysql
service: name=mysqld state=restarted
- name: restart iptables
service: name=iptables state=restarted

@ -2,7 +2,7 @@
# This playbook will install mysql and create db user and give permissions.
- name: Install Mysql package
action: yum pkg=$item state=installed
action: yum pkg={{ item }} state=installed
with_items:
- mysql-server
- MySQL-python
@ -13,7 +13,7 @@
seboolean: name=mysql_connect_any state=true persistent=yes
- name: Create Mysql configuration file
action: template src=../roles/db/templates/my.cnf.j2 dest=/etc/my.cnf
action: template src=my.cnf.j2 dest=/etc/my.cnf
notify:
- restart mysql
@ -21,12 +21,12 @@
service: name=mysqld state=started enabled=true
- name: insert iptables rule
lineinfile: dest=/etc/sysconfig/iptables state=present regexp="$mysql_port" insertafter="^:OUTPUT " line="-A INPUT -p tcp --dport $mysql_port -j ACCEPT"
lineinfile: dest=/etc/sysconfig/iptables state=present regexp="{{ mysql_port }}"
insertafter="^:OUTPUT " line="-A INPUT -p tcp --dport {{ mysql_port }} -j ACCEPT"
notify: restart iptables
- name: Create Application Database
mysql_db: name=$dbname state=present
mysql_db: name={{ dbname }} state=present
- name: Create Application DB User
mysql_user: name=$dbuser password=$upassword priv=*.*:ALL host='%' state=present
mysql_user: name={{ dbuser }} password={{ upassword }} priv=*.*:ALL host='%' state=present

@ -1,9 +1,9 @@
---
# This Playbook is responsible for copying the latest dev/production code from the version control system.
# These tasks are responsible for copying the latest dev/production code from
# the version control system.
- name: Copy the code from repository
git: repo=${repository} dest=/var/www/html/
git: repo={{ repository }} dest=/var/www/html/
- name: Create's the index.php file
template: src=../roles/web/templates/index.php.j2 dest=/var/www/html/index.php
- name: Creates the index.php file
template: src=index.php.j2 dest=/var/www/html/index.php

@ -1,22 +1,20 @@
---
# This playbook installs http and the php modules.
# These tasks install http and the php modules.
- name: Install http and php etc
action: yum name=$item state=installed
yum: name={{ item }} state=present
with_items:
- httpd
- php
- php-mysql
- git
- libsemanage-python
- libselinux-python
- name: insert iptables rule for httpd
lineinfile: dest=/etc/sysconfig/iptables state=present regexp="$httpd_port" insertafter="^:OUTPUT " line="-A INPUT -p tcp --dport $httpd_port -j ACCEPT"
register: last_run
- name: Apply iptable rule
service: name=iptables state=restarted
only_if: ${last_run.changed}
lineinfile: dest=/etc/sysconfig/iptables state=present regexp="{{ httpd_port }}" insertafter="^:OUTPUT "
line="-A INPUT -p tcp --dport {{ httpd_port }} -j ACCEPT"
notify: restart iptables
- name: http service state
service: name=httpd state=started enabled=yes

@ -0,0 +1,3 @@
---
- include: install_httpd.yml
- include: copy_code.yml

@ -1,5 +1,23 @@
---
# This Playbook deploys the whole application stack in this site.
# This playbook deploys the whole application stack in this site.
- include: playbooks/db.yml
- include: playbooks/web.yml
- name: apply common configuration to all nodes
hosts: all
user: root
roles:
- common
- name: configure and deploy the webservers and application code
hosts: webservers
user: root
roles:
- web
- name: deploy MySQL and configure the databases
hosts: dbservers
user: root
roles:
- db