(第三十一天)

上午
1 、使用 ansible 安装并启动 ftp 服务
[root@1 ~] # vim /etc/ansible/hosts
s0 ansible_ssh_host = 10 .0.0.12 ansible_ssh_port = 22 ansible_ssh_user = root
ansible_ssh_pass = 1
s1 ansible_ssh_host = 10 .0.0.13 ansible_ssh_port = 22 ansible_ssh_user = root
ansible_ssh_pass = 1
s2 ansible_ssh_host = 10 .0.0.14 ansible_ssh_port = 22 ansible_ssh_user = root
ansible_ssh_pass = 1
[s]
s0
s1
s2
# 下载最新版本的 ftp 软件包
[root@1 ~] # ansible s -m yum -a 'name=vsftpd state=latest'
# 开启 vsftp 服务并设置 vsftpd 服务开机自启
[root@1 ~] # ansible s -m service -a 'name=vsftpd state=started enabled=on'
# 关闭防火墙服务
[root@1 ~] # ansible s -m service -a 'name=firewalld state=stopped
enabled=off'
# 下载 lftp 软件包
[root@1 ~] # yum -y install lftp
# 连接文件共享服务器
[root@1 ~] # lftp 10.0.0.12
# 在共享目录中创建文件
[root@1 ~] # ansible s -m file -a 'path=/var/ftp/pub/sb state=touch'
# 连接文件共享服务器并查看共享文件
[root@1 ~] # lftp 10.0.0.12
lftp 10 .0.0.12:~> ls
drwxr-xr-x 2 0 0 16 Aug 19 01 :43 pub
lftp 10 .0.0.12:/> ls pub/
-rw-r--r-- 1 0 0 0 Aug 19 01 :43 sb
lftp 10 .0.0.12:/> quit
2 、使用 ansible script 模块远程批量执行脚本
[root@1 ~] # vim tst.sh
[root@1 ~] # ansible s -m script -a './tst.sh'
[root@ab ~] # tree /tmp
/tmp
├── three
│ └── test
[root@ab ~] # cat /tmp/three/test
i an echo ,at mt
3 、使用 ansible 安装启动 nfs 服务
# 使用 command 模块远程批量下载 nfs-utils 软件
[root@1 ~] # ansible s -m command -a 'yum -y install nfs-utils'
# 使用 yum 模块远程批量下载 rpcbind 软件
[root@1 ~] # ansible s -m yum -a 'name=rpcbind state=latest'
[root@ab ~] # rpm -qa | grep rpcbind
rpcbind-0.2.0-49.el7.x86_64
[root@ab ~] # rpm -qa | grep nfs
libnfsidmap-0.25-19.el7.x86_64
nfs-utils-1.3.0-0.68.el7.2.x86_64
# 在控制机上编辑 exports 文件
[root@1 ~] # vim /etc/exports
/static *(ro,sync)
# 使用 ansible file 模块远程批量下载 static 目录
[root@1 ~] # ansible s -m file -a 'path=/static state=directory'
# 使用 ansible file 模块远程批量下载 touch 文件
[root@1 ~] # ansible s -m file -a 'path=/static/test state=touch'
# 使用 ansible copy 模块将本地的 exports 文件拷贝到被控制机上覆盖原文件
[root@1 ~] # ansible s -m copy -a 'src=/etc/exports dest=/etc/exports'
# 使用 ansible command 模块远程批量启动、查看、开机自启 nfs 服务
[root@1 ~] # ansible s -m command -a 'systemctl start nfs'
[root@1 ~] # ansible s -m command -a 'systemctl status nfs'
[root@1 ~] # ansible s -m command -a 'systemctl enable nfs'
# 使用 ansible service 模块远程批量启动并设置开机自启 rpcbind 服务
[root@1 ~] # ansible s -m service -a 'name=rpcbind state=started enabled=yes'
# 在控制机上安装 nfs-utils 软件包
[root@1 ~] # yum -y install nfs-utils.x86_64
# 在控制机上创建 nfs 目录
[root@1 ~] # mkdir /nfs
# 10.0.0.12 主机上的 static 目录挂载到本机的 nfs 目录
[root@1 ~] # mount -t nfs 10.0.0.12:/static /nfs/
[root@1 ~] # ls /nfs/
test
4 playbook 的简单介绍
playbook( 剧本 ): ansible ⽤于配置 , 部署 , 和管理被控节点的剧本。⽤于 ansible 操作的编排。
使⽤的格式为 yaml 格式( saltstack,elk,docker,dockercompose,kubernetes 等也都会⽤到 yaml
)
YMAL 格式 :文件以 .yaml .yml 结尾
⽂件的第⼀⾏以 "---" 开始,表明 YMAL ⽂件的开始 ( 可选的 )
# 号开头为注释
列表中的所有成员都开始于相同的缩进级别 , 并且使⽤⼀个 "- " 作为开头 ( ⼀个横杠和⼀个空格 )
⼀个字典是由⼀个简单的 键 : 值 的形式组成 ( 这个冒号后⾯必须是⼀个空格 )
playbook 语法
hosts: ⽤于指定要执⾏任务的主机,其可以是⼀个或多个由冒号分隔主机组。
remote_user: ⽤于指定远程主机上的执⾏任务的⽤户。
tasks: 任务列表 , 按顺序执⾏任务 . 如果⼀个 host 执⾏ task 失败 , 整个 tasks 都会回滚 , 修正 playbook
中的错误 , 然后重新执⾏即可。
handlers: 类似 task ,但需要使⽤ notify 通知调⽤。 不管有多少个通知者进⾏了 notify ,等到 play
的所有 task 执⾏完成之后, handlers 也只会被执⾏⼀次。
handlers 最佳的应⽤场景是⽤来重启服务 , 或者触发系统重启操作。
variables: 变量 定义变量可以被多次⽅便调⽤。
master # vim /etc/ansible/playbook/example2.yaml
---
- hosts: group1
remote_user: root
vars:
- user: test1
tasks:
- name: create user
user: name = {{user}} state = present
5 、使用 playbook 卸载安装 vsftpd 软件包并启动 ftp 服务
[root@1 ~] # vim c.yml
---
- hosts: s
remote_user: root
tasks:
- name: 卸载 vsftpd
yum: name = vsftpd state = absent
- name: 安装 vsftpd
yum: name = vsftpd state = latest
- name: 启动服务并设置服务开机自启动
service : name = vsftpd state = started enabled = on
# 执行 playbook
[root@1 ~] # ansible-playbook c.yml
6 、使用 playbook 完成每次修改配置文件后自动重启服务
[root@1 ~] # vim c.yml
- name: 修改配置文件
command: sed -i '/^anonymous_enable=YES/ s/YES/NO/g'
/etc/vsftpd/vsftpd.conf
notify:
- ab
handlers:
- name: ab
service : name = vsftpd state = restarted
[root@1 ~] # ansible-playbook c.yml
下午
1 、简单 playbook 模板
---
- hosts: 组名 / 别名 /ip/ 域名
remote_user: root
tasks:
- name: 任务说明
模块 : key0 = value0
# service: name=vsftpd state=stated enabled=on
- name: 修改配置文件
command sed .......
notify:
- ab
handlers:
- name: ab
service : name = httpd state = restarted
2 、使用 playbook 安装重启 httpd 服务
[root@1 ~] # vim httpd.yml
---
- hosts: s
remote_user: root
tasks:
- name: 复制 repo 文件到被控制主机
copy: src = /etc/yum.repos.d dest = /etc/
- name: 安装 httpd
yum: name = httpd state = present
- name: 启动 httpd
service : name = httpd state = started enabled = on
- name: 修改配置文件
command: sed -i '/Listen 80/ s/80/8080/g' /etc/httpd/conf/httpd.conf
notify:
- ab
- name: 修改默认的资源文件
shell: echo 'ansible playbook' > /var/www/html/index.html
handlers:
- name: ab
service : name = httpd state = restarted
[root@1 ~] # ansible-playbook httpd.yml
[root@1 ~] # curl 10.0.0.12:8080
ansible playbook
[root@1 ~] # curl 10.0.0.13:8080
ansible playbook
[root@1 ~] # curl 10.0.0.14:8080
ansible playbook
3 、使用 playbook 操纵多台主机进行不同操作
[root@1 ~] # vim t.yml
---
- hosts: s1
remote_user: root
tasks:
- name: 创建一个文件
file: path = /tmp/x.txt state = touch
- hosts: s2
remote_user: root
tasks:
- name: 也创建一个文件
file: path = /tmp/c.txt state = touch
[root@1 ~] # ansible-playbook t.yml
4 、使用 playbook 一次性搭建 nfs 服务器端和客户端
[root@1 ~] # vim nfs.yml
---
- hosts: s1
remote_user: root
tasks:
- name: 安装 nfs
yum: name = nfs state = present
- name: 安装 rpcbind
yum: name = rpcbind state = present
- name: 创建一个共享目录
file: path = /abc state = directory
- name: 创建共享文件
file: path = /abc/a.txt state = touch
- name: 修改 exports 文件
shell: echo '/abc *(ro,sync)' > /etc/exports
notify:
- ab
- name: 启动 nfs 服务
service : name = nfs state = started enabled = on
- name: 启动 rpcbind 服务
service : name = rpcbind state = started enabled = on
handlers:
- name: ab
service : name = nfs state = restarted
- hosts: s2
remote_user: root
tasks:
- name: 创建挂载目录
file: path = /hhabc state = directory
- name: 下载 nfs-utils 软件
yum: name = nfs-utils state = present
- name: 挂载共享目录
command: mount -t nfs 10 .0.0.13:/abc /hhabc/
[root@ab ~] # ls /hhabc/
a.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值