0

I am learning ansible. i have written a jinja2 template for looping over a multiple values. However, i get those values from a linux command.

- name: fetching port ranges 
  command: ls /apache |grep redis|grep ".pid"  | awk -F"_" '{ print $2}'| awk -F"." '{print $1}'

Output of the above command will be :

10001
10002
10003
10004
10005

Below is the jinja2 template

 [{% for range in port  %}
    "127.0.0.1:{{ range }}",
  {% endfor %}]

With "for loop" in the jinja2 template, i am trying iterate over the values generated by linux command but i am not sure how to save those values and use it in jinja2 template. Any help would be appreciated.

learning fun
  • 519
  • 1
  • 5
  • 12

1 Answers1

2

You need to register the result and then use the output from stdout_lines.

Try something like this:

- name: fetching port ranges 
  command: ls /apache |grep redis|grep ".pid"  | awk -F"_" '{ print $2}'| awk -F"." '{print $1}'
  register: result

- name: my template task
  template:
    src: <src>
    dest: <dest>
  vars:
    port: "{{ result.stdout_lines }}"
Matt P
  • 2,452
  • 1
  • 12
  • 15