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

33 views
Skip to first unread message

Justin Seiser

unread,
Dec 13, 2017, 10:39:30 AM12/13/17
to Ansible Project
I see there was a feature 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


 - 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.

Toshio Kuratomi

unread,
Dec 13, 2017, 8:27:16 PM12/13/17
to ansible...@googlegroups.com
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

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/155465dd-7461-486c-9351-5a386c6c5f15%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin Seiser

unread,
Dec 14, 2017, 8:30:07 AM12/14/17
to Ansible Project
I would prefer to not use the shell module, thats why Im asking if anyone has any other way around it.

Thanks,


On Wednesday, 13 December 2017 20:27:16 UTC-5, Toshio Kuratomi wrote:
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
On Dec 13, 2017 7:39 AM, "Justin Seiser" <justin...@gmail.com> wrote:
I see there was a feature 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


 - 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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

Kai Stian Olstad

unread,
Dec 14, 2017, 9:52:45 AM12/14/17
to ansible...@googlegroups.com
On Wednesday, 13 December 2017 16.39.30 CET Justin Seiser wrote:
> 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
>
>
> - 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.

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$'
register: r

- set_fact:
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


--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages