Skip to content
Feb 24 / Greg

Ansible Tower Survey Textarea To List

I was recently leading an Ansible workshop when someone asked me if you could accept input from a survey in an ansible and use that as a list in a playbook. I worked out a quick solution that I figure may be useful for others too.

I first created a quick survey that would pass a text area to a playbook.

As you can see it passes the survey info over to the playbook as “test_data”. It’s important to remember that the list entries should be one per line.

Next I threw together a quick playbook that will accept the collected info(survey-list.yml):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
---
- name: test message area
  hosts: localhost
  gather_facts: false
  vars:
  tasks:
  - name: debug stuff
    debug:
      var: test_data
 
  - name: run it as a list
    debug:
      msg: "{{ item }}"
    loop: "{{ test_data.split('\n') }}"

Taking a look at the playbook tasks I’m doing two things.
In the first task I’m using the debug module to just dump the contents of the test_data variable.
In the second task I’m looping through the data and spitting out the results one at a time. Notice that I’m taking the same variable and using the split option to break them up into separate entries based on the new line character ‘\n’.

Here’s the output of the run:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PLAY [test message area] *******************************************************
TASK [debug stuff] *************************************************************
ok: [localhost] => {
    "test_data": "one\ntwo\nthree\nfour"
}
TASK [run it as a list] ********************************************************
ok: [localhost] => (item=one) => {
    "msg": "one"
}
ok: [localhost] => (item=two) => {
    "msg": "two"
}
ok: [localhost] => (item=three) => {
    "msg": "three"
}
ok: [localhost] => (item=four) => {
    "msg": "four"
}
PLAY RECAP *********************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Notice in the debug stuff task it has all the entries with the “\n” new lines jammed in between them.
The second task shows the output listed in four separate entries.

So now I can successfully iterate over anything provided by the survey text area!

Thanks and happy automating.

Leave a Comment

 

*