ansible集中化自动管理(部署LAMP环境)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

##ansible集中化自动管理

目标:1、生成公钥,并上传ssh的公钥到被控端主机

      2、在ansible的主控端配置本地yum源和网络yum源

      3、安装ansible,用ansible上传yum源目录到被控端主机。

      4、用ansible管理被控端主机的系统、软件和服务。

      5、用playbooks剧本(yaml脚本文件)来管理被控端。


各种网络yum仓库:

6zabbix-2.4: rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm

6zabbix-3.2(兼容性不好,可能无法安装): http://repo.zabbix.com/zabbix/3.4/rhel/6/x86_64/


7zabbix-2.4: rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/7/x86_64/zabbix-release-2.4-1.el7.noarch.rpm

7zabbix-3.2: rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-1.el7.centos.noarch.rpm


centos6: wget -O /etc/yum.repos.d/6CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

6epel源:wget -O /etc/yum.repos.d/6epel.repo http://mirrors.aliyun.com/repo/epel-6.repo


centos7: wget -O /etc/yum.repos.d/7CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

7epel源:wget -O /etc/yum.repos.d/7epel.repo http://mirrors.aliyun.com/repo/epel-7.repo



网络环境:

asible主控端:192.168.10.1

ansible被控端:192.168.10.10~192.168.10.20



具体实施:

1、生成公钥,并上传ssh的公钥到被控端主机

第1步,在asible主控端生成公钥。

ssh-keygen  -t  rsa  -f  ~/.ssh/id_rsa  -N  ''

yum  install  -y  expect


第2步,批量上传公钥到被控端。

for  i  in  11

do

   ssh-copy-id  root@192.168.10.$i

   ssh  root@192.168.10.$i  ip  a

done

ssh-add


sed  -ri  '/^#UseDNS/c\UseDNS  no'  /etc/ssh/sshd_config

sed  -ri  '/^GSSAPIAuthentication/c\GSSAPIAuthentication  no'  /etc/ssh/sshd_config


grep  -En  '^UseDNS|^GSSAPIAuth'  /etc/ssh/sshd_config


2、在ansible的主控端配置本地yum源和网络yum源。

cd  /etc/yum.repos.d

mkdir  -pv bak

mv  -vf  *.repo  bak/

wget -O /etc/yum.repos.d/6epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

wget -O /etc/yum.repos.d/6CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

rpm -ivh http://repo.zabbix.com/zabbix/2.4/rhel/6/x86_64/zabbix-release-2.4-1.el6.noarch.rpm

sed  -ri  's/\$releasever/6/g'   6CentOS-Base.repo

cat  > rhel6.5.repo <<-EOF

[rhel6.5]

name=Red Hat Enterprise Linux $releasever - $basearch - Source

baseurl=file:///dvd

enabled=1

gpgcheck=0

EOF


yum  clean  all

yum  makecache  fast

yum  list  zabbix  ansible

yum install zabbix-server-mysql  zabbix-web-mysql  zabbix-agent --enablerepo=zabbix  -y

rpm  -qa  |grep  zabbix


3、安装ansible,用ansible上传yum源到被控端主机。

yum  install  -y  ansible

yum  install  -y  curl  elinks  lynx  createrepo

grep -b2 '^\[test\]'  /etc/ansible/hosts || echo -e '[test]\n192.168.10.11\n192.168.10.12'  >> /etc/ansible/hosts


ansible  test  -m  ping

ansible  test  -m  copy  -a  'src=/etc/ssh/sshd_config  dest=/etc/ssh/'

ansible  test  -m  shell -a  'service  sshd  restart'

ansible  test  -m  shell  -a  'rm  -rf  /etc/yum.repos.d/*;ls  /etc/yum.repos.d/'

ansible  test  -m  copy  -a  'src=/etc/yum.repos.d/  dest=/etc/yum.repos.d/  force=yes mode=755'

ansible  test  -m  shell  -a  'ls  /etc/yum.repos.d'


4、用ansible管理被控端主机的系统、软件和服务。

ansible  test  -m  shell  -a  'rpm  -q  httpd  mysql-server   php'

ansible  test  -m  yum  -a  'name=httpd  state=present'

ansible  test  -m  yum  -a  'name=mysql-server  state=present'

ansible  test  -m  yum  -a  'name=php  state=present'

ansible  test  -m  shell  -a  'rpm  -q  httpd  mysql-server   php'

ansible  test  -m  service  -a  'name=httpd  state=restarted  enabled=1'

ansible  test  -m  service  -a  'name=mysqld  state=restarted  enabled=1'


ansible  test  -m  shell  -a  'yum  install  -y  curl  elinks  lynx  createrepo  --enablerepo=rhel6.5'

ansible  test  -m  shell  -a  'rpm  -q   curl  elinks  lynx  createrepo'


ansible  test  -m  shell  -a  "echo  '<?php  phpinfo()  ?>' > /var/www/html/p.php"

ansible  test  -m  shell  -a  "echo  'apache test' > /var/www/html/a.html"

ansible  test  -m  shell  -a  'curl  127.0.0.1/a.html'


ansible  test  -m  shell  -a  'mysql  -e "grant  all on *.* to  admin  identified  by 'admin  with  grant option;flush  privileges'"'

ansible  test  -m  shell  -a  'mysql -uadmin  -padmin -e "show  databases;select  user,host,password  from  mysql.user;"'


5、用playbooks剧本(yaml脚本文件)来管理被控端。

目标1:编写一个playbooks剧本install_lamp.yaml,实现全自动部署LAMP环境。

vim  install_lamp.yaml

- hosts: all

  vars:

     http_port: 80

  remote_user: root

  tasks:

  - name: apache

    yum: pkg=httpd  state=present

    notify:

    - apache restart

  - name: mysql-server

    yum: pkg=mysql-server  state=present

    notify:

    - mysqld restart

  - name: php

    yum: pkg=php  state=present

  handlers:

    - name: apache restart

      service: name=httpd  state=restarted

    - name: mysqld restart

      service: name=mysqld  state=restarted


运行剧本:ansible-playbook  install_lamp.yaml

验证:ansible  test  -m  shell  -a  'rpm  -q  httpd  mysql-server  php'

      

目标2:编写一个playbooks剧本remove_lamp.yaml,实现全自动卸载LAMP环境。

vim  remove_lamp.yaml

- hosts: all

  vars:

     http_port: 80

  remote_user: root

  tasks:

  - name: apache

    yum: pkg=httpd  state=absent

  - name: mysql-server

    yum: pkg=mysql-server  state=absent

  - name: php

    yum: pkg=php  state=absent


运行剧本:ansible-playbook  remove_lamp.yaml

验证:ansible  test  -m  shell  -a  'rpm  -q  httpd  mysql-server  php'


      本文转自rshare 51CTO博客,原文链接:http://blog.51cto.com/1364952/1958483,如需转载请自行联系原作者




相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
6月前
|
运维 Kubernetes 网络安全
Ansible自动化运维工具之主机管理与自定义配置文件(2)
Ansible自动化运维工具之主机管理与自定义配置文件(2)
|
9月前
ansible的环境部署及简单使用
ansible的环境部署及简单使用
98 0
|
8月前
|
运维 负载均衡 关系型数据库
【运维知识进阶篇】用Ansible Roles重构LNMP架构(Linux+Nginx+Mariadb+PHP),实现4个项目一键部署
【运维知识进阶篇】用Ansible Roles重构LNMP架构(Linux+Nginx+Mariadb+PHP),实现4个项目一键部署
112 0
|
9月前
管理工具ansible的部署
管理工具ansible的部署
65 0
管理工具ansible的部署
|
5月前
|
Linux 网络安全 调度
使用docker部署awx-1.7.1.0(ansible图形化界面)
使用docker部署awx-1.7.1.0(ansible图形化界面)
286 0
|
5月前
|
弹性计算 关系型数据库 数据库
通过计算巢轻松部署 Ansible Semaphore
AnsibleSemaphore是一个现代化的Ansible用户界面,通过计算巢可轻松部署。
152 1
通过计算巢轻松部署 Ansible Semaphore
|
8月前
|
应用服务中间件 nginx
Ansible模块——软件包管理模块
Ansible模块——软件包管理模块
|
9月前
|
存储 JSON 数据安全/隐私保护
ansible定义变量和管理事实
ansible定义变量和管理事实
124 0
|
11月前
|
Unix 应用服务中间件 Linux
ansible在web集群中批量部署nfs
ansible在web集群中批量部署nfs
154 0
|
11月前
|
监控 网络安全 开发工具
Ansible批量部署zabbix-agent(含zabbix-agent的yum源,zabbix自动发现和添加的界面操作)
Ansible批量部署zabbix-agent(含zabbix-agent的yum源,zabbix自动发现和添加的界面操作)
219 0