Ansible - Cum să Grep (ps -few) și să omorâți orice proces Linux care rulează pe gazdă la distanță?
Publicat: 2022-02-24Ansible este un instrument de administrare a sistemului destul de uimitor. Am publicat un număr de articole pe Ansible în ultimele săptămâni despre cum să copiați fișiere pe gazda la distanță, Cum să executați comenzi pe gazde la distanță, cum să instalați Java, Python pe o gazdă la distanță și așa mai departe.
În acest tutorial, vom analiza cum să grep procesul java care rulează pe o gazdă la distanță și să omorâm acel proces de la distanță folosind un manual de joc simplu ansible.
Iată pașii pe care îi vom face în acest tutorial:
- Pe gazda de la distanță, rulați CrunchifyAlwaysRunningProgram.java
- Urmați tutorialul despre Cum să rulați un program pentru totdeauna în Java
- rulați programul java folosind
nohup java CrunchifyAlwaysRunningProgram &
1 2 3 |
ubuntu @ ip - 172 - 31 - 10 - 150 : ~ $ nohup java CrunchifyAlwaysRunningProgram & [ 1 ] 18174 ubuntu @ ip - 172 - 31 - 10 - 150 : ~ $ nohup : ignoring input and appending output to 'nohup.out' |
Cum să verific dacă procesul este pornit și rulează pe gazda la distanță?
verificați ID 18174
.
1 2 3 |
ubuntu @ ip - 172 - 31 - 10 - 150 : ~ $ ps - few | grep CrunchifyAlwaysRunningProgram ubuntu 18174 15069 1 15 : 15 pts / 0 00 : 00 : 00 java CrunchifyAlwaysRunningProgram ubuntu 18187 15069 0 15 : 16 pts / 0 00 : 00 : 00 grep -- color = auto CrunchifyAlwaysRunningProgram |
- creați fișierul fișier
crunchify-hosts
care are IP gazdă la distanță - creați fișierul
crunchify-grep-kill-process.yml
cu sarcini ansible pentru grep și kill java - executați comanda: ansible-playbook -i ./crunchify-hosts crunchify-grep-kill-process.yml
- verificați rezultatul pe consola terminalului macOS
fișier crunchify-hosts
1 2 3 4 5 6 7 8 9 10 |
[ local ] localhost ansible_connection = local ansible_python_interpreter = python [ crunchify ] 3.16.83.84 [ crunchify : vars ] ansible_ssh_user = ubuntu ansible_ssh_private_key_file =/ Users / crunchify / Documents / ansible / crunchify . pem ansible_python_interpreter =/ usr / bin / python3 |
Fișierul conține adresa IP de la distanță și acreditările care vă vor ajuta să vă conectați fără parolă.
fișier crunchify-grep-kill-process.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
--- - hosts : crunchify become : yes tasks : - name : Get running processes list from remote host ignore_errors : yes shell : "ps -few | grep CrunchifyAlwaysRunningProgram | awk '{print $2}'" register : running_processes - name : Kill running processes ignore_errors : yes shell : "kill {{ item }}" with_items : "{{ running_processes.stdout_lines }}" - wait_for : path : "/proc/{{ item }}/status" state : absent with_items : "{{ running_processes.stdout_lines }}" ignore_errors : yes register : crunchify_processes - name : Force kill stuck processes ignore_errors : yes shell : "kill -9 {{ item }}" with_items : "{{ crunchify_processes.results | select('failed') | map(attribute='item') | list }}" |
Aici fișierul Ansible Playbook primește toate procesele java, omorându-l folosind comanda simplă kill -9
.

Executați Ansible Playbook:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
bash1 . 2 $ ansible - playbook - i . / crunchify - hosts crunchify - grep - kill - process . yml PLAY [ crunchify ] ************************************************************************************************************************************************************** TASK [ Gathering Facts ] ******************************************************************************************************************************************************** ok : [ 3.16.83.84 ] TASK [ Get running processes list from remote host ] **************************************************************************************************************************** changed : [ 3.16.83.84 ] TASK [ Kill running processes ] ************************************************************************************************************************************************* changed : [ 3.16.83.84 ] = > ( item = 18174 ) failed : [ 3.16.83.84 ] ( item = 18342 ) = > { "changed" : true , "cmd" : "kill 18342" , "delta" : "0:00:00.002602" , "end" : "2019-05-10 15:20:36.957062" , "item" : "18342" , "msg" : "non-zero return code" , "rc" : 1 , "start" : "2019-05-10 15:20:36.954460" , "stderr" : "/bin/sh: 1: kill: No such process" , "stderr_lines" : [ "/bin/sh: 1: kill: No such process" ] , "stdout" : "" , "stdout_lines" : [ ] } failed : [ 3.16.83.84 ] ( item = 18344 ) = > { "changed" : true , "cmd" : "kill 18344" , "delta" : "0:00:00.002648" , "end" : "2019-05-10 15:20:38.479354" , "item" : "18344" , "msg" : "non-zero return code" , "rc" : 1 , "start" : "2019-05-10 15:20:38.476706" , "stderr" : "/bin/sh: 1: kill: No such process" , "stderr_lines" : [ "/bin/sh: 1: kill: No such process" ] , "stdout" : "" , "stdout_lines" : [ ] } . . . ignoring TASK [ wait_for ] *************************************************************************************************************************************************************** ok : [ 3.16.83.84 ] = > ( item = 18174 ) ok : [ 3.16.83.84 ] = > ( item = 18342 ) ok : [ 3.16.83.84 ] = > ( item = 18344 ) TASK [ Force kill stuck processes ] ********************************************************************************************************************************************* PLAY RECAP * ******************************************************************************************************************************************************************* 3.16.83.84 : ok = 4 changed = 2 unreachable = 0 failed = 0 |
Cum se verifică?
Doar încercați să procesați grep din nou pe gazda la distanță.
1 2 |
ubuntu @ ip - 172 - 31 - 10 - 150 : ~ $ ps - few | grep CrunchifyAlwaysRunningProgram ubuntu 18484 15069 0 15 : 22 pts / 0 00 : 00 : 00 grep -- color = auto CrunchifyAlwaysRunningProgram |
După cum observați, nu veți vedea process ID 18174
în listă și nu există niciun proces java care rulează.
Asta e.
Acesta este cel mai simplu mod de a grep proces Java și de a ucide folosind Ansible. Anunțați-mă dacă vă confruntați cu vreo problemă la rularea acestui Ansible playbook
.