云计算day28

一、ansible模块
script模块
root@localhost \~# 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@localhost \~# source test000.sh
well done
root@localhost \~# ansible group02 -m script -a './test000.sh'

copy模块
root@localhost \~# ansible s -m copy -a 'src=./mysql_master.sh dest=~'

使用command模块下载 nfs-utils rpcbind
root@localhost \~# ansible s -m command -a 'yum -y install nfs-utils'
root@localhost \~# ansible s -m command -a 'yum -y install rpcbind'

root@localhost \~# vim /etc/exports

/static *(ro,sync)
在被控制的主机上添加static目录,并创建test文件
root@localhost \~# ansible s -m file -a 'path=/static state=directory'

root@localhost \~# ansible s -m file -a 'path=/static/test state=touch

root@localhost \~# ansible s -m copy -a 'src=/etc/exports dest=/etc/exports'

command模块
root@localhost \~# ansible s -m command -a 'systemctl start nfs'

root@localhost \~# ansible s -m command -a 'systemctl enable nfs'

service模块
root@localhost \~# ansible s -m service -a 'name=rpcbind state=started enabled=yes'
root@localhost \~# yum -y install nfs-utils
root@localhost \~# mkdir /nfs
root@localhost \~# mount -t nfs 192.168.8.157:/static /nfs/
root@localhost \~# 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: root
    tasks:
  • name: 任务说明
    模块: key0=value0
    service: name=vsftpd state=started anabled=yes
  • name: 修改配置文件
    command: sed...
    notify:
  • ancdef

handler:

  • name: abcdef
    service name=vsftpd stste=restared
    ...

三、playbook编排vsftpd
1、安装
root@localhost \~# vim test001.yml


  • hosts: s #使用tab键对齐 指定组
    remote_user: root #
    tasks:
  • name: 安装vsftpd
    yum: name=vsftpd state=latest
    root@localhost \~# ansible-playbook ./test001.yml

2、卸载
root@localhost \~# vim test001.yml


  • hosts: s
    remote_user: root
    tasks:
  • name: 卸载vsftp
    yum: name=vsftpd state=absent
  • name: 安装vsftpd
    yum: name=vsftpd state=latest
    root@localhost \~# ansible-playbook ./test001.yml

3、启动服务
root@localhost \~# vim test001.yml


  • hosts: s
    remote_user: root
    tasks:
  • name: 卸载vsftp
    yum: name=vsftpd state=absent
  • name: 安装vsftpd
    yum: name=vsftpd state=latest
  • name: 启动服务
    service: name=vsftpd state=started e
    nabled=yes
    root@localhost \~# ansible-playbook ./test001.yml

4、修改配置文件设置不允许匿名访问并重启
handlers:类似task,但需要使用notify通知调用。

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

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

root@localhost \~# yum -y install lftp

root@localhost \~# 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@localhost \~# vim test001.yml


  • hosts: s
    remote_user: root
    tasks:

  • name: 卸载vsftp
    yum: name=vsftpd state=absent

  • name: 安装vsftpd
    yum: 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.conf
    notify: #调用

  • abcdefg
    handlers:

  • name: abcdefg
    service: name=vsftpd state=res
    tarted

root@localhost \~# ansible-playbook ./test001.yml

root@localhost \~# lftp 192.168.8.157
lftp 192.168.8.157:~> exit
root@localhost \~# lftp 192.168.8.157
lftp 192.168.8.157:~> ls
中断

相关推荐
rich_Chan.3 小时前
【2026 实操】bl video edit 教程:已拍素材改风格换颜色全流程,含输出规格、角标自查
人工智能·ai·云计算·aigc
loulanyue_9 小时前
从沙箱隔离到统一运营:腾讯云Agent工程全景解密AgentHosting、AgentCFS与TeamAI开源——读2026全球数字生态大会实录
开源·云计算·腾讯云
主机哥哥11 小时前
2026年阿里云双十一大促活动详细解读
阿里云·云计算
杰克说12 小时前
人工智能前沿部署工程师(FDE),第五章·管理端·企业后台与WMA配置实战
人工智能·云计算·腾讯云·fde
运维行者_1 天前
供应链安全成最大风险源,企业如何构建可控的运维体系?
运维·开发语言·网络·数据库·安全·云计算·流量监控
静以修身zzz2 天前
计算云服务
运维·网络·云计算
Henry-SAP2 天前
阿里云大模型份额居首
阿里云·云计算
长不大的杨瑞杰2 天前
OpenCode v2 运行时预热实战:Warming、Prompt Cache、后台服务与成本治理
人工智能·安全·云计算·软件工程·devops
腾讯云大数据3 天前
腾讯云发布一站式多模态数据处理与模型训推方案,助力Physical AI释放生产力
大数据·人工智能·云计算·腾讯云
sbjdhjd3 天前
网工进阶笔记:H3CSE 园区网全栈技术与协议汇总
网络·笔记·云计算·ssh·网络工程·vlan·aaa