Hello. I have a task to collect some information from 100+ hosts and want 
to organize it in a table.
I want to use:

   1. Ansible for collecting information;
   2. CGI script to generate HTML page with collected information (the 
   information is some users last date login, last time of some files 
   modification (I get it with scripts by ssh) if you can advise some 
   monitoring service that supports similar operations, please do it).

The problem is that I'm new in Ansible and can't even understand if it's 
possible to do what I want with Ansible.

There's 2 hosts in group 'test'. Let's imagine I want to ls for some file 
somewhere (ls /var/someprogram.pid) and to know who is registered now 
("who").
My attemtpts:
1.
- hosts: test
  serial: 1
  user: dmitresso

  tasks:
  - shell: who
    register: response1

  - shell: ls /tmp/someprogram.pid
    register: response2
- hosts: local

  tasks:
  - debug:
      var: hostvars['item']['response1']['stdout_lines']
    with_items: "{{ groups['test'] }}"

Response:
dmitresso@host ~/ansible $ ansible-playbook pb.yml

PLAY [test] 
********************************************************************

TASK [setup] 
*******************************************************************
ok: [host1]

TASK [command] 
*****************************************************************
changed: [host1]

TASK [command] 
*****************************************************************
changed: [host1]

PLAY [test] 
********************************************************************

TASK [setup] 
*******************************************************************
ok: [host2]

TASK [command] 
*****************************************************************
changed: [host2]

TASK [command] 
*****************************************************************
changed: [host2]

PLAY [local] 
*******************************************************************

TASK [setup] 
*******************************************************************
ok: [localhost]

TASK [debug] 
*******************************************************************
ok: [localhost] => (item=host1) => {
    "hostvars['item']['response1']['stdout_lines']": "VARIABLE IS NOT 
DEFINED!",
    "item": "host1"
}
ok: [localhost] => (item=host2) => {
    "hostvars['item']['response1']['stdout_lines']": "VARIABLE IS NOT 
DEFINED!",
    "item": "host2"
}

PLAY RECAP 
*********************************************************************
host1  : ok=3    changed=2    unreachable=0    failed=0
host2  : ok=3    changed=2    unreachable=0    failed=0
localhost  : ok=2    changed=0    unreachable=0    failed=0
I can iterate the group, but variables are undefined. And I don't know how 
to get "response2" even if it was defined. 

2. 
- hosts: test
  serial: 1
  user: dmitresso

  tasks:
  - shell: who
    register: response1

  - shell: ls /tmp/someprogram.pid
    register: response2


- hosts: local

  tasks:

  - debug:
      var: hostvars[groups['test'][0]]['response1']['stdout_lines']

  - debug:
      var: hostvars[groups['test'][0]]['response2']['stdout_lines']

  - debug:
      var: hostvars[groups['test'][1]]['response1']['stdout_lines']

  - debug:
      var: hostvars[groups['test'][1]]['response2']['stdout_lines']

Response:
dmitresso@host ~/ansible $ ansible-playbook pb.yml

PLAY [test] 
********************************************************************

TASK [setup] 
*******************************************************************
ok: [host1]

TASK [command] 
*****************************************************************
changed: [host1]

TASK [command] 
*****************************************************************
changed: [host1]

PLAY [test] 
********************************************************************

TASK [setup] 
*******************************************************************
ok: [host2]

TASK [command] 
*****************************************************************
changed: [host2]

TASK [command] 
*****************************************************************
changed: [host2]

PLAY [local] 
*******************************************************************

TASK [setup] 
*******************************************************************
ok: [localhost]

TASK [debug] 
*******************************************************************
ok: [localhost] => {
    "hostvars[groups['test'][0]]['response1']['stdout_lines']": [
        "dmitresso pts/0        May 25 09:00 (host)"
    ]
}

TASK [debug] 
*******************************************************************
ok: [localhost] => {
    "hostvars[groups['test'][0]]['response2']['stdout_lines']": [
        "/tmp/someprogram.pid"
    ]
}

TASK [debug] 
*******************************************************************
ok: [localhost] => {
    "hostvars[groups['test'][1]]['response1']['stdout_lines']": [
        "dmitresso pts/0        May 25 09:00 (host)"
    ]
}

TASK [debug] 
*******************************************************************
ok: [localhost] => {
    "hostvars[groups['test'][1]]['response2']['stdout_lines']": [
        "/tmp/someprogram.pid"
    ]
}

PLAY RECAP 
*********************************************************************
host1  : ok=3    changed=2    unreachable=0    failed=0
host2  : ok=3    changed=2    unreachable=0    failed=0
localhost                  : ok=5    changed=0    unreachable=0    failed=0

There's nice result. But how can I make a request to multiple hosts? 
There's bad idea to write single line for every host. I've tried

   - hostvars[groups['test']]['response1']['stdout_lines']
   - hostvars[groups['test'][*]]['response1']['stdout_lines']
   - hostvars[groups['test'][1-2]]['response1']['stdout_lines']
   - hostvars[groups['test'][1:2]]['response1']['stdout_lines']

They all can't do what I want.
Second question - is there a bad idea to write this information to file and 
then parse it? Maybe start script on localhost and pass all this info to it?

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/428aeffd-7fcc-4710-8de4-1e2d12609f8e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to