[root@ansible ~]# vim /etc/ansible/hosts
[webservers]
10.0.0.150 ansible_connection=local
10.0.0.160
#创建角色相关目录
[root@ansible html]# mkdir -pv /data/ansible/roles/httpd/{tasks,handlers,files}
mkdir: created directory '/data/ansible'
mkdir: created directory '/data/ansible/roles'
mkdir: created directory '/data/ansible/roles/httpd'
mkdir: created directory '/data/ansible/roles/httpd/tasks'
mkdir: created directory '/data/ansible/roles/httpd/handlers'
mkdir: created directory '/data/ansible/roles/httpd/files'
#创建角色相关文件
[root@ansible html]# cd /data/ansible/roles/httpd/
#main.yml是task的入口文件
[root@ansible httpd]# vim tasks/main.yml
- include: group.yml
- include: user.yml
- include: install.yml
- include: config.yml
- include: index.yml
- include: service.yml
[root@ansible httpd]# vim tasks/group.yml
- name: create apache group
group: name=apache system=yes gid=80
[root@ansible httpd]# vim tasks/user.yml
- name: create apache user
user: name=apache system=yes shell=/sbin/nologin home=/var/www/ uid=80 group=apache
[root@ansible httpd]# vim tasks/install.yml
- name: install httpd package
yum: name=httpd
[root@ansible httpd]# vim tasks/config.yml
- name: config file
copy: src=httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart
[root@ansible httpd]# vim tasks/index.yml
- name: index.html
copy: content='{{ ansible_hostname }}\n' dest=/var/www/html/index.html
[root@ansible httpd]# vim tasks/service.yml
- name: start httpd service
service: name=httpd state=started enabled=yes
[root@ansible httpd]# vim handlers/main.yml
- name: restart
service: name=httpd state=restarted
#file目录下准备httpd.conf配置文件
[root@ansible httpd]# ls files/httpd.conf
files/httpd.conf
[root@ansible httpd]# tree /data/ansible/roles/httpd/
/data/ansible/roles/httpd/
├── files
│ └── httpd.conf
├── handlers
│ └── main.yml
└── tasks
├── config.yml
├── group.yml
├── index.yml
├── install.yml
├── main.yml
├── service.yml
└── user.yml
3 directories, 9 files
#在playbook中调用角色
[root@ansible ~]# vim /data/ansible/role_httpd.yml
- hosts: webservers
remote_user: root
roles:
- httpd
#运行playbook
[root@ansible ~]# ansible-playbook /data/ansible/role_httpd.yml
#验证:
[root@ansible ~]# curl 10.0.0.150
ansible
[root@ansible ~]# curl 10.0.0.160
node1
标签:index,name,httpd,data,ansible,html,node1,root,yml
From: https://www.cnblogs.com/tanll/p/17748189.html