使用ansible的playbook实现自动化安装httpd

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

本机安装LNMP环境

[root@jumpserver ~]#yum install httpd mariadb-server mariadb php php-mysql
[root@jumpserver ~]#mkdir -p /mydata/data
[root@jumpserver ~]#chown -R mysql.mysql /mydata/data
[root@jumpserver ~]#vim /etc/my.cnf
datadir=/mydata/data  
[root@jumpserver ~]#systemctl start mariadb
[root@jumpserver ~]#vim /var/www/html/index.php
<?php
  phpinfo();                                                                                           
?>
[root@jumpserver ~]#systemctl restart httpd
[root@jumpserver ~]#iptables -F

测试页面
在这里插入图片描述
hosts主机列表

[root@jumpserver ~]#vim /etc/ansible/hosts 
[web-servers]
172.18.5.51
172.18.5.52

使用playbook创建一个LAMP构建的任务

创建相关文件

[root@xuegod63 ~]# mkdir -pv /etc/ansible/lamp/roles/{prepare,httpd,mysql,php}/{tasks,files,templates,vars,meta,default,handlers}
[root@jumpserver ~]#tree /etc/ansible/
/etc/ansible/
├── ansible.cfg
├── hosts
└── lamp
    └── roles
        ├── httpd
        │   ├── default
        │   ├── files
        │   │   ├── httpd.conf
        │   │   └── index.php
        │   ├── handlers
        │   │   └── main.yml
        │   ├── meta
        │   ├── tasks
        │   │   └── main.yml
        │   ├── templates
        │   └── vars
        ├── mysql
        │   ├── default
        │   ├── files
        │   │   └── my.cnf
        │   ├── handlers
        │   ├── meta
        │   ├── tasks
        │   │   └── main.yml
        │   ├── templates
        │   └── vars
        ├── php
        │   ├── default
        │   ├── files
        │   ├── handlers
        │   ├── meta
        │   ├── tasks
        │   │   └── main.yml
        │   ├── templates
        │   └── vars
        ├── prepare
        │   ├── default
        │   ├── files
        │   ├── handlers
        │   ├── meta
        │   ├── tasks
        │   │   └── main.yml
        │   ├── templates
        │   └── vars
        └── site.yml

34 directories, 11 files

准备事先配置好的httpd和php配置文件

[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/

准备yum源

[root@xuegod63 ansible]# vim lamp/roles/prepare/tasks/main.yml
    - name: install wget
      yum: name=wget state=present
    - 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                 #清除原有防火墙规则,不然后可能上不了网

构建httpd的任务

[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运行,直接写个service来实现重启也可以

扩展:notify和handlers notify [ˈnəʊtɪfaɪ] 通知 notify:
这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时,每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。
在notify中列出的操作称为handler,也即notify中调用handler中定义的操作。
---- name: test.yml just for test
hosts: testserver
vars:
region: ap-southeast-1
tasks:
- name: template configuration
file template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache
handlers:
- name: restart memcached
service: name=memcached state=restarted
- name: restart apache
service: name=apache state=restarted handlers概述: Handlers 也是一些 task 的列表,通过名字来引用,它们和一般的 task 并没有什么区别。 Handlers 是由通知者进行notify,
如果没有被 notify,handlers 不会执行。 不管有多少个通知者进行了notify,等到 play 中的所有 task
执行完成之后,handlers 也只会被执行一次。 Handlers
最佳的应用场景是用来重启服务,或者触发系统重启操作.除此以外很少用到了。

构建httpd的handlers

[root@xuegod63 roles]# vim httpd/handlers/main.yml
    - name: restart httpd
      service: name=httpd enabled=yes state=restarted

创建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 data directory
      shell: mkdir -p /mydata/data                        #创建挂载点目录
    - name: provide configration file
      copy: src=my.cnf dest=/etc/my.cnf                   #提供mysql的配置文件
    - name: change the owner
      shell: chown -R mysql:mysql /mydata/*              #更改属主和属组
    - name: start mariadb
      service: name=mariadb enabled=yes state=started    #启动mysql服务

构建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交互的插件

定义整个的任务

[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

执行ansible-playbook,观察过程,而且重复执行结果是一样的

[root@jumpserver ansible]#ansible-playbook -i /etc/ansible/hosts /etc/ansible/lamp/roles/site.yml
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [LAMP build] *************************************************************************************

TASK [Gathering Facts] ********************************************************************************
ok: [172.18.5.52]
ok: [172.18.5.51]

TASK [prepare : install wget] *************************************************************************
ok: [172.18.5.51]
ok: [172.18.5.52]

TASK [prepare : delete yum config] ********************************************************************
changed: [172.18.5.52]
changed: [172.18.5.51]

TASK [prepare : provide yumrepo file] *****************************************************************
changed: [172.18.5.51]
changed: [172.18.5.52]

TASK [prepare : clean the yum repo] *******************************************************************
changed: [172.18.5.51]
changed: [172.18.5.52]

TASK [prepare : clean the iptables] *******************************************************************
changed: [172.18.5.51]
changed: [172.18.5.52]

TASK [install the mysql] ******************************************************************************
changed: [172.18.5.51]
changed: [172.18.5.52]

TASK [mysql : mkdir data directory] *******************************************************************
changed: [172.18.5.51]
changed: [172.18.5.52]

TASK [mysql : provide configration file] **************************************************************
changed: [172.18.5.52]
changed: [172.18.5.51]

TASK [mysql : change the owner] ***********************************************************************
changed: [172.18.5.52]
changed: [172.18.5.51]

TASK [mysql : start mariadb] **************************************************************************
changed: [172.18.5.51]
changed: [172.18.5.52]

TASK [install php] ************************************************************************************
changed: [172.18.5.52]
changed: [172.18.5.51]

TASK [install php-mysql] ******************************************************************************
changed: [172.18.5.52]
changed: [172.18.5.51]

TASK [httpd : web server install] *********************************************************************
ok: [172.18.5.51]
ok: [172.18.5.52]

TASK [httpd : provide test page] **********************************************************************
changed: [172.18.5.52]
changed: [172.18.5.51]

TASK [httpd : delete apache config] *******************************************************************
changed: [172.18.5.51]
changed: [172.18.5.52]

TASK [httpd : provide configuration file] *************************************************************
changed: [172.18.5.51]
changed: [172.18.5.52]

RUNNING HANDLER [restart httpd] ***********************************************************************
changed: [172.18.5.52]
changed: [172.18.5.51]

PLAY RECAP ********************************************************************************************
172.18.5.51                : ok=18   changed=15   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.5.52                : ok=18   changed=15   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

被控节点测试页面
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值