saving command output from a loop in different variables

22 views
Skip to first unread message

Kevin Hoyt

unread,
Sep 2, 2020, 4:43:13 PM9/2/20
to Ansible Development
Hi,

I'm trying to run a command on a remote server, multiple times and need to set a fact or register different names for each.  Here's the code I have for running the command:

    - name: 'run opctest status on PG'
      win_command: "opctest /cust {{ tenant_code }} /node {{ item }}"
      args:
        stdin: status
      register: opctest_out
      ignore_errors: yes
      with_items:
        - "{{ pg_list }}"
      

pg_list is a listing of 3 strings ('PG1A', 'PG100A','PG101A').  I'd like to name them after each of these, for example, something along the lines of "opctest_out_pg101a"

I can't find a way to do that.  Does anyone have any suggestions?

Thanks,
Kevin


Mike Sawyers

unread,
Sep 2, 2020, 5:45:51 PM9/2/20
to Kevin Hoyt, Ansible Development
I haven't tried it myself, but maybe you can make a dict object using your item as the key?


--
You received this message because you are subscribed to the Google Groups "Ansible Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-deve...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-devel/fe7fb6c2-0c54-4ca5-8ea6-16144c1e5706n%40googlegroups.com.

Kevin Hoyt

unread,
Sep 3, 2020, 9:22:10 AM9/3/20
to Mike Sawyers, Ansible Development
Hi Mike,

Thanks, I'll see if I can make that work.

Kevin
--
CATAPULTAM HABEO. NISI PECUNIAM OMNEM MIHI DABIS, AD CAPUT TUUM SAXUM IMMANE MITTAM

Kevin Hoyt

unread,
Sep 4, 2020, 12:12:54 PM9/4/20
to Mike Sawyers, Ansible Development
Hi Mike,

I've been staring at this and maybe I'm missing it.  I'm not sure how to get from the command output to the dict.  Setting a fact makes sense, but how do I do it as part of the data collection task?

Thanks,
Kevin


Kevin Hoyt

unread,
Sep 4, 2020, 2:28:52 PM9/4/20
to Mike Sawyers, Ansible Development
I was able to get a fact set with my data. Here is the task and the fact.  My last problem is how to make it cacheable.

    - name: 'run opctest status on PG'
      win_command: "opctest /cust {{ tenant_code }} /node {{ item }}"
      args:
        stdin: status
      register: opctest_out
      ignore_errors: yes
      with_items:
        - "{{ pg_list }}"
     
    - name: my_test
      set_fact: {"healthcheck_pg_{{ item.item }}": "{{ item.stdout }}"}
      loop: "{{ opctest_out | json_query('results[*]') }}"


that creates the following:
 "ansible_facts": {
        "healthcheck_pg_PG100B": "12:13:37 Trace: EMT Creating Mutex Glo..."
}

but since it's not cacheable it's not in the mongodb at the backend.

Thanks,
Kevin


Reply all
Reply to author
Forward
0 new messages