Ansible 工作中常用命令

1、查看底座升级主机docker stat运行状态查看

/usr/local/bin/ansible -i /data/www/shengjipintai/docker_dzsj_hosts all -m shell -a "date '+%Y/%m/%d %H:%M:%S'  && docker stats  --no-stream" | tee -a /data/2022-08-31.log

2、逐台重启docker某个服务 --forks=1表示并发执行服务器数量

ansible -i /data/soft/shell/hosts docker -m  shell -a "docker restart  ylxf-ms-user-rest && sleep 10  " --forks=1

3、综合平台代码更新(一台一台更新)

ansible -i /data/soft/shell/hosts zhpt -m win_copy -a  "src=/data/Egov/ dest=d:/project/Egov/" --forks=1

4、综合平台指定地州文件更新

ansible -i /data/soft/shell/hosts lj -m win_copy -a  "src=/data/Ljegov/ dest=d:/project/EGov_LJ/"

5、综合平台代码更新(一套代码更新多个文件夹,比如更新文件夹 PSNSServices8850 到 PSNSServices8854 ,一共五个文件夹)

ansible -i /data/soft/shell/hosts zhpt -m win_copy -a  "src=/data/egov/ dest=d:/project/Api/PSNSServices885[0-4]"
ansible -i /data/soft/shell/hosts zhpt -m win_shell -a "iisreset"

6、逐台重启docker所有服务 --forks=1表示并发执行服务器数量

ansible -i /data/www/shengjipintai/docker_dzsj_hosts 10.10.29.15 -m shell -a "docker restart \$(docker ps -a -q) && sleep 30 " --forks=1

7、批量重启docker服务中某一个服务,这里指定服务为:ylxf-party-org-rest

ansible -i /data/soft/shell/hosts docker -m  shell -a "docker restart \$(docker ps -a | grep ylxf-party-org-rest | awk '{ print $1}'"

8、获取IP地址

ansible -i /data/www/shengjipintai/docker_dzsj_hosts all -m shell -a " echo /sbin/ifconfig | grep 'inet ' | awk 'NR==2 {print $2}'`"

9、nginx日志分割,第一步执行设置nginx日志脚本,第二步设置计划任务

ansible -i /data/www/shengjipintai/docker_nginx_hosts nginx -m script -a 'chdir=/data /data/www/wwwroot/10.10.38.20/shell/logrotate_nginx.sh'
ansible -i /data/www/shengjipintai/docker_nginx_hosts_all nginx -m cron -a "minute=59 hour=23 name=logrotate_nginx job='/usr/sbin/logrotate -f /etc/logrotate.d/nginx' disabled=no"

10、分发服务配置备份脚本,设定定时任务计划

测试连通性

ansible -i /data/www/shengjipintai/docker_nginx_hosts nginx -m ping

拷贝配置备份脚本

ansible -i /data/www/shengjipintai/docker_nginx_hosts nginx -m copy -a "src=/data/www/wwwroot/10.10.38.20/shell/backup_peizhi_docker_data_nginx.sh dest=/data/ owner=root group=root mode=0755"

远程执行脚本

ansible -i /data/www/shengjipintai/docker_nginx_hosts nginx -m script -a 'chdir=/data /data/www/wwwroot/10.10.38.20/shell/backup_peizhi_docker_data_nginx.sh'

设置任务计划 夜里两点开始备份

ansible -i /data/www/shengjipintai/docker_nginx_hosts nginx -m cron -a "minute=0 hour=2 weekday=0 name=backup_config job='/data/backup_peizhi_docker_data_nginx.sh' disabled=no"

11、docker查看目录映射

docker inspect -f "{{ .HostConfig.Binds }}" 容器名称或容器ID

12、查看docker容器内部开放有端口

docker port 容器id

13、docker内容反推两个小工具

  • runlike:通过运行容器逆向打印出容器的启动命令
  • whaler:通过镜像逆向导出dockerfile

runlike 用法:

安装runlike的用法:runlike -p redis
非安装runlike用法:docker run -v /var/run/docker.sock:/var/run/docker.sock assaflavie/runlike 你的容器名称

runlike安装:

# pip安装
yum install python3-pip && pip3 install runlike && pip3 install runlike
# 免安装调用docker
alias runlike="docker run --rm -v /var/run/docker.sock:/var/run/docker.sock assaflavie/runlike"

whaler 安装

# 免安装调用docker
alias whaler="docker run -t --rm -v /var/run/docker.sock:/var/run/docker.sock:ro pegleg/whaler"

whaler用法:whaler -sV=1.36 容器镜像

参考文章:https://blog.csdn.net/fly910905/article/details/118994320

14、使用ansible批量操作windows服务器的文件夹本地磁盘拷贝执行命令:

ansible -i /data/soft/shell/hosts cs -m win_shell -a  "xcopy D:\project\Egov\ D:\fpf /y /e /i /q"

windows系统xcopy参数说明:

## 示例2:
例:将a文件夹内的所有内容(包括子文件夹)复制到b文件夹
xcopy a\* b /y /e /i /q

说明:
/y:不弹出“确认是否覆写已存在目标文件”的提示
/e:复制文件及子文件夹内所有内容,包括空文件夹(对比/s, /s不复制空文件夹)
/i:如果b不存在并且复制超过一个文件则默认b是目录名
/q:quiet,静默模式

参考文章:https://www.jianshu.com/p/86f367a03f9c

15、批量windows系统修补漏洞命令

0、编写主机清单

写法一:(适合账号密码完全相同的服务器使用)

[windows]
192.168.1.11
[windows:vars]
ansible_ssh_user="Administrator"
ansible_ssh_pass="123456"
ansible_ssh_port=5986
ansible_connection="winrm"

使用时候用法:

$ ansible windows -m win_ping
192.168.1.11 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}

写法二:(适合一机一个账号一个密码密码使用)

[windows]
192.168.1.11 ansible_ssh_user="Administrator" ansible_ssh_pass="123456" ansible_ssh_port=5986 ansible_connection="winrm"

要注意的是 端口方面ssl即https方式的使用5986,http使用5985

1.测试连通性

 ansible -i /data/www/hosts 10.10.20.144 -m win_ping

2.使用ansible命令将这个补丁复制到D盘

 ansible -i /data/www/Win_updata_list Win_Updata -m win_copy -a 'src=/data/Windows补丁/CVE-2018-0886/windows10.0-kb4103723-x64.msu dest=D:\\windows10.0-kb4103723-x64.msu'

注释:Win_updata_list为主机清单文件,Win_Updata为主机清单上的一个组

3、执行漏洞修复命令

wusa命令解析如下:

扩展:CMD安装MSI文件

msiexec /package "你的安装MSI文件的全路径".

cmd中执行修补漏洞命令如下:

wusa d:\windows10.0-kb4103723-x64.msu /quiet /norestart /log

ansible发送的修补漏洞命令:

 ansible -i /data/www/Win_updata_list Win_Updata -m win_shell -a "wusa D:\\windows10.0-kb4103723-x64.msu /quiet /norestart /log"

参考文章:https://blog.51cto.com/mengix/1947297

4、重启服务器命令:

任选一下一种即可:
$ansible windows -m win_reboot
$ansible windows -m win_command -a 'shutdown -s -t 0'
$ansible windows -m win_shell -a "shutdown -f -s -t 1"

5、删除补丁包:

 ansible -i /data/www/Win_updata_list Win_Updata -m win_file -a 'dest=d:\\windows10.0-kb4103723-x64.msu state=absent'

ansible执行Windows批量处理命令小结:

ping远程windows主机:
$ ansible 192.168.2.2 -m win_ping
创建目录:
$ ansible 192.168.2.2 -m win_file -a 'path=D:\\test state=directory'
下发文件
$ ansible 192.168.2.2 -m win_copy -a 'src=/etc/hosts dest=D:\\hosts.txt'
删除文件
$ ansible 192.168.2.2 -m win_file -a 'dest=d:\\config_dir\\hosts.txt state=absent'
删除目录
$ ansible 192.168.2.2 -m win_file -a 'dest=d:\\config_dir2 state=absent'
执行cmd命令
$ ansible 192.168.2.2 -m win_shell -a 'ipconfig'
重启windows
$ ansible 192.168.2.2 -m win_reboot
$ ansible 192.168.2.2 -m win_shell -a 'shutdown -r -t 0'
创建用户(远程在windows客户端上创建用户)
$ ansible 192.168.2.2 -m win_user -a "name=testuser1 passwd=123456"
windows服务管理
$ ansible 192.168.2.2 -m win_shell -a “net stop|start zabbix_agent”

更多ansible.windows 模块集合索引参考:https://docs.ansible.com/ansible/latest/collections/ansible/windows/index.html

16、其他新命令(待补充)

版权声明:
作者:WaterBear
链接:https://l-t.top/2505.html
来源:雷霆运维
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>
文章目录
关闭
目 录