ansible和apache练习

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

实验环境:

主机 os 软件 ip
ansible节点 centos7.6 安装ansible 172.16.2.132
ansible-host节点1 centos7.6 172.16.2.137
ansible-host节点2 centos7.6 172.16.2.138

*在所有节点实现相互之间ssh key验证

实验设想

通过ansible 自动部署httpd服务,达到以下效果:

  • ansible-host节点1:172.16.2.137

建立httpd服务器,要求提供两个基于名称的虚拟主机:
(1)www.X.com,页面文件目录为/web/vhosts/x;错误日志为/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access
(2)www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名

  • ansible-host节点2:172.16.2.138

建立httpd服务器,要求提供两个基于名称的虚拟主机:
(1)www.a.com,页面文件目录为/web/vhosts/a;错误日志为/var/log/httpd/a.err,访问日志为/var/log/httpd/a.access
(2)www.b.com,页面文件目录为/web/vhosts/b;错误日志为 /var/log/httpd/bwww2.err,访问日志为/var/log/httpd/b.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名

实现步骤

1.使用epel源 安装ansible

[root@node2 ~]# yum install ansible -y

2.修改主机hosts文件

vim /etc/ansible/hosts
[httpdservers]
172.16.2.137
172.16.2.138

3.基于role创建如下目录及文件

[root@node2 playbook]# tree
.
├── host_vars
│   ├── 172.16.2.137
│   └── 172.16.2.138
├── http_role.yml
└── roles
    └── httpd
        ├── files
        ├── handlers
        │   ├── main.yml
        │   └── restart.yml
        ├── tasks
        │   ├── config.yml
        │   ├── index1.yml
        │   ├── index2.yml
        │   ├── install.yml
        │   ├── main.yml
        │   ├── service.yml
        │   └── webdir.yml
        ├── templates
        │   ├── index1.html.j2
        │   ├── index2.html.j2
        │   └── test.conf.j2
        └── vars

4.准备需要的templates文件:

配置httpd模板

[root@node2 templates]# vim test.conf.j2
<virtualhost *:80>
documentroot /web/vhosts/{{ webdir1 }}  //配置文件个性化之处使用变量处理
servername {{ vhost1 }}
<Directory "/web/vhosts/{{ webdir1 }}">
    Require all granted
</Directory>
CustomLog "/var/log/httpd/{{ accesslog1 }}" combined
ErrorLog "/var/log/httpd/{{ errorlog1 }}"
</virtualhost>

<virtualhost *:80>
documentroot /web/vhosts/{{ webdir2 }}
servername {{ vhost2 }}
<Directory "/web/vhosts/{{ webdir2 }}">
    Require all granted
</Directory>
CustomLog "/var/log/httpd/{{ accesslog2 }}" combined
ErrorLog "/var/log/httpd/{{ errorlog2 }}"
</virtualhost>

配置index.html模板

vim index1.html.j2
{{ vhost1 }}

vim index2.html.j2
{{ vhost2 }}

5.编辑task

[root@node2 tasks]# vim install.yml
- name: install    //安装服务
 yum: name=httpd
[root@node2 tasks]# vim config.yml
- name: config    //复制conf到目标主机
 template: src=test.conf.j2 dest=/etc/httpd/conf.d/test.conf
 notify: restart  //关联handles
[root@node2 tasks]# vim webdir.yml
- name: webdir1   //创建网站目录
 file: path=//web/vhosts/{{webdir1}} state=directory
- name: webdir2
 file: path=//web/vhosts/{{webdir2}} state=directory
[root@node2 tasks]# vim index1.yml
- name: index1    //复制index文件
 template: src=index1.html.j2 dest=/web/vhosts/{{webdir1}}/index.html
[root@node2 tasks]# vim index2.yml
- name: index2
 template: src=index2.html.j2 dest=/web/vhosts/{{webdir2}}/index.html
[root@node2 tasks]# vim service.yml
- name: service   //启动服务
 service: name=httpd state=started enabled=yes
 
[root@node2 tasks]# vim main.yml  
- include: install.yml
- include: config.yml
- include: webdir.yml
- include: index1.yml
- include: index2.yml
- include: service.yml

6.编辑handles

[root@node2 handlers]# vim restart.yml
- name: restart
  service: name=httpd state=restarted
  
[root@node2 handlers]# vim main.yml
- include: restart.yml

7.编辑基于各主机host_vars

*注意host_vars目录的位置

[root@node2 host_vars]# vim 172.16.2.137
webdir1: x
webdir2: y
errorlog1: "e.err"
errorlog2: "www2.err"
accesslog1: "x.access"
accesslog2: "y.access"
vhost1: "www.X.com"
vhost2: "www.Y.com"

[root@node2 host_vars]# vim 172.16.2.138
webdir1: a
webdir2: b
errorlog1: "ae.err"
errorlog2: "bwww2.err"
accesslog1: "a.access"
accesslog2: "b.access"
vhost1: "www.A.com"
vhost2: "www.B.com"

8.编辑基于role的yml

[root@node2 playbook]# cat http_role.yml
- hosts: httpdservers

  roles:
    - httpd

9.检查配置

[root@node2 playbook]# ansible-playbook -C  http_role.yml

PLAY [httpdservers] *****************************************************************************************

TASK [Gathering Facts] **************************************************************************************
ok: [172.16.2.137]
ok: [172.16.2.138]

TASK [httpd : install] **************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

TASK [httpd : config] ***************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

TASK [httpd : webdir1] **************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

TASK [httpd : webdir2] **************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

TASK [httpd : index1] ***************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

TASK [httpd : index2] ***************************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

TASK [httpd : service] **************************************************************************************
changed: [172.16.2.138]
changed: [172.16.2.137]

RUNNING HANDLER [httpd : restart] ***************************************************************************
changed: [172.16.2.137]
changed: [172.16.2.138]

PLAY RECAP **************************************************************************************************
172.16.2.137               : ok=9    changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
172.16.2.138               : ok=9    changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

10.正式部署

[root@node2 playbook]# ansible-playbook  http_role.yml

11.验证

[root@node2 templates]# cat /etc/hosts  //查看管理主机的hosts文件
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.2.137    www.X.com www.Y.com   //同一个ip对应两个不同的虚拟主机地址
172.16.2.138    www.a.com www.b.com


[root@node2 templates]# curl www.X.com  使用curl访问
www.X.com
[root@node2 templates]# curl www.Y.com
www.Y.com
[root@node2 templates]# curl www.a.com
www.A.com
[root@node2 templates]# curl www.b.com
www.B.com

建立httpd服务器,要求提供两个基于名称的虚拟主机

实验环境:

服务器端: centos7.6 ip:172.16.2.137 安装httpd
客户端: centos7.6 ip:172.16.2.132 修改hosts文件增加两个虚拟主机ip

实验设想

建立httpd服务器,要求提供两个基于名称的虚拟主机:
(1)www.X.com,页面文件目录为/web/vhosts/x;错误日志为/var/log/httpd/x.err,访问日志为/var/log/httpd/x.access
(2)www.Y.com,页面文件目录为/web/vhosts/y;错误日志为 /var/log/httpd/www2.err,访问日志为/var/log/httpd/y.access
(3)为两个虚拟主机建立各自的主页文件index.html,内容分别为其对应的主机名

实验步骤

1.安装服务

 yum install -y httpd

2.编译配置文档

[root@node3 etc]# vim /etc/httpd/conf.d/test.conf
<virtualhost *:80>
documentroot /web/vhosts/x   //网站主目录
servername www.X.com        //设置域名
<Directory "/web/vhosts/x">
    Require all granted     //权限
</Directory>
CustomLog "/var/log/httpd/x.access" combined  //设置log
ErrorLog "/var/log/httpd/e.err"
</virtualhost>

<virtualhost *:80>
documentroot /web/vhosts/y
servername www.Y.com
<Directory "/web/vhosts/y">
    Require all granted
</Directory>
CustomLog "/var/log/httpd/y.access" combined
ErrorLog "/var/log/httpd/www2.err"
</virtualhost>

3.创建目录:

[root@node3 etc]# mkdir /web/vhosts/{x,y}

4.创建主页文件:

[root@node3 etc]# cat /web/vhosts/x/index.html
www.X.com
[root@node3 etc]# cat /web/vhosts/y/index.html
www.Y.com

5.启动服务:

systemctl start httpd

6.验证:

[root@node2 templates]# cat /etc/hosts //查看客户端host是文件
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.2.137    www.X.com www.Y.com  //同一个ip对应两个不同的虚拟主机地址
[root@node2 templates]# curl www.X.com
www.X.com   
[root@node2 templates]# curl www.Y.com
www.Y.com   
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353

推荐阅读更多精彩内容