List of running processes form multiple linux hosts using with_items

12 views
Skip to first unread message

smitconsu...@gmail.com

unread,
Apr 11, 2018, 4:12:45 AM4/11/18
to Ansible Project
Hi 
I am trying to achieve all running processes on our linux machines via ansible and get output in a file along with each host name and processes. I have made below playbook but not getting to the point how to get list of processes for each host one by one and store in a file. Can anyone please help with the code how can I achieve this? I am using with_items with list of hosts which is coming from inventory group hostlist. many thanks

---
- hosts: all
  gather_facts: false
  tasks:

  - name: Get Running Processes
    command: ps -ef
    delegate_to: "{{ item }}"
    run_once: false
    with_items:
     - "{{ groups['hostlist'] }}"
    register: process_list

Bishwajit Samanta

unread,
Apr 12, 2018, 3:09:41 AM4/12/18
to Ansible Project
I just changed the host as devops
---

- hosts: devops
  gather_facts: no
  tasks:
    - name: Get the running process across the systems
      command: ps -ef
      register: output

    - name: Getting some details of systems
      local_action: copy content={{ output }} dest=/opt/{{ inventory_hostname }}file

Output::-
-------------

[root@ansible-master opt]# ls
192.168.56.111file  192.168.56.116file 

You can try with this and see if your problem get resolved.

smitconsu...@gmail.com

unread,
Apr 13, 2018, 4:11:22 AM4/13/18
to Ansible Project
HI Bishwajit Samanta

Thanks. It is creating file for each hostname but data is not in readable format. Are you able to do favor little more, how to get the data in each file as orignal form like as processes list show on linux machine? I was initially thinking to get output from all hosts in one file and somehow get processes list along with hostname one by one. But this can work as well. Many Thanks.

Kai Stian Olstad

unread,
Apr 13, 2018, 11:14:28 AM4/13/18
to ansible...@googlegroups.com
On Friday, 13 April 2018 10.11.21 CEST smitconsu...@gmail.com wrote:

> Thanks. It is creating file for each hostname but data is not in readable
> format. Are you able to do favor little more, how to get the data in each
> file as orignal form like as processes list show on linux machine?

Replace {{ output }} with {{ output.stdout }}


> I was
> initially thinking to get output from all hosts in one file and somehow get
> processes list along with hostname one by one. But this can work as well.

To do that you need to use jinja template, something like this should work.

- name: Getting some details of systems
copy:
content: |
{% for i in ansible_play_hosts %}
Output for host {{ i }}:
{{ hostvars[i].output.stdout }}

{% endfor %}
dest: /my/file
delegate: localhost


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages