Ansible: ¿cómo Grep (ps -few) y eliminar cualquier proceso de Linux que se ejecute en el host remoto?
Publicado: 2022-02-24Ansible es una herramienta de administración de sistemas bastante sorprendente. Hemos publicado varios artículos en Ansible en las últimas semanas sobre cómo copiar archivos en un host remoto, cómo ejecutar comandos en hosts remotos, cómo instalar Java, Python en un host remoto, etc.
En este tutorial, repasaremos cómo grep el proceso Java que se ejecuta en un host remoto y eliminaremos ese proceso remoto usando un simple libro de jugadas ansible.
Estos son los pasos que haremos en este tutorial:
- En el host remoto, ejecute CrunchifyAlwaysRunningProgram.java
- Siga el tutorial sobre cómo ejecutar un programa para siempre en Java
- ejecutar el programa java usando
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' |
¿Cómo verificar si el proceso se inicia y se ejecuta en un host remoto?
echa un vistazo al 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 |
- crear archivo
crunchify-hosts
archivo que tiene IP de host remoto - cree el archivo
crunchify-grep-kill-process.yml
con tareas ansible para grep y kill java process - ejecutar el comando: ansible-playbook -i ./crunchify-hosts crunchify-grep-kill-process.yml
- verifique el resultado en la consola de terminal macOS
archivo de hosts de crunchify
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 |
El archivo contiene una dirección IP remota y credenciales que ayudarán a ansible a iniciar sesión sin contraseña.
archivo 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 }}" |
Aquí, el archivo del libro de jugadas de ansible está obteniendo todos los procesos de Java, eliminándolos con el comando simple kill -9
.

Ejecute el libro de jugadas de Ansible:
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 |
¿Cómo verificar?
Simplemente intente procesar grep nuevamente en el host remoto.
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 |
Como notará, no verá process ID 18174
en la lista y no hay ningún proceso Java en ejecución.
Eso es todo.
Esta es la forma más sencilla de grep del proceso de Java y matar usando Ansible. Avíseme si tiene algún problema al ejecutar este libro de Ansible playbook
.