使用模块搭建交作业页面
1.准备配置文件到m01
[root@m01 ~]# rz httpd.conf
2.所有的压缩包上传到m01
[root@m01 ~]# rz php.tar.gz
[root@m01 ~]# rz kaoshi.zip
3.编写ansible命令
#注:&&:前面执行完再执行后面的步骤(如果不加这些步骤会同时执行,有些没执行,有些已经执行完)
#安装httpd
ansible 'web_group' -m yum -a 'name=httpd state=present' &&\
#解压php包
ansible 'web_group' -m unarchive -a 'src=/root/php.tar.gz dest=/tmp/' &&\
#安装php
ansible 'web_group' -m shell -a 'yum localinstall -y /tmp/*.rpm'
#解压代码
ansible 'web_group' -m unarchive -a 'src=/root/kaoshi.zip dest=/var/www/html/' &&\
#创建文件目录
ansible 'web_group' -m file -a 'path=/var/www/html/upload state=directory' &&\
#创建用户组
ansible 'all' -m group -a 'name=www gid=666 state=present' &&\
#创建用户 create_home:是否创建家目录 /sbin/nologin:不需要登录
ansible 'all' -m user -a 'name=www group=www uid=666 create_home=false shell=/sbin/nologin' &&\
#授权代码目录
ansible 'web_group' -m file -a 'path=/var/www/ state=directory owner=www group=www' &&\
#推送httpd配置文件
ansible 'web_group' -m copy -a 'src=/root/httpd.conf dest=/etc/httpd/conf/' &&\
#启动httpd
ansible 'web_group' -m systemd -a 'name=httpd state=restarted enabled=yes' &&\
#安装nfs服务
ansible 'all' -m yum -a 'name=nfs-utils state=present' &&\
#编辑nfs配置文件
ansible 'nfs' -m copy -a 'content="/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)" dest=/etc/exports' &&\
#创建共享目录并授权
ansible 'nfs' -m file -a 'path=/data state=directory owner=www group=www' &&\
#启动nfs enabled:开机自启
ansible 'nfs' -m systemd -a 'name=nfs state=restarted enabled=yes' &&\
#挂载
ansible 'web_group' -m mount -a 'src=172.16.1.31:/data path=/var/www/html/upload fstype=nfs opts=defaults state=mounted'
Ansible playbook
一、playbook概述
1.什么是playbook
PlayBook即"剧本","兵书"之意,Playbook是由以下部分组成的
play(host): 定义的是主机的角色。(主角还是配角)
book(task): 定义的是具体执行的任务。(角色的台词和动作)
playbook: 由一个或多个play(角色)组成,一个play(角色)可以包含多个task(台词,动作)。
简单理解为:使用不同的模块完成一件事
在Ansible中"剧本文件"是以yml结尾的文件。
在SaltStack中"剧本文件"是以sls结尾的文件。
但是语法,使用的都是yaml语法
![]()
2.playbook组成
[root@m01 ~]# vim touch.yml #内容缩进2格
#定义要执行动作的主机
- hosts: web_group #算位数根据-的位置开始算
#定义操作的用户
remote_user: root #默认root,可加可不加
#定义变量
vars:
file_name: lhd #:后面一定要有空格
#动作
tasks:
#注释
- name: Touch File
#模块与实际动作
shell: touch /tmp/{{ file_name }}
#执行前先验证语法(只能验证语法,无法验证逻辑)
[root@m01 ~]# ansible-playbook --syntax-check touch.yml
playbook: touch.yml #说明语法没错
#执行
[root@m01 ~]# ansible-playbook touch.yml
#TASK [Gathering Facts] 这一步是最慢的,从每个客户端中收录信息(主机名,目录,磁盘,内存之类的信息)
#PLAY RECAP 输出结果
# 先创建文件夹再创建文件
[root@m01 ~]# vim touch.yml
- hosts: web_group
vars:
file_name: lhd
tasks:
- name: Mkdir File
file:
path: /tmp/file
state: directory
- name: Touch file
shell: touch /tmp/file/{{ file_name }}
3.playbook与ad-hoc
| 特定 | PlayBook | ad-hoc |
| 完整性 |
√ |
× |
| 持久性 |
√ |
× |
| 执行效率 |
低 |
高 |
| 变量 |
支持 |
不支持 |
| 耦合度 |
低 |
高 |
1.PlayBook功能比ad-hoc更全,是对ad-hoc的一种编排
2.PlayBook能很好的控制先后执行顺序,以及依赖关系
3.PlayBook语法展现更加的只管
4.PlayBook可以持久使用,ad-hoc无法持久使用
4.YAML语法
| 语法 | 描述 |
| 缩进 |
YAML使用固定的缩进风格表示层级结构,每个缩进由两个空格组成,不能使用TAB |
| 冒号 |
以冒号结尾的除外,其他所有冒号后面所有必须有空格 |
| 短横线 |
表示列表项,使用一个短横杠加一个空格,多个项使用同样的缩进级别作为同一列表 |
#缩进:
中国:
上海:
北京:
- 朝阳区
- 昌平
- 大兴区
#中横线:
代表是一层,表示同一个队列
二、playbook实战
1.部署httpd
1)配置主机清单
[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web01
web02
2)测试主机
[root@m01 ~]# ansible 'web_group' -m ping
web02 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
web01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
2.编写剧本
#创建项目目录
[root@m01 project]# tree /project
/project
└── httpd
[root@m01 httpd]# vim httpd.yml
- hosts: web_group
tasks:
#关闭防火墙
- name: Stop Firewalld
service:
name: firewalld
state: stopped
enabled: no
#关闭selinux
- name: Stop Selinux
selinux:
state: disabled
#安装httpd
- name: Install Httpd Server
yum:
name: httpd
state: present
#启动httpd
- name: Start Httpd Server
systemd:
name: httpd
state: started
enabled: yes
#添加访问页面
- name: Config index.html
copy:
content: test http
dest: /var/www/html/index.html
#检查语法
[root@m01 httpd]# ansible-playbook --syntax-check httpd.yml
playbook: httpd.yml
#测试
浏览器输入
10.0.0.7
10.0.0.8