JSON formatting on register value.

705 views
Skip to first unread message

Anthony Cheng

unread,
Sep 22, 2016, 2:26:10 PM9/22/16
to Ansible Project
I have a playbook that use the S3 module to list items:

- name: List s3 bucket
  local_action:
   module: s3
   bucket: "{{ S3_BUCKET_NAME }}"
   mode: list
 register: S3_LIST_ITEM


The result I output to a file using lineinfile:

 name: Output to folder
  local_action:
   module: lineinfile
   create: yes
    line: "{{ S3_LIST_ITEM.s3_keys }}"
   dest: "{{ S3_BUCKET_FILE }}"
   state: present

However this results in a JSON format:

['HelloWorld.sh', 'bucket1/', 'test1']

I tried using jinja2 filter (from_json), 

e.g. line: "{{ S3_LIST_ITEM.s3_keys }}"| from_json

But that didn't work as it gave me:

fatal: [localhost]: FAILED! => {"failed": true, "msg": "Unexpected templating type error occurred on ({{  S3_LIST_ITEM.s3_keys|from_json }}): expected string or buffer"}

There is also no stdout_lines equivalent so how can I put this in a text format that separate each value in a new line?

Anthony Cheng

unread,
Sep 23, 2016, 11:32:27 AM9/23/16
to Ansible Project
FYI I figured it out that I needed to use with_item to parse the JSON.
Reply all
Reply to author
Forward
0 new messages