root@router:lamp_haproxy#pwd
/root/lamp_haproxy
root@router:lamp_haproxy#cat
hosts
[self]
localhost ansible_connection=local
[nodes:children]
webservers
dbservers
lbservers
[nodes:vars]
ansible_ssh_private_key_file=/home/web/.ssh/id_rsa
[webservers]
192.168.8.101
[dbservers]
192.168.8.102
[lbservers]
192.168.8.103
一.创建管理用户
ansible all -i hosts -m user -a 'name=web generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa'
ansible all -i hosts -m shell -a "echo 'web ALL=(ALL) NOPASSWD:ALL' >/etc/sudoers.d/web"
2.将web用户的公钥导入到被管理节点
ansible nodes -i hosts -m copy -a "src=/home/web/.ssh/id_rsa.pub dest=/home/web/.ssh/authorized_keys"
ansible nodes -i hosts -m file -a "dest=/home/web/.ssh/authorized_keys mode=600 owner=web group=web"
root@router:lamp_haproxy#ansible
nodes -i hosts -u web -s -m ping
192.168.8.103 | SUCCESS => {
}
192.168.8.102 | SUCCESS => {
}
192.168.8.101 | SUCCESS => {
}
测试ok
二.简单playbook
cat >foo.yml <<EOF
---
- name: install a few packages
EOF
root@router:lamp_haproxy#ansible-playbook
foo.yml
PLAY [install a few packages] **************************************************
TASK [setup] *******************************************************************
ok: [192.168.8.101]
TASK [install python bindings for selinux] *************************************
ok: [192.168.8.101] => (item=[u'libselinux-python', u'libsemanage-python'])
TASK [test to see if selinux is running] ***************************************
ok: [192.168.8.101]
TASK [install apache] **********************************************************
ok: [192.168.8.101]
TASK [ensure apache is running] ************************************************
ok: [192.168.8.101]
PLAY RECAP *********************************************************************
192.168.8.101
提示:
ansible-playbook提供非常实用的retry和tag机制(执行指定tag,跳过某些tag)来减少没必要的操作以加快执行速度。
root@router:playbook#ansible-playbook foo.yml
--list-tags
playbook: foo.yml
root@router:playbook#ansible-playbook foo.yml --list-task
playbook: foo.yml
root@router:playbook#ansible-playbook foo.yml --tags p3
PLAY [install a few packages]
**************************************************
TASK [setup]
*******************************************************************
ok: [192.168.8.101]
TASK [install apache]
**********************************************************
ok: [192.168.8.101]
PLAY RECAP
*********************************************************************
192.168.8.101
root@router:playbook#ansible-playbook foo.yml --skip-tags p1,p2,p3
PLAY [install a few packages]
**************************************************
TASK [setup]
*******************************************************************
ok: [192.168.8.101]
TASK [ensure apache is running]
************************************************
ok: [192.168.8.101]
PLAY RECAP
*********************************************************************
192.168.8.101
三.基于roles的模块化playbook(lamp_haproxy)
1.初始化roles目录结构(ansible-galaxy init)
http://docs.ansible.com/ansible/galaxy.html
root@router:lamp_haproxy#mkdir roles
root@router:lamp_haproxy#cd roles/
root@router:roles#for i in common apache web db haproxy;do ansible-galaxy init $i;done
- common was created successfully
- apache was created successfully
- web was created successfully
- db was created successfully
- haproxy was created successfully
root@router:roles#tree common/
common/
├── defaults
│
├── files
├── handlers
│
├── meta
│
├── README.md
├── tasks
│
├── templates
├── tests
│
│
└── vars
8 directories, 8 files
2.编写playbook
i.selinux
selinux task
cat >roles/common/tasks/selinux.yml <<EOF
---
- name: install python bindings for selinux
- name: test to see if selinux is running
EOF
ii.ntp
ntp变量
cat
>group_vars/all
---
# Variables here are applicable to all host groups
httpd_port:
80
ntpserver: 192.168.8.254
EOF
模板ntp.conf.j2
cat
>roles/common/templates/ntp.conf.j2
driftfile /var/lib/ntp/drift
restrict 127.0.0.1
restrict -6 ::1
server {{ ntpserver }}
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
EOF
ntp task
cat >roles/common/tasks/ntp.yml <<EOF
---
-
name:
install
ntp
-
name:
configure
ntp
- name: start the ntp service
EOF
ntp handler
cat
>roles/common/handlers/main.yml
---
-
name:
restart
ntp
EOF
作为common组件的入口,只需要在main.yml中include定义好的yml及可
cat >roles/common/tasks/main.yml <<EOF
---
-
include:
selinux.yml
- include: ntp.yml
EOF
iii.apache
cat >roles/apache/tasks/main.yml <<EOF
---
EOF
iv.web
web变量
cat
>group_vars/webservers
---
# Variables for the web server configuration
# iface: eth1
iface: '{{ ansible_default_ipv4.interface }}'
# sample webapp
repository: https://github.com/bennojoy/mywebapp.git
# this is the sha1sum of V5 of test test webapp.
webapp_version:
351e47276cc66b018f4890a0
EOF
web task
cat >roles/web/tasks/main.yml <<EOF
---
-
name:
install
php and git
- name: configure selinux to allow httpd to connect to remote database
- name: checkout code from repository
EOF
v.mariadb
mariadb变量
cat
>group_vars/dbservers
---
# The variables file used by the playbooks in the dbservers group.
# These don't have to be explicityly imported by vars_files: they are autopopulated.
mysqlservice: mysqld
mysql_port: 3306
dbuser: root
dbname: foodb
upassword: abc
EOF
mariadb配置文件模板
cat
>roles/db/templates/my.cnf.j2
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
init_connect = 'SET collation_connection = utf8_general_ci'
init_connect = 'SET NAMES utf8'
character_set_server = utf8
collation_server = utf8_general_ci
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
port={{ mysql_port }}
[mysqld_safe]
pid-file = /var/run/mysqld/mysqld.pid
log-error = /var/log/mysqld.log
EOF
mariadb task
cat >roles/db/tasks/main.yml <<EOF
---
- name: install mariadb/firewalld
- name: configure selinux to start mariadb on any port
- name: create mariadb configuration file
- name: create mariadb log file
- name: create mariadb PID directory
- name: start mariadb service
- name: start firewalld service
- name: insert firewalld rule
- name: create application database
- name: create application database user
EOF
mariadb handler
cat
>roles/db/handlers/main.yml
---
- name: restart mariadb
EOF
vi.haproxy
haproxy变量
cat
>group_vars/lbservers
---
# Variables for the HAproxy configuration
# HAProxy supports 'http' and 'tcp'. For SSL, SMTP, etc, use 'tcp'.
mode: http
# Port on which HAProxy should listen
listenport: 8888
# A name for the haproxy daemon, this will be the suffix in the logs.
daemonname: myapplb
# Balancing Algorithm. Available options:
# roundrobin,source,leastconn,uri (if persistance is required use 'source')
balance: leastconn
# iface: eth1
iface: '{{ ansible_default_ipv4.interface }}'
EOF
haproxy配置文件模板
cat
>roles/haproxy/templates/haproxy.cfg.j2
global
defaults
backend app
EOF
haproxy task
cat >roles/haproxy/tasks/main.yml <<EOF
---
EOF
haproxy handler
cat
>roles/haproxy/handlers/main.yml
---
-
EOF
vii.整合playbook
cat >lamph.yml <<EOF
---
- name: install a few packages
- name: configure and deploy database server
- name: configure and deploy web server
- name: configure and deploy load balancer
EOF
3.测试playbook
root@router:lamp_haproxy#ansible-playbook
-i hosts lamph.yml
PLAY [install a few packages] **************************************************
TASK [setup] *******************************************************************
ok: [192.168.8.103]
ok: [192.168.8.102]
ok: [192.168.8.101]
TASK [common : install python bindings for selinux] ****************************
ok: [192.168.8.103] => (item=[u'libselinux-python', u'libsemanage-python'])
ok: [192.168.8.102] => (item=[u'libselinux-python', u'libsemanage-python'])
ok: [192.168.8.101] => (item=[u'libselinux-python', u'libsemanage-python'])
TASK [common : test to see if selinux is running] ******************************
ok: [192.168.8.103]
ok: [192.168.8.102]
ok: [192.168.8.101]
TASK [common : install ntp] ****************************************************
ok: [192.168.8.102]
ok: [192.168.8.103]
ok: [192.168.8.101]
TASK [common : configure ntp] **************************************************
ok: [192.168.8.103]
ok: [192.168.8.102]
ok: [192.168.8.101]
TASK [common : start the ntp service] ******************************************
ok: [192.168.8.103]
ok: [192.168.8.102]
ok: [192.168.8.101]
PLAY [configure and deploy database server] ************************************
TASK [setup] *******************************************************************
ok: [192.168.8.102]
TASK [db : install mariadb/firewalld] ******************************************
ok: [192.168.8.102] => (item=[u'mariadb-server', u'MySQL-python', u'firewalld'])
TASK [db : configure selinux to start mariadb on any port] *********************
ok: [192.168.8.102]
TASK [db : create mariadb configuration file] **********************************
ok: [192.168.8.102]
TASK [db : create mariadb log file] ********************************************
changed: [192.168.8.102]
TASK [db : create mariadb PID directory] ***************************************
ok: [192.168.8.102]
TASK [db : start mariadb service] **********************************************
ok: [192.168.8.102]
TASK [db : start firewalld service] ********************************************
ok: [192.168.8.102]
TASK [db : insert firewalld rule] **********************************************
ok: [192.168.8.102]
TASK [db : create application database] ****************************************
ok: [192.168.8.102]
TASK [db : create application database user] ***********************************
ok: [192.168.8.102]
PLAY [configure and deploy web server] *****************************************
TASK [setup] *******************************************************************
ok: [192.168.8.101]
TASK [apache : install apache] *************************************************
ok: [192.168.8.101]
TASK [apache : ensure apache is running] ***************************************
ok: [192.168.8.101]
TASK [web : install php and git] ***********************************************
ok: [192.168.8.101] => (item=[u'php', u'php-mysql', u'git'])
TASK [web : configure selinux to allow httpd to connect to remote database] ****
skipping: [192.168.8.101]
TASK [web : checkout code from repository] *************************************
ok: [192.168.8.101]
PLAY [configure and deploy load balancer] **************************************
TASK [setup] *******************************************************************
ok: [192.168.8.103]
TASK [haproxy : install haproxy] ***********************************************
ok: [192.168.8.103]
TASK [haproxy : create haproxy configuration file] *****************************
ok: [192.168.8.103]
TASK [haproxy : start haproxy service] *****************************************
ok: [192.168.8.103]
PLAY RECAP *********************************************************************
192.168.8.101
192.168.8.102
192.168.8.103
root@router:lamp_haproxy#elinks -dump 192.168.8.103:8888
root@router:lamp_haproxy#telnet 192.168.8.102 3306
Trying 192.168.8.102...
Connected to 192.168.8.102.
Escape character is '^]'.
R
5.5.44-MariaDBu+r!J0O|!?D}jzH6sbr9cemysql_native_password
^]
telnet> ^C
Connection closed by foreign host.