-3

I tried this one

---
- name: py
  hosts: master
  tasks:
  - name:
    command: /home/vagrant/test.py
    register: csvfile
    changed_when: false
  - debug:
      var: csvfile
  - name: Create csvfile directories
    file:
      path: "/tmp/{{ item.host }}_{{ item.app }}"
      state: directory
    with_dict: "{{ csvfile }}"

Test.py results are:

{'key': 'stdout_lines', 'value': ["{'host': '123', 'app': 'abc'}", "{'host': '2345', 'app': 'def'}", "{'host': '8484', 'app': 'ghju'}", "{'host': '89393', 'app': 'yruru'}"]}

and i'm getting error at "/{{ item.host }}_{{ item.app }}"

Can someone help me?

RamPrakash
  • 1,687
  • 3
  • 20
  • 25
Sumanth
  • 1
  • 2

3 Answers3

1

The registered variable csvfile must have the attribute stdout_lines

csvfile:
  stdout_lines:
    - {'host': '123', 'app': 'abc'}
    - {'host': '2345', 'app': 'def'}
    - {'host': '8484', 'app': 'ghju'}
    - {'host': '89393', 'app': 'yruru'}

The simple loop should do the job

  - name: Create csvfile directories
    file:
      path: "/tmp/{{ item.host }}_{{ item.app }}"
      state: directory
    loop: "{{ csvfile.stdout_lines }}"

The key/value decomposition was added very probably by with_dict. Please confirm, update and fix the question. Post the output of the task

  - debug:
      var: csvfile
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63
0

There are a bunch of things wrong, so while this is an "answer" because it is too big to fit into a comment here on S.O., it is not the fix to your problem because there is so much wrong with your code.

As your debug output demonstrates, the register: captures the output of the whole task, and not just the output from your program. Thus, at the very least you would need with_dict: "{{ csvfile.stdout }}" but that, too, will not work because the output is not an interoperability format that ansible can use. Just because it is written in python and your script is written in python does not mean they can communicate

You will need to have the test.py call json.dump or json.dumps on the results, and not just print() or repr or whatever it is calling now, so that the output can be parsed by ansible back into an actual data structure in your playbook

Then, what happens next depends on whether you continue to write out each dictionary from test.py on a per-line basis, or you choose to package them all into a list of dictionaries and dump that as JSON

Start by fixing the output from test.py to be parseable by ansible, and we'll go from there

mdaniel
  • 31,240
  • 5
  • 55
  • 58
  • Thanks for your help, As i cant post everything here i posted as an answer can you please guide me on this – Sumanth Jan 07 '20 at 18:57
0

Sumanth, you can try the same approach but using with_items:

  - name: Create csvfile directories
    file:
      path: "/tmp/{{ item.host }}_{{ item.app }}"
      state: directory
    with_items: "{{ csvfile.stdout_lines }}"
  • The offending line appears to be: file: splunkapps.csv path: "/tmp/{{ item.host }}_{{ item.app }}" ^ here We could be wrong, but this one looks like it might be an issue with missing quotes. Always quote template expression brackets when they start a value. For instance: with_items: - {{ foo }} Should be written as: with_items: - "{{ foo }}" I go this error – Sumanth Jan 07 '20 at 19:21
  • If you are testing with values with special chars, you can always set {{ item.host | string}} or {{ item.app | string}} to cast it to strings. – Jonathan Tissot Jan 07 '20 at 19:31
  • I added | string as mentioned; I am with this error FAILED! => {"failed": true, "msg": "with_dict expects a dict"} – Sumanth Jan 07 '20 at 19:53
  • Have you added it to the csvfile.stdout_lines or to each item? Could you put a snippet of code of how this is actually looking right now? You can as well, create a debug with the following code [just before the Create csvfile directories]: ` - name: Debugging csvfile debug: var: "{{ item }}" with_items: "{{ csvfile.stdout_lines }}" ` This will output what is the item content, to see if we have to deep dive into this. – Jonathan Tissot Jan 07 '20 at 20:01
  • --- - name: py hosts: master tasks: - name: command: /home/vagrant/test.py register: csvfile changed_when: false - debug: var: csvfile - name: Create csvfile directories file: path: "/tmp/{{ item.AppName | string }}_{{ item.SealID | string }}" state: directory with_dict: "{{ csvfile.stdout_lines }}" – Sumanth Jan 07 '20 at 20:07
  • Please, add the output of the debug that you got. You can add the `- name` parameter before `- debug` [but you need to remove the `- ` from debug] to spot it in your run. This will actually provide some insight about what are we looping actually. – Jonathan Tissot Jan 07 '20 at 20:10
  • TASK [Debugging csvfile] ---- ok: [192.168.121.72] => (item={'host': '123', 'AppName': 'abc'}) => { "": "VARIABLE IS NOT DEFINED!", "item": "{'host': '123', 'AppName': 'abc'}" } TASK [Create csvfile directories] ---- fatal: [192.168.121.72]: FAILED! => {"failed": true, "msg": "with_dict expects a dict"} to retry, use: --limit @/home/i737119/vms/host2/splunk.retry – Sumanth Jan 07 '20 at 20:35
  • --- - name: py hosts: master tasks: - name: command: /home/vagrant/test.py register: csvfile changed_when: false - debug: var: csvfile - name: Debugging csvfile debug: var: "{{ item }}" with_items: "{{ csvfile.stdout_lines }}" - name: Create csvfile directories file: path: "/tmp/{{ item.AppName | string }}_{{ item.Host | string }}" state: directory with_dict: "{{ csvfile.stdout_lines }}" – Sumanth Jan 07 '20 at 20:37