1.创建目录
[root@ansible ~]# cd /etc/ansible/roles/
# 创建需要用到的目录
[root@ansible roles]# mkdir -p httpd/{handlers,tasks,templates,vars}
注意:初始化httpd角色,注意需要在roles目录下执行,并且更改ansible.cfg文件更改roles-path路径可以识别到自创建的角色。.
[root@ansible roles]# cd httpd/ [root@ansible httpd]# tree . . ├── handlers ├── tasks ├── templates └── vars 4 directories, 0 file
2.变文件量准备vars/main.yml
[root@ansible httpd]# vim vars/main.yml portnum: 8909 username: www groupname: www
3.配置文件模板准备templates/httpd.conf.j2
# copy一个本地的配置文件放在templates/下并已j2为后缀
[root@ansible httpd]# cp /etc/httpd/conf/httpd.conf templates/httpd.conf.j2
4.任务剧本编写,创建用户、创建组、安装软件、配置、启动等
# 创建组的task
root@ansible httpd]# vim tasks/group.yml - name: Create a Startup Group group: name=www gid=60 system=yes
# 创建用户的task
[root@ansible httpd]# vim tasks/user.yml - name: Create Startup Users user: name=www uid=60 system=yes shell=/sbin/nologin
# 安装软件的task
[root@ansible httpd]# vim tasks/install.yml - name: Install Package Httpd yum: name=httpd state=installed
# 配置软件的task
[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
# 启动软件的task
[root@ansible httpd]# vim tasks/start.yml - name: Start Httpd Service service: name=httpd state=started enabled=yes
# 编写main.yml,将上面的这些task引入进来
[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
5.编写重启httpd的handlers,handlers/main.yml
[root@ansible httpd]# vim handlers/main.yml # 这里的名字需要和task中的notify保持一致 - name: Restart Httpd service: name=httpd state=restarted
6.编写主的httpd_roles.yml文件调用httpd角色
[root@ansible httpd]# cd .. [root@ansible roles]# vim httpd_roles.yml --- - hosts: all remote_user: root roles: - role: httpd #指定角色名称
7.整体的一个目录结构查看
[root@server roles]# pwd /root/ansible/roles [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