ansible自动化运维两个重要模块

本文档介绍了如何使用Ansible模板模块安装配置HTTPD服务器,并演示了如何通过标签功能执行特定任务。在基础环境中,对两台服务器进行HTTPD服务的安装与配置,包括设置ServerName和Listen参数。此外,详细阐述了Ansible中tags的用法,包括执行指定标签的命令和使用特殊标签always确保某些任务总是被执行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

template模块安装配置httpd服务器

基本环境

名称主机名P地址
ansible服务器ansible20.0.0.10
节点1server120.0.0.20
节点2server220.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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值