当前位置: 首页 > news >正文

day31(8/19)——静态文件共享、playbook

目录

一、ansible模块

script模块

 copy模块

使用command模块下载 nfs-utils rpcbind

在被控制的主机上添加static目录,并创建test文件

command模块

service模块

二、playbook

三、playbook编排vsftpd

1、安装

2、卸载

3、启动服务

4、修改配置文件设置不允许匿名访问并重启

5、练习:修改httpd端口为8080

四、playbook编排多个hosts任务

1、创建文件

2、nfs

五、roles(难点)

1、roles介绍


一、ansible模块
script模块

[root@m0 ~]# vim test000.sh

#!/bin/bash
mkdir /tmp/three
touch /tmp/three/test
echo 'i am echo,at mttt' > /tmp/three/test
echo 'well done'

[root@m0 ~]# source test000.sh
well done
[root@m0 ~]# ansible group02 -m script -a './test000.sh'

 copy模块

[root@m0 ~]# ansible s -m copy -a 'src=./mysql_master.sh dest=~'

使用command模块下载 nfs-utils rpcbind

[root@m0 ~]# ansible s -m command -a 'yum -y install nfs-utils'
[root@m0 ~]# ansible s -m command -a 'yum -y install rpcbind'

[root@m0 ~]# vim /etc/exports

/static *(ro,sync)
在被控制的主机上添加static目录,并创建test文件

[root@m0 ~]# ansible s -m file -a 'path=/static state=directory'

[root@m0 ~]# ansible s -m file -a 'path=/static/test state=touch

[root@m0 ~]# ansible s -m copy -a 'src=/etc/exports dest=/etc/exports'

command模块

[root@m0 ~]# ansible s -m command -a 'systemctl start nfs'

[root@m0 ~]# ansible s -m command -a 'systemctl enable  nfs'

service模块

[root@m0 ~]# ansible s -m service -a 'name=rpcbind state=started enabled=yes'
[root@m0 ~]# yum -y install nfs-utils
[root@m0 ~]# mkdir /nfs
[root@m0 ~]# mount -t nfs 192.168.8.157:/static /nfs/
[root@m0 ~]# ls /nfs/
test

二、playbook

playbook(剧本):是ansible用于配置,部署,和管理被控节点的剧本。用于ansible操作的编排。

一般使用ymal格式

playbook 剧本是保存在控制机的yml文件

YMAL格式
■ 以.yaml或xyml结尾

■ 文件的第一行以” --- “开始,表明YMAL文件的开始(可选的)

■ 以#号开头为注释

■ 列表中的所有成员都开始于相同的缩进级别,并且使用一个"-“作为开头(一个横杠和一个空格)

■ 一个字典是由一个简单的 键:值 的形式组成(这个冒号后面必须是一个空格)

■ 注意:写这种文件可以使用tab键和空格

Playbook常⻅语法

hosts: ⽤于指定要执⾏任务的主机,其可以是⼀个或多个由冒号分 隔主机组.   

remote_user: ⽤于指定远程主机上的执行任务的⽤户.

tasks:任务列表,按顺序执行任务.

■ 如果一个host执行task失败,整个tasks都会回滚,修正playbook中的错误,然后重新执行即可.

---
-    hosts: 组名/别名/ip/域名remote_user:    roottasks:-    name:    任务说明模块:    key0=value0service:    name=vsftpd state=started anabled=yes-    name:    修改配置文件command:    sed...notify:-    ancdefhandler:-    name:    abcdefservice    name=vsftpd stste=restared
...
三、playbook编排vsftpd
1、安装

[root@m0 ~]# vim test001.yml

---
-       hosts:          s   #使用tab键对齐  指定组remote_user:    root #tasks:                -       name:   安装vsftpdyum:    name=vsftpd     state=latest

[root@m0 ~]# ansible-playbook ./test001.yml

2、卸载

[root@m0 ~]# vim test001.yml

---
-       hosts:          sremote_user:    roottasks:-       name:   卸载vsftpyum:    name=vsftpd     state=absent-       name:   安装vsftpdyum:    name=vsftpd     state=latest

 [root@m0 ~]# ansible-playbook ./test001.yml

3、启动服务

[root@m0 ~]# vim test001.yml 

---
-       hosts:          sremote_user:    roottasks:-       name:   卸载vsftpyum:    name=vsftpd     state=absent-       name:   安装vsftpdyum:    name=vsftpd     state=latest-       name:   启动服务service:        name=vsftpd     state=started   e
nabled=yes

[root@m0 ~]# ansible-playbook ./test001.yml

4、修改配置文件设置不允许匿名访问并重启

handlers:类似task,但需要使用notify通知调用。

■ 不管有多少个通知者进行了notify,等到play中的所有task执行完成之后,handlers也只会被执行一次.

■ handlers最佳的应用场景是用来重启服务,或者触发系统重启操作.除此以外很少用到了.

[root@m0 ~]# yum -y install lftp

[root@m0 ~]# lftp 192.168.8.157
lftp 192.168.8.157:~> ls

drwxr-xr-x    2 0        0               6 Jun 09  2021 pub
[root@s0 ~]# sed -n '/^anonymous_enable=YES/s/YES/NO/g' /etc/vsftpd/vsftpd.conf 
[root@m0 ~]# vim test001.yml

---
-       hosts:          sremote_user:    roottasks:-       name:   卸载vsftpyum:    name=vsftpd     state=absent-       name:   安装vsftpdyum:    name=vsftpd     state=latest-       name:   启动服务service:        name=vsftpd     state=started   enabled=yes-       name:   修改配置文件command:        sed -i '/^anonymous_enable=YES/s/YES/NO/g' /etc/vsftpd/vsftpd.confnotify:           #调用-       abcdefghandlers:-       name:   abcdefgservice:        name=vsftpd     state=res
tarted

[root@m0 ~]# ansible-playbook ./test001.yml

[root@m0 ~]# lftp 192.168.8.157
lftp 192.168.8.157:~> exit
[root@m0 ~]# lftp 192.168.8.157
lftp 192.168.8.157:~> ls
中断    

5、练习:修改httpd端口为8080

[root@m0 ~]# yum -y install httpd
[root@m0 ~]# systemctl start httpd
[root@m0 ~]# ls /var/www/html
[root@m0 ~]# echo 'http html file' > /var/www/html/index.html
[root@m0 ~]# curl localhost
http html file
[root@m0 ~]# vim /etc/httpd/conf/httpd.conf 

Listen 8080
[root@m0 ~]# systemctl restart httpd
[root@m0 ~]# curl localhost
curl: (7) Failed connect to localhost:80; 拒绝连接

[root@m0 ~]# curl localhost:8080
http html file

[root@m0 ~]# vim test002.yml

---
-       hosts:          sremote_user:    roottasks:-       name:   将本机的rope文件复制到被控制主机copy:   src=/etc/yum.repos.d    dest=/etc/-       name:   安装httpdyum:    name=httpd      state=present-       name:   修改配置文件command:        sed -i '/^Listen/s/80/8080/g' /etc/httpd/conf/httpd.conf-       name:   修改默认的资源文件command:        echo 'woowowowowowwo' > /var/www/html/index.html-       name:   启动httpd服务service:        name=httpd      state=started
四、playbook编排多个hosts任务
1、创建文件

[root@m0 ~]# vim test003.yml

---
-       hosts:           sremote_user:     roottasks:-       name:   创建一个文件file:   path=/tmp/xxxx.txt   state=touch-       hosts:  s1    #两台主机remote_user:    roottasks:-       name:   也创建一个文件file:   path=/tmp/yyyy.txt      state=touch
...

[root@m0 ~]# ansible-playbook ./test003.yml

[root@s0 ~]# ls /tmp/

xxxx.txt

[root@s1 ~]# ls /tmp/

yyyy.txt

2、nfs

[root@m0 ~]# vim test004.yml 

---
-       hosts:          sremote_user:    roottasks:-       name:   安装nfs-utilsyum:    name=nfs-utils  state=present-       name:   安装rpcbindyum:    name=rpcbind    state=present-       name:   创建共享文件file:   path=/static    state=directory-       name:   配置文件shell:  echo '/static   *(ro,sync)' > /etc/exports-       name:   启动nfsservice:        name=nfs        state=started   enabled=yes-       name:   启动rpcbindservice:        name=rpcbind    state=started   enabled=yes-        hosts:  192.168.8.158remote_user:    roottasks:-       name:   安装nfs-utilsyum:    name=nfs-utils  state=latest-       name:   创建挂载目录file:   path=/nfs       state=directory-       name:   挂载nfscommand:        mount -t nfs 192.168.8.157:/static /nfs

[root@m0 ~]# ansible-playbook ./test004.yml

[root@s1 ~]# ls /nfs/
test

五、roles(难点)
1、roles介绍

roles(角色):就是通过分别将variables,tasks及handlers等放置于单独的目录中,并可以便捷地调用它们的一种机制。

假设我们要写一个playbook来安装管理lamp环境,那么这个playbook就会写很长。所以我们希望把这个很大的文件分成多个功能拆分,分成apache管理,php管理,mysql管理,然后在需要使用的时候直接调用就可以了,以免重复写。就类似编程里的模块化的概念,以达到代码复用的效果。

http://www.lryc.cn/news/427693.html

相关文章:

  • 白骑士的C#教学实战项目篇 4.4 游戏开发
  • 在Vue工程中开发页面时,发现页面垂直方向出现两个滚动条的处理
  • 【C++初阶】:C++入门篇(一)
  • 【JAVA CORE_API】Day14 Collection、Iterator、增强for、泛型、List、Set
  • Go更换国内源配置环境变量
  • 澎湃认证显实力,浪潮信息存储兼容新篇章
  • Leetcode 3255. Find the Power of K-Size Subarrays II
  • Kotlin学习02-变量、常量、整数、浮点数、操作符、元组、包、导入
  • C++的模板简介
  • 树莓派5 笔记25:第一次启动与配置树莓派5_8G
  • Melittin 蜂毒肽;GIGAVLKVLT TGLPALISWI KRKRQQ
  • day32
  • 【clickhouse】 使用 SQLAlchemy 连接 ClickHouse 数据库的完整指南
  • 按键收集单击,双击和长按
  • 进程的异常终止
  • 并发编程 | Future是如何优化程序性能
  • Oracle笔记
  • LVS+Keepalived 双机热备
  • Web Image scr图片从后端API获取基本实现
  • 2024音频剪辑指南:探索四大高效工具!
  • “CSS”第一步——WEB开发系列13
  • IEEE802网络协议和标准
  • vulnhub靶机 DC-9(渗透测试详解)
  • javaweb的新能源充电系统pf
  • 如何在桌面同时展示多个窗口
  • The Sandbox 游戏制作教程(第 5 部分):创建基于分类的系统
  • HTML浏览器缓存(Browser Cache)
  • 短剧APP系统,推动短剧市场发展
  • 嵌入式 | 嵌入式 Linux 系统使用摄像头
  • C 开源库之cJSON