template模块安装配置httpd服务器
基本环境
名称 | 主机名 | P地址 |
---|---|---|
ansible服务器 | ansible | 20.0.0.10 |
节点1 | server1 | 20.0.0.20 |
节点2 | server2 | 20.0.0.30 |
# 在之前的部署基础上只修改了hosts文件
[root@ansible ~]# vim /etc/ansible/hosts
[node1]
20.0.0.20
[node2]
20.0.0.30
在节点1上面安装并配置httpd服务
1、获得httpd的配资文件
[root@server2 ~]# yum -y install httpd
、
[root@ansible apache]# scp root@20.0.0.30:/etc/httpd/conf/httpd.conf /root/apache/
[root@ansible apache]# mv httpd.conf httpd.conf.j2
2、修改hosts文件
[root@ansible apache]# vim /etc/ansible/hosts
[node1]
20.0.0.20 server_port=20.0.0.20:80 server_name="www.abc.com:80"
[node2]
20.0.0.30
3、修改httpd配置文件
[root@ansible apache]# vim httpd.conf.j2
Listen {{server_port}}
ServerName {{server_name}}
4、创建yaml文件
[root@ansible apache]# vim apache.yaml
- hosts: node1
remote_user: root
vars:
- abcd: httpd
tasks:
- name: yum httpd
yum: name={{abcd}} state=latest
- name: httpd conf
template: src=/root/apache/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- httpd restart
- name: httpd start
service: name={{abcd}} enabled=true state=started
handlers:
- name: httpd restart
service: name={{abcd}} state=restarted
3、检查语法
[root@ansible apache]# ansible-playbook apache.yaml --syntax-check
playbook: apache.yaml
4、执行yaml文件
[root@ansible apache]# ls
apache.yaml httpd.conf.j2
[root@ansible apache]# ansible-playbook apache.yaml
PLAY [node1] ************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [20.0.0.20]
TASK [yum httpd] ********************************************************************************************************
changed: [20.0.0.20]
TASK [httpd conf] *******************************************************************************************************
changed: [20.0.0.20]
TASK [httpd start] ******************************************************************************************************
changed: [20.0.0.20]
RUNNING HANDLER [httpd restart] *****************************************************************************************
changed: [20.0.0.20]
PLAY RECAP **************************************************************************************************************
20.0.0.20 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@ansible apache]#
5、验证
节点1验证
[root@server1 ~]# rpm -q httpd
httpd-2.4.6-67.el7.centos.x86_64
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf
找到ServerName和Listen看后面参数是否变化
ServerName www.abc.com:80
Listen 20.0.0.20:80
测试
tag标签模块
如果我们只想执行其中的某一个task或多个task时就可以使用tags标签功能
1、执行指定标签对应的命令
[root@ansible apache]# vim tag.yaml
- hosts: node1
remote_user: root
tasks:
- name: copy
copy: src=/etc/hosts dest=/opt/hosts
tags:
- abc
- name: touch
file: path=/opt/hosts01 state=touch
2、检查语法
[root@ansible apache]# ansible-playbook tag.yaml --syntax-check
playbook: tag.yaml
3、执行yaml文件
[root@ansible apache]# ansible-playbook tag.yaml --tag="abc"
PLAY [node1] ************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [20.0.0.20]
TASK [copy] *************************************************************************************************************
changed: [20.0.0.20]
PLAY RECAP **************************************************************************************************************
20.0.0.20 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4、验证
节点1是否只执行对应标签上的命令
[root@server1 opt]# ls
hosts
2、特殊标签
playbook还提供了一个特殊的tags为always。作用就是当使用always当不管你执行的什么标签下的名都会执行always标签对应的命令,
1、创建一个测试文件
[root@ansible apache]# vim ts.yaml
- hosts: node2
remote_user: root
tasks:
- name: copy
copy: src=/etc/hosts dest=/opt/hosts
tags:
- abc
- name: touch01
file: path=/opt/hosts01 state=touch
- name: touch02
file: path=/opt/hosts02 state=touch
tags:
- always
2、检查语法
[root@ansible apache]# ansible-playbook ts.yaml --syntax-check
playbook: ts.yaml
3、执行yaml文件
[root@ansible apache]# ansible-playbook ts.yaml --tag="abc"
PLAY [node2] ************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [20.0.0.30]
TASK [copy] *************************************************************************************************************
changed: [20.0.0.30]
TASK [touch02] **********************************************************************************************************
changed: [20.0.0.30]
PLAY RECAP **************************************************************************************************************
20.0.0.30 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4、验证
去节点2验证
[root@server2 opt]# ls
hosts hosts02