Ansible is pretty amazing system admin tool.
目的: 通过 ansible 操作远程机器,对机器上的程序停用操作
扩展: 通过 ansible 批量操作远端机器,在机器上进行权限内的任意操作
准备
远端服务器准备
- 使用 apollo-portal.jar 进行测试 ,可替换成其他任何后台运行程序
- 确认 apollo-portal.jar 后台运行正常
- 检查进程号 ID 4174
root@pTestApp212:~# ps -ef |grep apollo-portal.jar
develop 4174 1 53 13:43 ? 00:00:47 apollo-portal.jar
root 1414 17087 0 13:45 pts/1 00:00:00 grep apollo-portal.jar
ansible 准备
- ansible 可用
- ansible 配置路径说明
/etc/ansible
Ansible 操作及配置
操作流程说明
- 文件
/etc/ansible/hosts
填入远程主机 IP 放入分组javagroup
内 - 创建文件
/opt/kill_apollo-portal.yml
可运行 ansible tasks 进行 kill java 进程 - 运行
ansible-playbook -i /opt/kill_apollo-portal.yml
- 检查远端机器进程情况
hosts 文件
编辑文件 /etc/ansible/hosts
[ydh_servers]
#192.168.1.111
[javagroup]
192.168.1.212
kill_apollo-portal.yml 文件
创建文件 /opt/kill_apollo-portal.yml
---
- hosts: javagroup
become: yes
tasks:
- name: Get running processes list from remote host
ignore_errors: yes
shell: "kill -9 `ps -aux | grep apollo-portal |grep -v grep| awk '{print $2}'`"
- 将进行强制结束进程
kill -9 PID
kill -9 `ps -aux |grep apollo-portal |grep -v grep |awk '{print $2}'`
执行 ansible playbook
ansible-playbook -b -vvv -u root /opt/kill_pid.yml
执行结果如下:
Using /etc/ansible/ansible.cfg as config file
PLAYBOOK: kill_apollo-portal.yml ****************************************************************************************
1 plays in /opt/kill_apollo-portal.yml
PLAY [javagroup] ********************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
Using module file /usr/local/lib/python2.7/dist-packages/ansible/modules/system/setup.py
ok: [192.168.1.212]
META: ran handlers
TASK [Get running processes list from remote host] **********************************************************************
task path: /opt/kill_apollo-portal.yml:6
changed: [192.168.1.212] => {
"changed": true,
"cmd": "kill -9 `ps -aux | grep apollo-portal |grep -v grep| awk '{print $2}'`",
"delta": "0:00:00.017110",
"end": "2020-06-05 14:07:34.427771",
"invocation": {
"module_args": {
"_raw_params": "kill -9 `ps -aux | grep apollo-portal |grep -v grep| awk '{print $2}'`",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
}
},
"rc": 0,
"start": "2020-06-05 14:07:34.410661",
"stderr": "",
"stderr_lines": [],
"stdout": "",
"stdout_lines": []
}
META: ran handlers
META: ran handlers
PLAY RECAP **************************************************************************************************************
192.168.1.212 : ok=2 changed=1 unreachable=0 failed=0
验证结果
远程查看机器是否存在这个应用在运行
root@pTestApp212:~# ps -ef |grep apollo-portal.jar
root 4017 17087 0 14:05 pts/1 00:00:00 grep apollo-portal.jar
扩展
- shell 中命令可替换成任何的 linux 命令,可以做更多的事
- yml 文件中 tasks 可以有多个,可以把命令拆解成多段,做异常处理
---
- hosts: javagroup
become: yes
tasks:
- name: make a director list from remote host
ignore_errors: yes
shell: "mkdir /opt/test"
- name: touch a file list from remote host
ignore_errors: yes
shell: "touch /opt/test.txt"
- hosts 文件的
javagroup
中可以添加多个机器,进行批量操作
[javagroup]
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5