ansible-playbook一键化部署apache服务

Playbook(任务剧本)

编排定义ansible任务集的配置文件,由ansible顺序依次执行,通常是JSON格式的* YML文件
playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的模式,可作为一个适合部署复杂应用程序的基础。Playbook可以定制配置,可以按照指定的操作步骤有序执行,支持同步和异步方式。

实战:playbook一键化部署apache服务

1.配置playbook.yml文件

[devops@server4 ansible]$ vim playbook.yml
---
- hosts: webservers
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: latest

    - name: start httpd
      service:
        name: httpd
        state: started

ansible-playbook一键化部署apache服务
2.查看playbook.yml部署的任务

[devops@server4 ansible]$ ansible-playbook playbook.yml --list-tasks

ansible-playbook一键化部署apache服务
3.语法检测

[devops@server4 ansible]$ ansible-playbook playbook.yml --syntax-check

ansible-playbook一键化部署apache服务
4.推送

[devops@server4 ansible]$ ansible-playbook playbook.yml 

ansible-playbook一键化部署apache服务
5.修改测试页

[devops@server4 ansible]$ vim playbook.yml
---
- hosts: webservers
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: latest
    - name: create index.html
      copy:
        content: "www.westos.org\n"
        dest: /var/www/html/index.html

    - name: start httpd
      service:
        name: httpd
        state: started

6.语法检测

[devops@server4 ansible]$ ansible-playbook playbook.yml --syntax-check

7.推送

[devops@server4 ansible]$ ansible-playbook playbook.yml 

ansible-playbook一键化部署apache服务
8.测试
ansible-playbook一键化部署apache服务
9.推送httpd.conf配置文件,开机自启

[devops@server4 ansible]$ vim playbook.yml 
---
- hosts: webservers
  tasks:
    - name: install httpd
      yum:
        name: httpd
        state: latest
    - name: create index.html
      copy:
        content: "www.westos.org\n"
        dest: /var/www/html/index.html

    - name: configure httpd
      copy:
        src: files/httpd.conf
        dest: /etc/httpd/conf/httpd.conf
        owner: root
        group: root
        mode: 644
      notify: restart httpd

    - name: start httpd
      service:
        name: httpd
        state: started
        enabled: true

10.将http.conf配置文件放到files目录下

[devops@server4 ansible]$ mkdir files
[devops@server4 ansible]$ cd files/
[devops@server4 files]$ scp server5:/etc/httpd/conf/httpd.conf .
httpd.conf                                             100%   11KB  11.5KB/s   00:00 

11.语法检测并推送

[devops@server4 ansible]$ ansible-playbook playbook.yml --syntax-check

playbook: playbook.yml
[devops@server4 ansible]$ ansible-playbook playbook.yml

ansible-playbook一键化部署apache服务
12.查看是否同步过去

[devops@server4 files]$ md5sum httpd.conf
f5e7449c0f17bc856e86011cb5d152ba  httpd.conf
[root@server5 ~]# md5sum /etc/httpd/conf/httpd.conf 
f5e7449c0f17bc856e86011cb5d152ba  /etc/httpd/conf/httpd.conf

13.server2是否开启httpd,并且是开机自启动
ansible-playbook一键化部署apache服务
14.火墙允许httpd服务

[devops@server4 ansible]$ vim playbook.yml 
    - name: configure firewalld
      firewalld:
        service: http
        state: enabled
        permanent: yes
        immediate: yes

15.语法检测并部署

[devops@server4 ansible]$ ansible-playbook playbook.yml --syntax-check

playbook: playbook.yml
[devops@server4 ansible]$ ansible-playbook playbook.yml 

13.不同虚拟机显示不同的测试页内容

[devops@server4 ansible]$ vim playbook.yml

ansible-playbook一键化部署apache服务

[devops@server4 ansible]$ ansible-playbook playbook.yml
[devops@server4 ansible]$ curl server5
server5
[devops@server4 ansible]$ curl server6
server6

16.定义httpd端口

[devops@server4 ansible]$ vim playbook.yml

ansible-playbook一键化部署apache服务

[devops@server4 ansible]$ vim files/httpd.conf
Listen {{ http_port }}
[devops@server4 ansible]$ ansible-playbook playbook.yml

17.检测端口号
ansible-playbook一键化部署apache服务
18.修改apache服务的端口为8080
1)建立模版目录templates

[devops@server4 ansible]$ mkdir templates

2)将模版放到该目录里

[devops@server4 ansible]$ mv files/httpd.conf.j2 templates/

3)修改playbook.yml文件

[devops@server4 ansible]$ vim playbook.yml

ansible-playbook一键化部署apache服务
4)推送

[devops@server4 ansible]$ ansible-playbook playbook.yml

5)查看配置文件是否修改成功
ansible-playbook一键化部署apache服务
ansible-playbook一键化部署apache服务

上一篇:Nginx学习笔记(八)——Nginx的盗链与防盗链


下一篇:获得identity Server4当前用户的角色