使用自动化运维工具 Ansible 集中化管理 用playbook批量搭建lamp

ansible概述和运行机制
实战-安装并配置Ansible管理两个节点
ansible常见模块高级使用方法
实战-使用Playbook批量部署多台LAMP环境

安装并配置 Ansible 管理两个节点
安装 ansible 服务
实验环境
在这里插入图片描述
ansible 服务端 xuegod63 192.168.1.63
ansible 节点 xuegod63 192.168.1.63
ansible 节点 xuegod64 192.168.1.64
设置 cd /etc/hosts
cd /etc/hostname

在xuegod63上安装ansible
1、设置EPEL仓库
Ansible仓库默认不在yum仓库中,因此我们需要使用下面的命令启用epel仓库。
[root@xuegod63 ~]# yum install epel-release -y
2、 使用yum安装Ansible
[root@xuegod63 ~]#yum install ansible -y
安装完成后,检查ansible版本:
[root@xuegod63 ~]# ansible --version

( ansible命令参数
anisble命令语法: ansible [-i 主机文件] [-f 批次] [组名] [-m 模块名称] [-a 模块参数]
ansible详细参数:
-v,–verbose # 详细模式,如果命令执行成功,输出详细的结果 (-vv –vvv -vvvv)
-i PATH, -inventory=PATH # 指定 host 文件的路径,默认是在 /etc/ansible/hosts
inventory [ˈɪnvəntri] 库存
-f NUM,-forks=NUM # NUM 是指定一个整数,默认是 5 ,指定 fork 开启同步进程的个数。
-m NAME,-module-name=NAME # 指定使用的 module 名称,默认使用 command模块
-a,MODULE_ARGS #指定 module 模块的参数
-k,-ask-pass #提示输入 ssh 的密码,而不是使用基于 ssh 的密钥认证
-sudo # 指定使用 sudo 获得 root 权限
-K,-ask-sudo-pass #提示输入 sudo 密码,与 -sudo 一起使用
-u USERNAME,-user=USERNAME # 指定移动端的执行用户
-C,–check #测试此命令执行会改变什么内容,不会真正的去执行
ansible-doc详细参数:
ansible-doc -l #列出所有的模块列表
ansible-doc -s 模块名 #查看指定模块的参数 -s, --snippet # [ˈsnɪpɪt] 片断
例:[root@xuegod63 ~]# ansible-doc -s service

ansible基于ssh连接-i (inventory)参数后指定的远程主机时,也可以写端口,用户,密码。
格式:ansible_ssh_port:指定ssh端口 ansible_ssh_user:指定 ssh 用户 ansible_ssh_pass:指定 ssh 用户登录是认证密码(明文密码不安全) ansible_sudo_pass:指明 sudo 时候的密码
例: [root@xuegod63 ~]# vim /etc/ansible/hosts #文件 /etc/ansible/hosts 维护着Ansible中服务器的清单。在文件最后追加以下内容)

操作:
[root@xuegod63 ~]# vim /etc/ansible/hosts
[web-servers] #主机组名
192.168.1.64 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456

简单测试下主机的连通性
[root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m ping
-i # 指定 host 文件的路径,默认是在 /etc/ansible/hosts
-m # 指定使用的ping模块
报错:
192.168.1.63 | FAILED! => {
“msg”: “Using a SSH password instead of a key is not possible because Host Key checking is enabled and sshpass does not support this. Please add this host’s fingerprint to your known_hosts file to manage this host.”
}
解决:[root@xuegod63 ~]# ssh root@192.168.1.63 #手动连接一下/etc/ansible/hosts主机清单中的主机,这样就可以在ansible服务器上保存目标主机的fingerprint指纹。后期可以正常连接了
[root@xuegod63 ~]# ansible -i hosts web-servers -m ping #测试成功
192.168.1.64 | SUCCESS => { #表示成测试。通信成功。
“changed”: false, #因为ping命令不会改变被管理的服务器的状态。所以是false正常
“ping”: “pong”
}
2、基于ssh密钥来访问定义主机清单
一般来说,使用明文密码不安全,所以增加主机无密码访问。
在Ansible服务端生成密钥,并且复制公钥到节点中。
root@xuegod63 ~]#ssh-keygen #一路回车
使用ssh-copy-id命令来复制Ansible公钥到节点:xuegod63和xuegod63
[root@xuegod63 ~]# ssh-copy-id root@192.168.1.63
[root@xuegod63 ~]# ssh-copy-id root@192.168.1.64
[root@xuegod63 ~]# ssh 192.168.1.64
[root@xuegod64 ~]# exit

[root@xuegod63 ~]# vim /etc/ansible/hosts #在文件的最后添加以下内容
删除之前在最后添加的两行主机清单:
[web-servers]
192.168.1.64 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456
增加:
[web-servers]
192.168.1.63
192.168.1.64

3 在Ansible服务端运行命令
(1) ping模块检查网络连通性
ansible -i /etc/ansible/hosts ‘web-servers’ -m ping

ansible ‘web-servers’ -m ping

(2)command模块执行shell命令,command:作为ansible的默认模块,可以运行远程权限范围内的所有shell命令
ansible -m command -a “uptime” ‘web-servers’ #也可以把主机清单组名写到最后,这样方便阅读命令
192.168.1.63 | SUCCESS | rc=0 >>
12:45:23 up 32 min, 5 users, load average: 0.17, 0.11, 0.27

192.168.1.64 | SUCCESS | rc=0 >>
12:45:23 up 26 min, 2 users, load average: 0.03, 0.03, 0.10
检查节点的内核版本
[root@xuegod63 ~]# ansible -m command -a “uname -r” ‘web-servers’
给节点增加用户
[root@xuegod63 ~]# ansible -m command -a “useradd mk123” ‘web-servers’
192.168.1.64 | SUCCESS | rc=0 >>
192.168.1.63 | SUCCESS | rc=0 >>
[root@xuegod63 ~]# ansible -m command -a “grep mk123 /etc/passwd” ‘web-servers’
将df命令在所有节点执行后,重定向输出到本机的/tmp/command-output.txt文件中
[root@xuegod63 ~]# ansible -m command -a “df -Th” ‘web-servers’ > /tmp/command-output.txt
[root@xuegod63 ~]# cat /tmp/command-output.txt

ansible常见模块高级使用方法
(1)、command模块为ansible默认模块,不指定-m参数时,使用的就是command模块; comand模块比较简单,常见的命令都可以使用,但其命令的执行不是通过shell执行的,所以,像这些 “<”, “>”, “|”, and "&"操作都不可以,当然,也就不支持管道; 缺点:不支持管道,没法批量执行命令; ansible -m command -a “useradd mk123” ‘web-servers’

(2)、shell模块:使用shell模块,在远程命令通过/bin/sh来执行;所以,我们在终端输入的各种命令方式,都可以使用。
例1:运行free -m 命令
[root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m shell -a “free -m”
注:但是我们自己定义在/.bashrc或/.bash_profile中的环境变量shell模块由于没有加载,所以无法识别;如果需要使用自定义的环境变量,就需要在最开始,执行加载自定义脚本的语句;
对shell模块的使用可以分成两块:

  1. 如果待执行的语句少,可以直接写在一句话中:
    [root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m shell -a “source ~/.bash_profile && df -h | grep sda3”
  2. 如果在远程待执行的语句比较多,可写成一个脚本,通过copy模块传到远端,然后再执行;但这样就又涉及到两次ansible调用;对于这种需求,ansible已经为我们考虑到了,script模块就是干这事的;

(3)
scripts模块
使用scripts模块可以在本地写一个脚本,在远程服务器上执行:
[root@xuegod63 ~]# vim /etc/ansible/net.sh
#!/bin/bash
date
hostname
[root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m script -a “/etc/ansible/net.sh”
copy模块:实现主控端向目标主机拷贝文件,类似scp功能
例1:把ansible主机上的/etc/hosts文件复制到主机组中机器的/tmp目录下
[root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m copy -a "src=/etc/hosts dest=/tmp/ owner=root group=root mode=0755
在xuegod64上查看
[root@xuegod64 ~]# ll /tmp/hosts
-rwxr-xr-x 1 root root 240 8月 24 16:09 /tmp/hosts

绿色是没发生变化 黄色是发生变化了 红色是错误
(4)
stat模块获取远程文件信息
[root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m stat -a “path=/tmp/hosts”

(5)get_url模块实现远程主机下载指定url到本地,支持sha256sum文件校验。
例如:下载epel-release-latest-7.noarch.rpm到主机清单中的/tmp/目录下
[root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m get_url -a “url=https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm dest=/tmp/ mode=0440 force=yes”
注:url=https://xxx 的等号=前后不能有空格
扩展:查看force=yes的作用
[root@xuegod63 ~]# ansible-doc -s get_url #在弹出的信息中找到force
如果force=yes,当下载文件时,如果所下的内容和原目录下的文件内容不一样,则替换原文件,如果一样,就不下载了。
如果为“否”,则仅在目标不存在时才下载文件。 一般来说,只有小型本地文件才应该为“是”。 在0.6之前,该模块表现为默认为“是”。
查看下载的文件:
[root@xuegod63 ~]# ll /tmp/epel-release-latest-7.noarch.rpm
-r–r----- 1 root root 15080 8月 24 16:20 /tmp/epel-release-latest-7.noarch.rpm

测试:下载文件时,当文件不一样时,会替换原来的文件
[root@xuegod64 ~]# cp /etc/passwd /tmp/epel-release-latest-7.noarch.rpm
[root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m get_url -a “url=https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm dest=/tmp/ mode=0440 force=yes”
192.168.1.63 | SUCCESS => {
“changed”: false, #xuegod63上原来的文件和当前的文件一样,就没有改变。执行成功,但没有发生改变,那么显示绿色
。。。
}
192.168.1.64 | SUCCESS => {
“changed”: true, #xuegod64上的文件名字一样,但是内容变,就会重新下载。执行成功,且发生改变,那么显示黄色

(6)yum模块linux平台软件包管理。
yum模块可以提供的status状态: latest ,present,installed #这3个代表安装;removed, absent #后面2个是卸载
例子:安装php软件
[root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m yum -a “name=httpd state=latest”

(7)cron模块远程主机crontab配置。
例如:增加每30分钟执行ls /tmp
[root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m cron -a “name=‘list dir’ minute=‘*/30’ job=‘ls /tmp’”
在xuegod63上查看
[root@xuegod63 ~]# crontab -l
#Ansible: list dir
*/30 * * * * ls /tmp

(8)service模块远程主机系统服务管理。
service模块常用参数:
(1)、name参数:此参数用于指定需要操作的服务名称,比如 nginx,httpd。
(2)、state参数:此参数用于指定服务的状态,比如,我们想要启动远程主机中的httpd,则可以将 state 的值设置为 started;如果想要停止远程主机中的服务,则可以将 state 的值设置为 stopped。此参数的可用值有 started、stopped、restarted(重启)、reloaded。
enabled参数:此参数用于指定是否将服务设置为开机 启动项,设置为 yes 表示将对应服务设置为开机启动,设置为 no 表示不会开机启动。
注:想使用service模块启动服务,被启动的服务,必须可以使用service 命令启动或关闭

例如:远程启动apache服务
[root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m service -a “name=httpd state=restarted”

(9)sysctl模块远程主机sysctl配置。
例:开启路由转发功能
[root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m sysctl -a “name=net.ipv4.ip_forward value=1 reload=yes”
验证:
[root@xuegod63 ~]# cat /proc/sys/net/ipv4/ip_forward
1

(10)user模块远程主机用户管理
例如:
[root@xuegod63 ~]# ansible -i /etc/ansible/hosts web-servers -m user -a “name=xuegod6 state=present”

present [ˈpreznt] 目前

验证:
[root@xuegod63 ~]# id xuegod6
uid=1001(xuegod6) gid=1001(xuegod6) 组=1001(xuegod6)

实战-使用Playbook批量部署多台LAMP环境

1.在playbooks 中定义任务:
- name: task description #任务描述信息
module_name: module_args #需要使用的模块名字: 模块参数
2、ansible-playbook 执行 命令:
[root@xuegod63 ~]# ansible-playbook site.yml
playbook是由一个或多个"play"组成的列表。play的主要功能在于将事先归为一组的主机装扮成事先通过ansible中的task定义好的角色。
github上提供了大量的实例供大家参考 https://github.com/ansible/ansible-examples

3、 实战-使用Playbook批量部署多台LAMP环境
Playbook常用文件夹作用:
files:存放需要同步到异地服务器的源码文件及配置文件;
handlers:当服务的配置文件发生变化时需要进行的操作,比如:重启服务,重新加载配置文件; ['hændləz] 处理程序
meta:角色定义,可留空; ['metə] 元
tasks:需要进行的执行的任务; #任务
templates:用于执行lamp安装的模板文件,一般为脚本; ['templɪts] 模板
vars:本次安装定义的变量

使用playbook 批量安装部署多台 lamp 环境

(1) 实战-使用Playbook批量部署多台LAMP环境

首先,我们可以在ansible服务器上安装LAMP环境,然后,再将配置文件通过ansible拷贝到远程主机上
第一步:安装httpd软件
[root@xuegod63 ~]# yum install httpd -y
第二部:安装MySQL
root@xuegod63 ~]# yum install mariadb-server mariadb -y
[root@xuegod63 ~]# mkdir -p /mydata/data #创建目录作为数据存放的位置
[root@xuegod63 ~]# chown -R mysql:mysql /mydata/
[root@xuegod63 ~]# vim /etc/my.cnf #改变数据存放目录
改:2 datadir=/var/lib/mysql
为:2 datadir=/mydata/data
[root@xuegod63 ~]# systemctl start mariadb
第三步:安装PHP和php-mysql模块
[root@xuegod63 ~]# yum install php php-mysql -y
第四步:提供php的测试页
[root@xuegod63 ~]# vim /var/www/html/index.php

<?php phpinfo(); ?>

启动httpd服务,在浏览器中访问
[root@xuegod63 ~]# systemctl restart httpd
[root@xuegod63 ~]# iptables -F
测试:http://192.168.1.63/index.php
确保已经出现上面的测试页,而且,要看到MySQL已经被整合进来了,才能进行下一步操作

(2)定义组名:
[root@xuegod63 ~]# vim /etc/ansible/hosts #还使用之前定义好的,这里不用修改
[web-servers]
192.168.1.63
192.168.1.64

然后,将公钥信息复制到被控制节点,ansible和两个节点间通过ssh进行连接。下面3个命令之前已经做过,不用执行了。
[root@xuegod63 ~]# ssh-keygen
[root@xuegod63 ~]# ssh-copy-id root@192.168.1.63
[root@xuegod63 ~]# ssh-copy-id root@192.168.1.64

29.4.2 使用playbook创建一个LAMP构建的任务
1、创建相关文件
[root@xuegod63 ~]# mkdir -pv /etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,meta,default,handlers}
我们将上面搭建成功的LAMP环境的httpd和MySQL的配置文件拷贝到对应目录下
[root@xuegod63 ~]# cd /etc/ansible/
[root@xuegod63 ~]# cp /etc/httpd/conf/httpd.conf lamp/roles/httpd/files/
[root@xuegod63 ~]# cp /etc/my.cnf lamp/roles/mysql/files/

写prepare(前期准备)角色的playbooks
[root@xuegod63 ansible]# vim lamp/roles/prepare/tasks/main.yml #复制以下红色内容到文件中,配置好yum源
(- name: delete yum config
shell: rm -rf /etc/yum.repos.d/* #删除原有的yum配置文件

  • name: provide yumrepo file
    shell: wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo #下载新的yum配置文件
  • name: clean the yum repo
    shell: yum clean all #清除原有的yum缓存信息
  • name: clean the iptables
    shell: iptables -F #清除原有防火墙规则,不然后可能上不了网
    )如果配置好yum 源 这个可以不写

(2) 构建httpd 的任务
一般写用pythone 写的软件在编辑配置文件的时候要注意空格

[root@xuegod63 roles]# cd /etc/ansible/lamp/roles
[root@xuegod63 roles]# mv /var/www/html/index.php httpd/files/
[root@xuegod63 roles]# vim httpd/tasks/main.yml #将以下内容复制到文件中

  • name: web server install
    yum: name=httpd state=present #安装httpd服务

  • name: provide test page
    copy: src=index.php dest=/var/www/html #提供测试页

  • name: delete apache config
    shell: rm -rf /etc/httpd/conf/httpd.conf #删除原有的apache配置文件,如果不删除,下面的copy任务是不会执行的,因为当源文件httpd.conf和目标文件一样时,copy命令是不执行的。如果copy命令不执行,那么notify将不调用handler。

  • name: provide configuration file
    copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf #提供httpd的配置文件
    notify: restart httpd #当前面的copy复制成功后,通过notify通知名字为restart httpd的handlers运行。
    notify: 这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时,每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。

    handlers概述:
    Handlers 也是一些 task 的列表,通过名字来引用,它们和一般的 task 并没有什么区别。
    Handlers 是由通知者进行notify, 如果没有被 notify,handlers 不会执行。
    不管有多少个通知者进行了notify,等到 play 中的所有 task 执行完成之后,handlers 也只会被执行一次。
    Handlers 最佳的应用场景是用来重启服务,或者触发系统重启操作.除此以外很少用到了。

(3)部署我们的mariadb数据库
创建MySQL服务的任务,需要安装MySQL服务,改变属主信息,启动MySQL
[root@xuegod63 roles]# cd /etc/ansible/lamp/roles
[root@xuegod63 roles]# vim mysql/tasks/main.yml

  • name: install the mysql
    yum: name=mariadb-server state=present #安装mysql服务
  • name: mkdir date directory
    shell: mkdir -p /mydata/data #创建挂载点目录
  • name: provide configration file
    copy: src=my.cnf dest=/etc/my.cnf #提供mysql的配置文件
  • name: chage the owner
    shell: chown -R mysql:mysql /mydata/* #更改属主和属组
  • name: start mariadb
    service: name=mariadb enabled=yes state=started #启动mysql服务

(4)构建PHP的任务
[root@xuegod63 roles]# vim php/tasks/main.yml

  • name: install php
    yum: name=php state=present #安装php
  • name: install php-mysql
    yum: name=php-mysql state=present #安装php与mysql交互的插件

(5)定义整个的任务
[root@xuegod63 roles]# cd /etc/ansible/lamp/roles
[root@xuegod63 roles]# vim site.yml #写入以下内容

  • name: LAMP build
    remote_user: root
    hosts: web-servers
    roles:
    • prepare
    • mysql
    • php
  • httpd

注:所有yml的配置文件中,空格必须严格对齐

(6)
开始部署:
[root@xuegod63 roles]# ansible-playbook -i /etc/ansible/hosts /etc/ansible/lamp/roles/site.yml
然后,在浏览器中访问这两台节点主机,可以直接访问成功。
http://192.168.1.63/index.php

注:
1、默认情况下,首次登陆一台服务器,系统会提示是否要记住对端的指纹,用ansible也会这样,这样会导致需要手工输入yes或no,ansible 才可以往下执行。如需避免这种情况,需要在 /etc/ansible/ansible.cfg 文件中设置 host_key_checking = False
例1:
[root@xuegod63 roles]# rm -rf /root/.ssh/known_hosts
[root@xuegod63 roles]# ansible-playbook -i /etc/ansible/hosts ./site.yml #发现需要输入yes,来保存对端的指纹
解决:
[root@xuegod63 roles]# vim /etc/ansible/ansible.cfg
改:62 #host_key_checking = False #就是把前面的#号去了
为:host_key_checking = False
[root@xuegod63 roles]# rm -rf /root/.ssh/known_hosts
[root@xuegod63 roles]# ansible-playbook -i /etc/ansible/hosts ./site.yml #发现不需要输入yes,可以自动安装了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值