# Trying to duplicate a Bash script into ansible -- Not sure how to best handle alternatives with Slaves

**URL:** <https://forum.ansible.com/t/trying-to-duplicate-a-bash-script-into-ansible-not-sure-how-to-best-handle-alternatives-with-slaves/26282>\
**Category:** Ansible Project\
**Created:** [December 13, 2017, 3:39pm UTC](https://forum.ansible.com/t/trying-to-duplicate-a-bash-script-into-ansible-not-sure-how-to-best-handle-alternatives-with-slaves/26282 "2017-12-13T15:39:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Justin2](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@Justin2](https://forum.ansible.com/u/Justin2)\
**Post date:** [December 13, 2017, 3:39pm UTC](https://forum.ansible.com/t/trying-to-duplicate-a-bash-script-into-ansible-not-sure-how-to-best-handle-alternatives-with-slaves/26282/1 "2017-12-13T15:39:30Z")

</div>

I see there was a [feature](https://github.com/ansible/ansible/issues/24278) request for this, but they do not note how they actually worked around it. In a nutshell, we download a groovy archive, unzip it, and create slaves for every file in the archive that is not the groovy executable, and doesnt end with .bat. We then do a update-alternatives for groovy.

`

GROOVY\_ALT\_SLAVES=“”  
for f in $(ls /opt/groovy-${GROOVY\_VERSION}/bin | fgrep -v .bat | grep -v ‘^groovy$’); do  
GROOVY\_ALT\_SLAVES=" ${GROOVY\_ALT\_SLAVES} --slave /usr/bin/${f} ${f} /opt/groovy-${GROOVY\_VERSION}/bin/${f}"  
done

update-alternatives --install /usr/bin/groovy groovy /opt/groovy-${GROOVY\_VERSION}/bin/groovy 2000 ${GROOVY\_ALT\_SLAVES}  
update-alternatives --set groovy /opt/groovy-${GROOVY\_VERSION}/bin/groovy

`

I cant really wrap my head around the best way to dupe this, that isnt just running the shell module. Once I resigned myself to using the shell module, I then read that doing something like

```auto
 - shell: GROOVY_ALT_SLAVES=" ${GROOVY_ALT_SLAVES} --slave /usr/bin/{{ item }} {{ item }} /opt/groovy-{{ groovy_version }}/bin/{{ item }}"
    with_items: ['file1', 'file2']

```

Wouldn’t work, since each shell run is a separate connection, so Im not really building up a long string to then append to the update-alternatives command. There is also the fact I have to manually specify a list of items, since I cant use glob because i need to match files that do not match a certain set of expressions.

Im sure Im missing something obvious, but I haven’t made any progress on this one and any help would be great.

---

<div class="post-metadata">

**Author:** ![tkuratom](https://avatars.discourse-cdn.com/v4/letter/t/b19c9b/32.png) [@tkuratom](https://forum.ansible.com/u/tkuratom)\
**Post date:** [December 14, 2017, 1:26am UTC](https://forum.ansible.com/t/trying-to-duplicate-a-bash-script-into-ansible-not-sure-how-to-best-handle-alternatives-with-slaves/26282/2 "2017-12-14T01:26:52Z")

</div>

If you don’t need idempotence (I assume you don’t since the shell module doesn’t have idempotence either) I’d use the script module with your existing bash script.

-toshio

---

<div class="post-metadata">

**Author:** ![Justin2](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@Justin2](https://forum.ansible.com/u/Justin2)\
**Post date:** [December 14, 2017, 1:30pm UTC](https://forum.ansible.com/t/trying-to-duplicate-a-bash-script-into-ansible-not-sure-how-to-best-handle-alternatives-with-slaves/26282/3 "2017-12-14T13:30:07Z")

</div>

I would prefer to not use the shell module, thats why Im asking if anyone has any other way around it.

Thanks,

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [December 14, 2017, 2:52pm UTC](https://forum.ansible.com/t/trying-to-duplicate-a-bash-script-into-ansible-not-sure-how-to-best-handle-alternatives-with-slaves/26282/4 "2017-12-14T14:52:13Z")

</div>

You could do something like this, not tested so it might have some syntax and logical errors.

- shell: ls /opt/groovy-${GROOVY\_VERSION}/bin | fgrep -v .bat | grep -v '^groovy$'  
&nbsp;&nbsp;register: r

- set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;GROOVY\_ALT\_SLAVES: '{% for item in r.stdout\_lines %} --slave /usr/bin/{{ item }} {{ item }} /opt/groovy-{{ groovy\_version }}/bin/{{ item }} {%- endfor %}'

- command: update-alternatives --install /usr/bin/groovy groovy /opt/groovy-{{ groovy\_version }}/bin/groovy 2000 {{ GROOVY\_ALT\_SLAVES }}

- command: update-alternatives --set groovy /opt/groovy-{{ groovy\_version }/bin/groovy
