设为首页 收藏本站
查看: 1268|回复: 0

[经验分享] ansible集中化自动管理(部署LAMP环境)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-8-23 08:57:26 | 显示全部楼层 |阅读模式
##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-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-1.el7.noarch.rpm
7zabbix-3.2: rpm -ivh http://repo.zabbix.com/zabbix/3. ... 7.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-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'


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-403342-1-1.html 上篇帖子: ansible playbook 下篇帖子: ansible-playbook安装tomcat1.8
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表