一、部署web服务器
1、部署yum仓库
2、安装httpd
3、新建/www目录
4、在/www中新建index.html,内容为my name is renweiwei
5、该web服务器的DocumentRoot为/www
5、实现在ansible中能够使用http://node1访问到该网页内容
[student@ansible ansible]$ vim ren.yml
- name: create web service
hosts: node1
tasks:
- name: mount mnt
mount:
src: /dev/cdrom
path: /mnt
fstype: iso9660
state: mounted
- name: yum_repe
yum_repository:
file: baseos
name: bb
description: bb
baseurl: file:///mnt/BaseOS
enabled: yes
gpgcheck: no
- name: yum_repo2
yum_repository:
file: appstream
name: aa
description: aa
baseurl: file:///mnt/AppStream
enabled: yes
gpgcheck: no
- name: install httpd
yum:
name: httpd
state: present
- name: create /www
file:
src: /var/www/html
dest: /www
state: link
mode: 0775
- name: create /www/index.html
copy:
content: "my name is renweiwei\n"
dest: /www/index.html
- name: context
file:
path: /www/index.html
setype: httpd_sys_content_t
- name: httpd.conf
replace:
path: /etc/httpd/conf/httpd.conf
regexp: 'DocumentRoot "/var/www/html"'
replace: 'DocumentRoot "/www"'
- name: httpd.conf2
replace:
path: /etc/httpd/conf/httpd.conf
regexp: <Directory "/var/www">
replace: <Directory "/www">
- name: started httpd service
service:
name: httpd
state: restarted
enabled: yes
[student@ansible ansible]$ ansible-playbook ren.yml
PLAY [create web service] **********************************************************************
TASK [Gathering Facts] *************************************************************************
ok: [node1]
TASK [mount mnt] *******************************************************************************
ok: [node1]
TASK [yum_repe] ********************************************************************************
changed: [node1]
TASK [yum_repo2] *******************************************************************************
changed: [node1]
TASK [install httpd] ***************************************************************************
ok: [node1]
TASK [create /www] *****************************************************************************
ok: [node1]
TASK [create /www/index.html] ******************************************************************
ok: [node1]
TASK [context] *********************************************************************************
ok: [node1]
TASK [httpd.conf] ******************************************************************************
ok: [node1]
TASK [httpd.conf2] *****************************************************************************
ok: [node1]
TASK [started httpd service] *******************************************************************
changed: [node1]
PLAY RECAP *************************************************************************************
node1 : ok=11 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
//在node1主机上查看
[root@node1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[student@ansible ansible]$ curl http://node1
my name is renweiwei
二、使用notify…handlers
1、写一个剧本runtime.yml,只对node1操作
2、创建用户aa,该用户不能用于登录,家目录/www
3、在/www创建一个文件html
4、每次执行该剧本时,将系统的当前时间输入到html文件中。
5、如果html中的时间发生变化,那么创建/tmp/kk的文件
[student@ansible ansible]$ cat runtime.yuml
- name: create file
hosts: node1
tasks:
- name: create user
user:
name: aa
shell: /sbin/nologin
home: /www
- name: create html
file:
path: /www/html
state: touch
- name: date
shell: date > /www/html
notify:
- kk
handlers:
- name: kk
file:
path: /tmp/kk
state: touch
[student@ansible ansible]$ ansible-playbook runtime.yuml
PLAY [create file] *****************************************************************************
TASK [Gathering Facts] *************************************************************************
ok: [node1]
TASK [create user] *****************************************************************************
changed: [node1]
TASK [create html] *****************************************************************************
changed: [node1]
TASK [date] ************************************************************************************
changed: [node1]
RUNNING HANDLER [kk] ***************************************************************************
changed: [node1]
PLAY RECAP *************************************************************************************
node1 : ok=5 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0