利用Ansible自动化运维工具一键部署Apache服务

简介:

ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。
ansible是基于模块工作的,本身没有批量部署的能力。真正具有批量部署的是ansible所运行的模块,ansible只是提供一种框架。

1
2

1.安装部署ansible

1.下载并安装ansible

安装包:

ansible-2.7.8-1.el7.noarch.rpm
ansible-tower-setup-bundle-3.4.2-1.el7.tar.gz
libtomcrypt-1.17-25.el7.x86_64.rpm
libtommath-0.42.0-5.el7.x86_64.rpm
python2-crypto-2.6.1-13.el7.x86_64.rpm
python2-jmespath-0.9.0-1.el7.noarch.rpm
python-httplib2-0.9.2-0.1.el7.noarch.rpm
python-keyczar-0.71c-2.el7.noarch.rpm
python-paramiko-2.1.1-0.9.el7.noarch.rpm
sshpass-1.06-1.el7.x86_64.rpm

#1.在官网上下载
[root@server1 ~]# ls
ansible
[root@server1 ~]# cd ansible/
[root@server1 ansible]# ls
在这里插入图片描述

#2.安装
[root@server1 ansible]# yum install -y *.rpm

2.定义主机组变量

[root@server1 ~]# cd /etc/ansible/
[root@server1 ansible]# ls
ansible.cfg  hosts  roles
#主文件
[root@server1 ansible]# ll ansible.cfg 
-rw-r--r-- 1 root root 20277 Feb 22 07:04 ansible.cfg
#定义主机组变量
[root@server1 ansible]# ll hosts 
-rw-r--r-- 1 root root 1016 Feb 22 07:04 hosts
#定义角色
[root@server1 ansible]# cd roles/
[root@server1 roles]# ls
    [root@server1 ansible]# pwd
    /etc/ansible
    #编写文件;分组是为了在外层直接调用组名

[root@server1 ansible]# vim hosts 

#######################
[web]            #组名
server1          #主机名

[db]             #组名
server2          #主机名

2.常用模块

1.ping模块

先做免密:(默认为root用户
[root@server1 ansible]# ssh-keygen

在这里插入图片描述
[root@server1 ansible]# ssh-copy-id server1
在这里插入图片描述[root@server1 ansible]# ansible server1 -m ping
在这里插入图片描述
[root@server1 ansible]# ansible server* -m ping
在这里插入图片描述
2.user模块

(1).创建用户

#创建用户
[root@server1 ansible]# ansible all -m user -a “name=admin password=westos”

在这里插入图片描述[root@server1 ansible]# passwd admin
在这里插入图片描述
[root@server1 ansible]# ssh-copy-id admin@server1
在这里插入图片描述
[root@server1 ~]# ll /etc/sudoers
-r–r-----. 1 root root 3907 Jul 19 2016 /etc/sudoers
[root@server1 ~]# chmod 755 /etc/sudoers
[root@server1 ~]# ll /etc/sudoers
-rwxr-xr-x. 1 root root 3907 Jul 19 2016 /etc/sudoers
[root@server1 ~]# vim /etc/sudoers
#################
admin ALL=(ALL) NOPASSWD: ALL #允许admin用户免密执行任意路径下的任意命令
在这里插入图片描述
#以admin用户身份远程登陆; -b表示切换为超户执行以下命令,all表示所有主机
[root@server1 ~]# ansible all -m ping -u admin -b
在这里插入图片描述
#1.安装apache
[root@server1 ~]# ansible server2 -u admin -b -m yum -a “name=httpd state=present”
在这里插入图片描述
#2.启动apache
[root@server1 ~]# ansible server2 -u admin -b -m service -a “name=httpd state=started”
在这里插入图片描述
#3.更改发布页面;copy拷贝文件,src表示源地址,dest表示目标地址(将当前目录下的index.html拷贝到/var/www/html/index.html文件中)
[root@server1 ~]# ansible server2 -u admin -b -m copy -a “src=index.html dest=/var/www/html/index.html”
在这里插入图片描述
[root@foundation66 ~]# curl 172.25.66.2
westos.org
部署数据库:

#1.安装数据库
[root@server1 ~]# ansible server2 -m yum -a “name=mariadb-server state=present”
在这里插入图片描述#2.开启数据库
[root@server1 ~]# ansible server2 -m service -a “name=mariadb state=started”
在这里插入图片描述
#4.安装依赖包
[root@server1 ~]# ansible server2 -m yum -a “name=MySQL-python state=present”
在这里插入图片描述
#5.重新创建数据库用户
[root@server1 ~]# ansible server2 -m mysql_user -a “name=lee password=westos priv=test.*:ALL state=present”
在这里插入图片描述
[root@server2 ~]# mysql
MariaDB [(none)]> use mysql;
MariaDB [mysql]> select * from user;
在这里插入图片描述
3.整合模块

部署apache:

#先卸载之前安装好的apache
[root@server2 ~]# rpm -e httpd
1.创建目录

[root@server1 ~]# cd /etc/ansible/
[root@server1 ansible]# ls
ansible.cfg hosts roles
[root@server1 ansible]# mkdir playbooks
[root@server1 ansible]# cd playbooks/
[root@server1 playbooks]# ls
[root@server1 playbooks]# mkdir httpd
[root@server1 playbooks]# cd httpd
[root@server1 httpd]# ls

2.编写yml文件

[root@server1 httpd]# vim httpd.yml
#####################

#部署apache

  • hosts: server2
    remote_user: root
    tasks:

    • name: install httpd
      yum: name=httpd state=present

    • name: config httpd
      copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify: restart httpd

    • name: start httpd
      service: name=httpd state=started

    handlers:

    • name: restart httpd
      service: name=httpd state=restarted

在这里插入图片描述
#检测语法
[root@server1 httpd]# ansible-playbook httpd.yml --syntax-check

playbook: httpd.yml

#检查生效的主机
[root@server1 httpd]# ansible-playbook httpd.yml --list-hosts
在这里插入图片描述#推送
[root@server1 httpd]# ansible-playbook httpd.yml
在这里插入图片描述
[root@server1 httpd]# ansible-playbook httpd.yml --start-at-task=“start httpd”
在这里插入图片描述
#传输配置文件
[root@server2 ~]# cd /etc/httpd/conf/
[root@server2 conf]# ls
httpd.conf magic
[root@server3 conf]# scp httpd.conf server1:/etc/ansible/playbooks/httpd
[root@server1 httpd]# ls
httpd.conf httpd.yml
[root@server1 httpd]# md5sum httpd.conf
f5e7449c0f17bc856e86011cb5d152ba httpd.conf

[root@server2 ~]# cd /etc/httpd/conf/
[root@server2 conf]# ls
httpd.conf magic
[root@server2 conf]# md5sum httpd.conf
f5e7449c0f17bc856e86011cb[root@server1 httpd]# ls
httpd.conf httpd.yml
[root@server1 httpd]# vim httpd.conf
####################
42 Listen 8080 #修改端口
5d152ba httpd.conf
在这里插入图片描述
[root@server1 httpd]# pwd
/etc/ansible/playbooks/httpd
[root@server1 httpd]# md5sum httpd.conf
04e9239e7bd5d5b9b85864226d60eee5 httpd.conf

[root@server2 conf]# pwd
/etc/httpd/conf
[root@server2 conf]# md5sum httpd.conf
f5e7449c0f17bc856e86011cb5d152ba httpd.conf
#再次推送
[root@server1 httpd]# ansible-playbook httpd.yml
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值