常用模块的使用(详情)
- 运维发布方式有哪些?
线上平稳发布(部署)手段:
-
蓝绿部署
用于0 downtime应用上线时的一套部署策略 不停止老版,额外搞一套新版本,等测试发现新版本OK后,删除老版本。
斜体样式在部署新版本之前,需要将部署新版本的流量掐断,全部打到ok的老版本上。
缺点:需要有两倍的机器资源,得保持冗余的服务始终在线,
优点:两个版本可能都可能组任何时间点获取流量 -
灰度发布(又名金丝雀发布)
在不停止老版本的情况下,额外搞一套新版本,经常与A/B测试一起使用,用于测试选择多种方案。从30-70的流量比例开始,慢慢调大新系统的接入流量比例,直到彻底完成新旧更替。
优点:既能做风控又能辅助产品做决策
缺点:发布自动化程度不够,发布期间可引发服务中断。 -
滚动发布
一般是取出一个或者多个服务器停止服务,执行更新,并重新将其投入使用。周而复始,直到集群中所有的实例都更新成新版本。
优点:比蓝绿部署节约资源
缺点:服务器节点数量多,会很慢 -
红黑部署
这是Netflix采用的部署手段,Netflix的主要基础设施是在AWS上,所以它利用AWS的特性,在部署新的版本时,通过AutoScaling Group用包含新版本应用的AMI的LaunchConfiguration创建新的服务器。测试不通过,找到问题原因后,直接干掉新生成的服务器以及Autoscaling
Group就可以,测试通过,则将ELB指向新的服务器集群,然后销毁掉旧的服务器集群以及AutoScaling Group。
优点:服务始终在线,同时采用不可变部署的方式
缺点:只有一个版本组任何时间点获取流量
效果验证手段:
- AB测试
同时也叫对照实验(在几种方案中选择最优的方案)
这是一种产品优化中的应用方法,在产品正式迭代发版本之前,为同一个目标定制两个或以上种方案,将用户流量对应分组,在确保每组用户特征相同的前提下,让用户分别看到不同的方案设计,根据每组用户的真实数据反馈,科学的帮助产品进行决策。
简单说就是将多种设计方案暴露给用户,分组对照测试,让用户在使用后决策哪种更优。
A/B测试的应用方式决定了它拥有的三大特性:
- 先验性、并行性和科学性。
- 先验性: 分组对照试验,由试验结果得出结论。
- 并行性: 多组试验同环境同时间进行,更加科学客观对比优劣,且节省验证时间。
- 科学性: 指的是流量分配的科学性——将相似特征的用户均匀的分配到试验组中,确保每个组的用户特征相似,避免有数据偏差,使得结论更具有代表性。
ansible常用模块有:
- ping
- yum
- template
- copy
- user
- group
- service
- raw
- command
- shell
- script
注意:
- shell模块调用的/bin/sh指令执行
- command模块不是调用的shell的指令,所以没有bash的环境变量
- raw在很多地方和shell类似,但建议使用shell和command模块多一点。如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了
- shell、command、raw 被称为万能模块,除了需要交互的命令以外,几乎所有命令都可以执行,但他们有个共同的缺点 (不具备幂等性)
- 应尽量避免使用这三个模块来执行命令,因为其他模块大部分都是幂等性的,可以自动进行更改跟踪
模块一:
ping
测试主机是否是通的,不涉及参数(若为通,则出现的回复为pong)
[root@localhost ~]# ansible all -m ping
192.168.142.192 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
模块二:
yum
使用yum包管理器来管理软件包
常用参数:
参数名 | 说明 |
---|---|
config_file | yum的配置文件 |
disable_gpg_check | 关闭gpg_check |
disablerepo | 不启用某个源 |
enablerepo | 启用某个源 |
注意:name:要进行操作的软件包的名字,也可以传递一个url或者一个本地的rpm包的路径
state:状态(present,absent,latest)
[root@localhost ~]# ansible web -m yum -a 'name=lly state=absent'
192.168.142.192 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "",
"rc": 0,
"results": [
"lly is not installed"
]
}
模块三:
template
使用了Jinjia2格式作为文件模版,进行文档内变量的替换的模块。(它的每次使用都会被ansible标记为”changed”状态)
常用参数:
参数名 | 是否必须 | 默认值 | 选项 | 说明 |
---|---|---|---|---|
backup | no | no | yes/no | 建立个包括timestamp在内的文件备份,以备不时之需 |
dest | yes | 远程节点上的绝对路径,用于放置template文件 | ||
group | no | 设置远程节点上的的template文件的所属用户组 | ||
mode | no | 设置远程节点上的template文件权限。类似Linux中chmod的用法 | ||
owner | no | 设置远程节点上的template文件所属用户 | ||
src | yes | 本地Jinjia2模版的template文件位置 |
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# echo "hello" > index.teml
[root@localhost yum.repos.d]# ls
CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo index.teml
CentOS-CR.repo CentOS-Media.repo epel.repo
CentOS-Debuginfo.repo CentOS-Sources.repo epel-testing.repo
[root@localhost yum.repos.d]# ansible web -m template -a "src=/etc/yum.repos.d/index.teml dest=/etc/yum.repos.d/index.teml"
192.168.142.192 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "f572d396fae9206628714fb2ce00f72e94f2258f",
"dest": "/etc/yum.repos.d/index.teml",
"gid": 0,
"group": "root",
"md5sum": "b1946ac92492d2347c6235b4d2611184",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:system_conf_t:s0",
"size": 6,
"src": "/root/.ansible/tmp/ansible-tmp-1626596325.1-3176-240513364177824/source",
"state": "file",
"uid": 0
}
#去192.168.142.192主机查看是否成功
[root@computer ~]# cd /etc/yum.repos.d/
[root@computer yum.repos.d]# ls
CentOS-Base.repo CentOS-Media.repo CentOS-Storage-common.repo
CentOS-Ceph-Nautilus.repo CentOS-NFS-Ganesha-28.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-OpenStack-train.repo CentOS-x86_64-kernel.repo
CentOS-Debuginfo.repo CentOS-QEMU-EV.repo index.teml
CentOS-fasttrack.repo CentOS-Sources.repo
模块四:
copy
用于复制文件至远程受控机
[root@localhost ansible]# ansible web -m copy -a "src=/etc/ansible/scr/ak.sh dest=/etc/yum.repos.d/scr/"
192.168.142.192 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/etc/yum.repos.d/scr/ak.sh",
"gid": 0,
"group": "root",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:system_conf_t:s0",
"size": 0,
"src": "/root/.ansible/tmp/ansible-tmp-1626597396.38-3989-48481669162374/source",
"state": "file",
"uid": 0
}
[root@localhost ansible]# ansible web -m shell -a "ls /etc/yum.repos.d/scr/"192.168.142.192 | CHANGED | rc=0 >>
ak.sh
模块五:
user
用于管理受控机的用户帐号
[root@localhost ~]# ansible web -m user -a 'name=test1 password=1'
[WARNING]: The input password appears not to have been hashed. The 'password'
argument must be encrypted for this module to work properly. #提醒:密码必须加密后此模板才可以正常运行
192.168.142.192 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
"home": "/home/test********",
"name": "test********",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER"
}
模块六:
group
用于在受控机上添加或删除组
[root@localhost ~]# ansible web -m group -a "name=you gid=789 state=present" #添加组
192.168.142.192 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 789,
"name": "you",
"state": "present",
"system": false
}
[root@localhost ~]# ansible web -m shell -a "grep you /etc/group" #查看是否添加成功
192.168.142.192 | CHANGED | rc=0 >>
you:x:789:
[root@localhost ~]# ansible web -m group -a "name=you state=absent" #删除组
192.168.142.192 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "you",
"state": "absent"
}
[root@localhost ~]# ansible web -m shell -a "grep you /etc/group" #查看是否删除成功
192.168.142.192 | FAILED | rc=1 >>
non-zero return code
模块七:
service
用于管理受控机上的服务
[root@localhost ~]# ansible web -m service -a "name=httpd state=started"
192.168.142.192 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "httpd",
"state": "started",
"status": {
"ActiveEnterTimestampMonotonic": "0",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "inactive",
"After": "nss-lookup.target remote-fs.target tmp.mount basic.target system.slice network.target -.mount systemd-journald.socket",
"AllowIsolate": "no",
"AmbientCapabilities": "0",
"AssertResult": "no",
"AssertTimestampMonotonic": "0",
"Before": "shutdown.target",
"BlockIOAccounting": "no",
"BlockIOWeight": "18446744073709551615",
"CPUAccounting": "no",
#次处省略剩余行数
[root@localhost ~]# ansible web -m shell -a "ss -antl" #查看是否开启
192.168.142.192 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:631 :::*
LISTEN 0 100 ::1:25 :::*
模块八:
raw
用于在远程主机上执行命令
[root@localhost ~]# ansible web -m raw -a "echo 'hello' > /tmp/test" #支持重定向
192.168.142.192 | CHANGED | rc=0 >>
Shared connection to 192.168.142.192 closed.
[root@localhost ~]# ansible web -a "cat /tmp/test "
192.168.142.192 | CHANGED | rc=0 >>
hello
[root@localhost ~]# ansible web -m raw -a "cat /tmp/test|grep -Eo hello" #支持管道符
192.168.142.192 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.142.192 closed.
模块九:
command
用于在远程主机上执行命令(指令模块、也是默认模块、不支持管道、通配符、脚本)
[root@localhost ~]# ansible web -m command -a 'ls /root'
192.168.142.192 | CHANGED | rc=0 >>
anaconda-ks.cfg
Docker
initial-setup-ks.cfg
ll
公共
模板
视频
图片
文档
下载
音乐
桌面
[root@localhost ~]# ansible web -a 'ls /root'
192.168.142.192 | CHANGED | rc=0 >>
anaconda-ks.cfg
Docker
initial-setup-ks.cfg
公共
模板
视频
图片
文档
下载
音乐
桌面
模块十:
shell
用于在受控机上执行受控机上的脚本,可直接在受控机上执行命令。
[root@localhost ~]# ansible web -m shell -a "grep you /etc/group"
192.168.142.192 | CHANGED | rc=0 >>
you:x:789:
模块十一:
script
用于在受控机上执行主控机上的脚本
[root@localhost ~]# ll /etc/ansible/scr/
总用量 0
-rw-r--r-- 1 root root 0 7月 18 16:28 ak.sh
[root@localhost ~]# ansible web -m script -a "/etc/ansible/scr/ak.sh & > /tmp/a"
192.168.142.192 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.142.192 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.142.192 closed."
],
"stdout": "",
"stdout_lines": []
}
[root@localhost ~]# ansible web -m shell -a "cat /tmp/a"
192.168.142.192 | CHANGED | rc=0 >>
模块十二:
lineinfile
用于确保某一行文本存在于指定的文件中或删除指定的文本,还可以根据正则表达式替换某一行文本
[root@localhost ~]# ansible web -a "cat /tmp/test"
192.168.142.192 | CHANGED | rc=0 >>
hello
1
2
1
[root@localhost ~]# ansible web -m lineinfile -a "path=/tmp/test line=3"
192.168.142.192 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@localhost ~]# ansible web -a "cat /tmp/test"
192.168.142.192 | CHANGED | rc=0 >>
hello
1
2
1
3
模块十三:
blockinfile
用于组指定的文件中插入一段文本,这段文本则为被标记过的,以便在以后的操作中可以通过标记找到这段文本,然后修改或者删除这段文本
[root@localhost ~]# ansible web -m blockinfile -a "path=/tmp/test block=hello_world"
192.168.142.192 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"msg": "Block inserted"
}
[root@localhost ~]# ansible web -a "cat /tmp/test"
192.168.142.192 | CHANGED | rc=0 >>
hello
1
2
1
3
# BEGIN ANSIBLE MANAGED BLOCK
hello_world
# END ANSIBLE MANAGED BLOCK