1、如果当前受管主机的根分区容量大于1G,则安装httpd和mariadb-server软件包,如果httpd和mariadb服务未运行则运行该服务。
[xiaoming@centos7 chap04]$ cat play5-1.yml
---
- hosts: node01
tasks:
- name: install
yum:
name:
- httpd
- mariadb-server
when: item.mount == "/" and item.size_total > 1000000000
loop: "{{ ansible_facts.mounts }}"
- name: start service
service:
name: "{{ item }}"
state: started
loop:
- httpd
- mariadb
2、将example.conf文件复制到/etc/httpd/conf.d/目录,example.conf文件内容如下:
<virtualhost *:80>
servername 0.0.0.0
documentroot /var/www/html
</virtualhost>
<directory /var/www/html>
allowoverride none
require all granted
</directory>
如果/etc/httpd/conf.d/目录下的文件更新,则重启httpd服务。配置/var/www/html/index.html文件内容如下: zuoye
//主机
[xiaoming@centos7 chap04]$ cat example.conf
<virtualhost *:80>
servername 0.0.0.0
documentroot /var/www/html
</virtualhost>
<directory /var/www/html>
allowoverride none
require all granted
</directory>
[xiaoming@centos7 chap04]$ cat play5-2.yml
---
- hosts: node01
tasks:
- copy:
src: example.conf
dest: /etc/httpd/conf.d/example.conf
notify: restart httpd
- copy:
content: "zuoye\n"
dest: /var/www/html/index.html
- service:
name: firewalld
state: stopped
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
//受控
[root@node01 ~]# cat /etc/httpd/conf.d/example.conf
<virtualhost *:80>
servername 0.0.0.0
documentroot /var/www/html
</virtualhost>
<directory /var/www/html>
allowoverride none
require all granted
</directory>
[root@node01 html]# pwd
/var/www/html
[root@node01 html]# cat index.html
zuoye
3、创建一个playbook,要求如下:
该playbook运行在所有受控节点
该playbook覆盖/etc/message文件的内容
在dev主机组的主机上,内容是:Development
在test主机组的主机上,内容是:Test
[xiaoming@centos7 chap04]$ cat play5-3.yml
---
- hosts: all
tasks:
- copy:
content: "development\n"
dest: /etc/message
when: inventory_hostname in groups.dev
- copy:
content: "Test\n"
dest: /etc/message
when: inventory_hostname in groups.test
//受控dev
[root@node01 html]# cat /etc/message
development
//受控test
[root@node02 ~]# cat /etc/message
Test