目录
3)配置文件模板准备 templates/httpd.conf.j2
# 编写 main.yml,将上面的这些 task 引入进来
5)编写重启 httpd 的 handlers, handlers/main.yml
# 这里的名字需要和 task 中的 notify 保持一致
6)编写主的 httpd_roles.yml 文件调用 httpd 角色
通过 ansible roles 安装配置 httpd 服务,此处的 roles 使用默认的路径/etc/ansibl
e/roles
[root@ansible ~]# cd /etc/ansible/roles/
[root@ansible roles]# mkdir -p httpd/{handlers,tasks,templates,vars}
初始化 httpd 角色,注意需要在 roles 目录下执行,并且更改 ansible.cfg 文件更改 r
oles-path 路径可以识别到自创建的角色。
- [root@ansible roles]# cd httpd/
- [root@ansible httpd]# tree .
- .
- ├── handlers
- ├── tasks
- ├── templates
- └── vars
- 4 directories, 0 file
- [root@ansible httpd]# vim vars/main.yml
- portnum: 8909
- username: www
- groupname: www
- # copy 一个本地的配置文件放在 templates/下并已 j2 为后缀
- [root@ansible httpd]# cp /etc/httpd/conf/httpd.conf templates/httpd.con
- f.j2
- [root@ansible httpd]# vim templates/httpd.conf.j2
- portnum: 8909
- username: www
- groupname: www
- [root@ansible httpd]# vim tasks/group.yml
- - name: Create a Startup Group
- group: name=www gid=60 system=yes
- [root@ansible httpd]# vim tasks/user.yml
- - name: Create Startup Users
- user: name=www uid=60 system=yes shell=/sbin/nologin
- [root@ansible httpd]# vim tasks/install.yml
- - name: Install Package Httpd
- yum: name=httpd state=installed
- [root@ansible httpd]# vim tasks/config.yml
- - name: httpd configer file
- template:
- src: httpd/templates/httpd.conf.j2
- dest: /etc/httpd/conf/httpd.conf
- notify: restart httpd
- [root@ansible httpd]# vim tasks/start.yml
- - name: Start Httpd Service
- service: name=httpd state=started enabled=yes
- [root@ansible httpd]# vim tasks/main.yml
- - import_tasks: group.yml
- - import_tasks: user.yml
- - import_tasks: install.yml
- - import_tasks: start.yml
- - include_tasks: config.yml
[root@ansible httpd]# vim handlers/main.yml
- - name: Restart Httpd
- service: name=httpd state=restarted
- [root@ansible httpd]# cd ..
- [root@ansible roles]# vim httpd_roles.yml
-
- - hosts: all
- remote_user: root
- roles:
- [root@server roles]# pwd
- /root/ansible/roles
- [root@server roles]# tree
- [root@server roles]# tree
- .
- ├── httpd
- │ ├── handlers
- │ │ └── main.yml
- │ ├── tasks
- │ │ ├── config.yml
- │ │ ├── group.yml
- │ │ ├── install.yml
- │ │ ├── main.yml
- │ │ ├── start.yml
- │ │ └── user.yml
- │ ├── templates
- │ │ └── httpd.conf.j2
- │ └── vars
- │ ├── httpd.conf.j2
- │ └── main.yml
- └── httpd_roles.yml
8)测试
- [root@rhcsa playbook-project]# ansible-playbook playbook1.yml
-
- PLAY [rhce] ****************************************************************************
-
- TASK [Gathering Facts] *****************************************************************
- ok: [rhce]
-
- TASK [httpd : stop firewalld] **********************************************************
- ok: [rhce]
-
- TASK [httpd : set selinux work mode] ***************************************************
- ok: [rhce]
-
- TASK [httpd : Create a Startup Group] **************************************************
- ok: [rhce]
-
- TASK [httpd : Create Startup Users] ****************************************************
- ok: [rhce]
-
- TASK [httpd : install httpd] ***********************************************************
- ok: [rhce]
-
- TASK [httpd : httpd configer file] *****************************************************
- ok: [rhce]
-
- TASK [debug] ***************************************************************************
- ok: [rhce] => {
- "msg": "This is my task1"
- }
-
- PLAY RECAP *****************************************************************************
- rhce : ok=8 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
