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

author author     2022-09-16     348

关键词:

##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  [email protected]$i

   ssh  [email protected]$i  ip  a

done

ssh-add


sed  -ri  ‘/^#UseDNS/cUseDNS  no‘  /etc/ssh/sshd_config

sed  -ri  ‘/^GSSAPIAuthentication/cGSSAPIAuthentication  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] 192.168.10.11 192.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‘


本文出自 “网络技术天地” 博客,请务必保留此出处http://1364952.blog.51cto.com/1354952/1958483

ansible自动化运维详解ansible的安装部署参数使用清单管理配置文件参数及用户级ansible操作环境构建(代码片段)

文章目录ansible自动化运维详解(一)ansible的安装部署、参数使用、清单管理、配置文件参数及用户级ansible操作环境构建一、ansible的安装部署1.1、ansible简介1.2、实验环境1.3、安装部署ansible二、ansible的基本信息和参数使... 查看详情

ansible使用笔记

...被控端无需任何操作默认使用SSH协议对设备进行管理主从集中化 查看详情

ansible自动化运维实战在docker环境部署ansible管理平台awx

【Ansible自动化运维实战】在docker环境部署ansible管理平台awx一、awx介绍二、检查本地docker环境1.检查docker版本2.检查docker状态3.检查系统版本三、安装docker-compose1.下载docker-compose软件包2.给文件增加执行权限3.检查docker-compose版本4.... 查看详情

集中化管理平台ansible详解(代码片段)

什么是ansible?ansible是一种集成IT系统的配置管理、应用部署、执行特定任务的开源平台.它是基于python语言,由Paramiko和PyYAML两个关键模块构建。集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置... 查看详情

详解ansible(roles)自动化部署配置lamp架构(代码片段)

Roles简介Ansible为了层次化、结构化地组织Playbook,使用了角色(roles)。Roles能够根据层次型结构自动装载变量文件、task以及handlers等。简单来讲,roles就是通过分别将变量、文件、任务、模块及处理器放置于单独的目录中,并可... 查看详情

lamp基础环境部署

...务需求频繁基于LAMP或LNMP部署业务系统,出于方便实施和自动化管理,编写LAMP部署脚本,实现一键安装部署,后续配合脚本Saltstack批量执行。二.生产环境    Linux:Centos6.9    Apac 查看详情

掌握ansible角色(roles)自动化部署配置lamp架构(代码片段)

Roles简介:Ansible为了层次化、结构化地组织Playbook,使用了角色(roles)。Roles能够根据层次型结构自动装载变量文件、task以及handlers等。简单来讲,roles就是通过分别将变量、文件、任务、模块及处理器放置于单独的目录中,并... 查看详情

ansible的安装及常用模块

...、默认使用SSH(SecureShell)协议对设备进行管理。3、主从集中化管理。4、配置简单、功能强大、扩展性强。5、支持API及自定义模块,可通过Python轻松扩展。6、通过Playbooks来定制强大的配 查看详情

自动化运维ansible之roles部署配置lamp架构(代码片段)

Roles介绍Ansible为了层次化、结构化地组织Playbook,使用了角色(roles)。Roles能够根据层次型结构自动装载变量文件、task以及handlers等。简单来讲,roles就是通过分别将变量、文件、任务、模块及处理器放置于单独的目录中,并可... 查看详情

ansible自动化运维管理工具的概述与部署(代码片段)

Ansible自动化运维管理工具的概述与部署项目总体思路一、Ansible概述二、ansible环境安装部署三、ansible命令行模块1.command模块2.shell模块3.cron模块4.user模块5.group模块6.copy模块7.file模块8.hostname模块9.ping模块10.yum模块11.service/systemd模... 查看详情

ansible自动化运维管理工具的概述与部署(代码片段)

Ansible自动化运维管理工具的概述与部署项目总体思路一、Ansible概述二、ansible环境安装部署三、ansible命令行模块1.command模块2.shell模块3.cron模块4.user模块5.group模块6.copy模块7.file模块8.hostname模块9.ping模块10.yum模块11.service/systemd模... 查看详情

自动化运维之ansible-安装部署与基础命令篇(代码片段)

一、Ansible简介Ansible基于Python语言开发,集合了众多优秀运维工具的优点,实现了批量运行命令、部署程序、配置系统等功能。二、安装部署Ansible服务Ansible自动化运维环境由控制主机与被管理主机组成,由于Ansible是基于SSH协议... 查看详情

安装部署自动化运维之ansible(代码片段)

初步了解Ansibleansible基于Python开发,,集合了众多运维工具的优点,实现了批量运行命令,部署程序,配置系统等功能。默认通过SSH协议进行远程命令执行或下发配置,无需部署任何客户端代理软件,从而使得自动化环境部署变... 查看详情

ansible运维自动化--配置管理工具(代码片段)

Ansible的使用介绍:Ansible是为了更方便、快捷的进行配置管理。用Ansible可以将平常复杂的配置工作变得简单,更加标准化且更容易控制。Ansible可以实现100、1000台批量部署等。Ansible特点:(1)部署简单,只需在主控端部署Ansible... 查看详情

ansible自动化运维实战在jumpserver平台使用ansible管理服务器

【Ansible自动化运维实战】在Jumpserver平台使用Ansible管理服务器一、Jumpserver介绍二、本地环境规划三、部署JumpServer1.下载一键部署脚本2.一键部署JumpServer3.查看部署结果四、访问Jumpserver平台1.进入Jumpserver登录页2.访问Jumpserver首页... 查看详情

第1天:ansible安装部署

Ansible介绍Ansible是一个简单的自动化引擎,可完成配置管理、应用部署、服务编排以及各种IT需求。它是一款使用Python语言开发实现的开源软件,其依赖Jinjia2、paramiko和PyYAML这几个python库。Ansible安装部署简单,只需要再主控端部... 查看详情

saltstack实战之配置管理-lamp自动化部署

SaltStack实战之配置管理-LAMP自动化部署学习 SaltStackSaltStack实战之配置管理-LAMP自动化部署1.部署思路2.编写lamp.sls1.部署思路650)this.width=650;"src="https://s2.51cto.com/wyfs02/M00/98/E8/wKiom1lB7T2QE2CZAABOVbwTRK8932.png"title="部署 查看详情

自动化运维之ansible(代码片段)

Ansible概述Ansible基于Python开发,集合了众多优秀运维工具的特点,实现了批量运行命令、部署程序、配置系统等功能。默认通过SSH协议进行远程命令执行或下发配置,无需部署任何客户端代理软件,从而使得自动化环境部署变得... 查看详情