ansible练习三

一、部署web服务器
1、部署yum仓库、

2、安装httpd

3、新建/www目录

4、在/www中新建index.html,内容为my name is chenyu(chenyu为你们自己名字的全拼)

5、该web服务器的DocumentRoot为/www

6、实现在ansible中能够使用http://node1访问到该网页内容

[student@ansible ansible]$ vim a.yml
---
- name: web statin
  hosts: node1
  tasks: 
    - name: disposition AppStream yum
      yum_repository: 
        file: myrepo
        name: AppStream
        description: Centos8.0 AppStream
		baseurl: http://mirrors.ustc.edu.cn/centos-vault/8.5.2111/AppStream/x86_64/os/
		enabled: yes
		gpgcheck: no
	- name: disposition BaseOS yum
	  yum_repository: 
		file: myrepo
		name: BaseOS
		description: Centos8.0 BaseOS
		baseurl: http://mirrors.ustc.edu.cn/centos-vault/8.5.2111/BaseOS/x86_64/os/
		enabled: yes
		gpgcheck: no
	- name: mount Dev
	  mount: 
	    src: /dev/cdrom
	    path: /mnt
	  	fstype: iso9660
	  	state: mounted
	- name: install httpd
	  yum: 
	  	name: httpd
	  	state: installed
	- name: create link
	  file: 
	  	src: /var/www/html
	  	dest: /www
	  	state: link
	- name: create files
	  file: 
	  	path: /www/index.html
	  	state: touch
	- name: Add content
	  lineinfile: 
	    dest: /www/index.html
	  	line: my name is zhaoshulin
	- name: set selinux context
	  file: 
	    path: '/www/index.html'
	  	setype: httpd_sys_content_t
	- name: apply context
	  shell: 
	    cmd: restorecon -Rv /www/index.html
	- name: modify apache config1
	  replace: 
	    path: /etc/httpd/conf/httpd.conf
		regexp: DocumentRoot "/var/www/html"
		replace: DocumentRoot "/www"
	- name: modify apache config2
	  replace: 
	    path: /etc/httpd/conf/httpd.conf
		regexp: <Directory "/var/www">
		replace: <Directory "/www">
	- name: restart httpd
	  service: 
		name: httpd
		state: restarted
		enabled: yes
	- name: set firewalld for httpd
	  firewalld: 
		service: http
		state: enabled
		permanent: yes
		immediate: yes
[student@ansible ansible]$ ansible-playbook a.yml
******略******
PLAY RECAP ********************************************************************************************
node1                      : ok=14    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

测试

[student@ansible ansible]$ curl http://node1
my name is liuxiang

二、使用notify....handlers
1、写一个剧本runtime.yml,只对node1操作

2、创建用户aa,该用户不能用于登录,家目录/www

3、在/www创建一个文件html

4、每次执行该剧本时,将系统的当前时间输入到html文件中。

5、如果html中的时间发生变化,那么创建/tmp/kk的文件

[student@ansible ansible]$ vim runtime.yml
---
- name: runtime
  hosts: node1
  tasks:
	- name: useradd aa
	  user:
	  	name: aa
	  	shell: /sbin/nologin
		home: /www
	- name: create file
	  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 b.yml
******略******
PLAY RECAP ********************************************************************************************
node1                      : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
posted @ 2022-10-26 00:01  Archer-x  阅读(47)  评论(0编辑  收藏  举报