ansible实现role的高级用法---httpd服务剧本简单样例

这个博客展示了如何使用Ansible Role来自动化部署Apache HTTP服务。Role包括创建用户和组、安装包、配置模板、复制文件及启动服务。通过`httpd_role.yml` playbook运行,它在指定主机上执行任务,如创建app用户和组、安装httpd、设置配置文件并启动服务,确保服务正常运行。

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

学习目标:

能够编写简单的role角色

学习内容:

[root@zabbix_server ansible]# tree roles/app/
roles/app/
├── files
│   └── testfile.conf
├── handlers
│   └── main.yml
├── tasks
│   ├── file.yml
│   ├── group.yml
│   ├── main.yml
│   ├── start.yml
│   ├── templ.yml
│   ├── user.yml
│   └── yum.yml
├── templates
│   └── httpd.conf.j2
└── vars
    └── main.yml

5 directories, 11 files
[root@zabbix_server tasks]# cat file.yml 
- name: copy file
  copy: src=testfile.conf  dest=/etc/httpd/conf.d/ owner=app 
[root@zabbix_server tasks]# cat group.yml 
- name: create group
  group: name=app system=yes gid=80
[root@zabbix_server tasks]# cat user.yml 
- name: create user
  user: name=app system=yes group=app shell=/sbin/nologin uid=80
[root@zabbix_server tasks]# cat start.yml 
- name: start service
  service: name=httpd state=started enabled=yes
[root@zabbix_server tasks]# cat templ.yml 
- name: copy templates
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  notify: restart service
[root@zabbix_server tasks]# cat yum.yml 
- name: install package
  yum: name=httpd
[root@zabbix_server tasks]# cat main.yml 
- include: group.yml
- include: user.yml
- include: yum.yml
- include: templ.yml
- include: file.yml
- include: start.yml
-rw-r--r-- 1 root root 0 May  3 10:44 testfile.conf
[root@zabbix_server files]# pwd
/etc/ansible/roles/app/files
[root@zabbix_server app]# cd handlers/
[root@zabbix_server handlers]# ll
total 4
-rw-r--r-- 1 root root 62 May  3 10:49 main.yml
[root@zabbix_server handlers]# cat main.yml 
- name: restart service
  service: name=httpd state=restarted
-rw-r--r-- 1 root root 11801 May  3 10:42 httpd.conf.j2
[root@zabbix_server templates]# grep '{{.*}}' httpd.conf.j2 
Listen {{ ansible_processor_vcpus*100 }}
User {{ username }}
Group {{ groupname }}
[root@zabbix_server templates]# pwd
/etc/ansible/roles/app/templates
[root@zabbix_server app]# cd vars/
[root@zabbix_server vars]# ll
total 4
-rw-r--r-- 1 root root 29 May  3 10:46 main.yml
[root@zabbix_server vars]# cat main.yml 
username: app
groupname: app
[root@zabbix_server ansible]# cat httpd_role.yml 
- hosts: LYP
  remote_user: root

  roles:
    - app
[root@zabbix_server ansible]# ansible-playbook httpd_role.yml 

PLAY [LYP] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************************************************************************************
ok: [172.28.102.131]
ok: [172.28.102.130]

TASK [app : create group] ***********************************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]

TASK [app : create user] ************************************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]

TASK [app : install package] ********************************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]

TASK [app : copy templates] *********************************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]

TASK [app : copy file] **************************************************************************************************************************************************************************************
changed: [172.28.102.131]
changed: [172.28.102.130]

TASK [app : start service] **********************************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]

RUNNING HANDLER [app : restart service] *********************************************************************************************************************************************************************
changed: [172.28.102.130]
changed: [172.28.102.131]

PLAY RECAP **************************************************************************************************************************************************************************************************
172.28.102.130             : ok=8    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.28.102.131             : ok=8    changed=7    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值