ansible剧本练习二
要求
创建Web内容目录
按照下方所述,创建一个名为 /home/student/ansible/webcontent.yml 的 playbook:
该 playbook 在 dev 主机组中的受管节点上运行
创建符合下列要求的目录 /webdev:
所有者为 devops 组
具有常规权限:owner=read+write+execute,group=read+write+execute,other=read+execute
具有特殊权限: set group ID
用符号链接将/webdev目录链接到/var/www/html/webdev
创建文件 /webdev/index.html,其中包含如下所示的单行文本:Development
在 dev 主机组中主机上浏览此目录(例如 http://node1.example.com/webdev/ )将生成以下输出:
Development
编写剧本
[root@server ~]# su - student
Last login: Wed Oct 26 03:06:37 CST 2022 on pts/1
[student@server ansible]$ cat webcontent.yml
---
- name: web station
hosts: dev
tasks:
- name: mount dev
mount:
src: /dev/cdrom
path: /mnt
fstype: iso9660
state: mounted
- name: yum_repo
yum_repository:
file: server
name: aa
description: aa
baseurl: file:///mnt/BaseOS
enabled: yes
gpgcheck: no
- name: yum_repo2
yum_repository:
file: server
name: bb
description: bb
baseurl: file:///mnt/AppStream
enabled: yes
gpgcheck: no
- name: yum httpd
yum:
name: httpd
state: latest
- name: install semanage
yum:
name: policycoreutils-python-utils
state: latest
- name: create group
group:
name: devops
state: present
- name: mkdir /webdev
file:
path: /webdev
state: directory
group: devops
mode: 2775
- name: /webdev/index.html
copy:
content: Development
dest: /webdev/index.html
- name: set context
sefcontext:
target: /webdev/index.html
setype: httpd_sys_content_t
state: present
- name: restorecon
command: restorecon -Rv /webdev/index.html
- name: creste link
file:
src: /webdev
dest: /var/www/html/webdev
state: link
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
- name: firewalld for http
firewalld:
rich_rule: rule family=ipv4 source address=192.168.87.0/24 service name=http accept
state: enabled
permanent: yes
immediate: yes
执行
[student@server ansible]$ ansible-playbook webcontent.yml
PLAY [web station] *****************************************************************************
TASK [Gathering Facts] *************************************************************************
ok: [node1]
TASK [mount dev] *******************************************************************************
changed: [node1]
TASK [yum_repo] ********************************************************************************
changed: [node1]
TASK [yum_repo2] *******************************************************************************
changed: [node1]
TASK [yum httpd] *******************************************************************************
changed: [node1]
TASK [install semanage] ************************************************************************
changed: [node1]
TASK [create group] ****************************************************************************
changed: [node1]
TASK [mkdir /webdev] ***************************************************************************
changed: [node1]
TASK [/webdev/index.html] **********************************************************************
changed: [node1]
TASK [set context] *****************************************************************************
changed: [node1]
TASK [restorecon] ******************************************************************************
changed: [node1]
TASK [creste link] *****************************************************************************
changed: [node1]
TASK [restart httpd] ***************************************************************************
changed: [node1]
TASK [firewalld for http] **********************************************************************
changed: [node1]
PLAY RECAP *************************************************************************************
node1 : ok=14 changed=13 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
验证
[student@server ansible]$ curl http://node1.example.com/webdev/
Development