多个playbook运行实验
实验环境
[student@workstation playbook-multi]$ cat inventory
servera.lab.example.com
[student@workstation playbook-multi]$ cat ansible.cfg
[defaults]
inventory=inventory
remote_user=devops
[privilege_escalation]
become=False
become_method=sudo
become_user=root
become_ask_pass=False
实验目的
在servera搭建一个web服务器并访问
yml文件
第一个play:确认服务是否开启,目标主机位servera,需要提权
- 判断httpd和firewalld服务是否安装为最新版本
- 在http服务中添加首页
- 确认防火墙装填
- 确认httpd服务状态
第二个play:探测是否能访问到index文件
- 连接到servera,并访问web页面
[student@workstation playbook-multi]$ cat intranetweb.yml
---
- name: Enable intranet services
hosts: servera.lab.example.com
become: yes
tasks:
- name: latest httpd and firewalld service installed
yum:
name:
- httpd
- firewalld
state:
latest
- name: test html page is installed
copy:
content: "welcome to the example.com intranetw!\n"
dest: /var/www/html/index.html
- name: firewalld enabled and running
service:
name: firewalld
enabled: true
state: started
- name: firewalld permits access to httpd service
firewalld:
service: http
permanent: yes
state: enabled
immediate: yes
- name: httpd enabled and running
service:
name: httpd
enabled: true
state: started
- name: test intranet web server
hosts: localhost
become: no
tasks:
- name: connect to intranet web server
uri:
url: http://servera.lab.example.com
return_content: yes
status_code: 200
运行结果
[student@workstation playbook-multi]$ !ansi
ansible-playbook intranetweb.yml
PLAY [Enable intranet services] ******************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************
ok: [servera.lab.example.com]
TASK [latest httpd and firewalld service installed] **********************************************************************************
ok: [servera.lab.example.com]
TASK [test html page is installed] ***************************************************************************************************
changed: [servera.lab.example.com]
TASK [firewalld enabled and running] *************************************************************************************************
ok: [servera.lab.example.com]
TASK [firewalld permits access to httpd service] *************************************************************************************
ok: [servera.lab.example.com]
TASK [httpd enabled and running] *****************************************************************************************************
ok: [servera.lab.example.com]
PLAY [test intranet web server] ******************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************
ok: [localhost]
TASK [connect to intranet web server] ************************************************************************************************
ok: [localhost]
PLAY RECAP ***************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
servera.lab.example.com : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0