# Seeking advice on converting bash oneliners into ansible

**URL:** <https://forum.ansible.com/t/seeking-advice-on-converting-bash-oneliners-into-ansible/8600>\
**Category:** Get Help\
**Created:** [September 18, 2024, 2:47pm UTC](https://forum.ansible.com/t/seeking-advice-on-converting-bash-oneliners-into-ansible/8600 "2024-09-18T14:47:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jringoot](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jringoot/32/4791_2.png) [@jringoot](https://forum.ansible.com/u/jringoot)\
**Post date:** [September 18, 2024, 2:47pm UTC](https://forum.ansible.com/t/seeking-advice-on-converting-bash-oneliners-into-ansible/8600/1 "2024-09-18T14:47:46Z")

</div>

**Seeking advice on converting bash oneliners into ansible**  
Trying to install the latest glpi-agent on workstations

This the code in bash that works, when run as root

```auto
#!/bin/bash
# ensure that perl en curl are installed
apt install perl curl -y
#there is no "latest" script and no repository, this method is approved in the git notes request by the developers to retrieve the current version
export TAG=`curl -s https://api.github.com/repos/glpi-project/glpi-agent/releases/latest | grep tag_name | cut -d \" -f 4`
#downloading the current script based on the $TAG version retrieved from the webpage
wget https://github.com/glpi-project/glpi-agent/releases/download/$TAG/glpi-agent-$TAG-linux-installer.pl
#install the agent
perl ./glpi-agent-$TAG-linux-installer.pl -s https://inventory.domain.com/
#run the agent
glpi-agent

```

or as oneliner:

```auto
apt install perl curl -y && export TAG=`curl -s https://api.github.com/repos/glpi-project/glpi-agent/releases/latest | grep tag_name | cut -d \" -f 4` && wget https://github.com/glpi-project/glpi-agent/releases/download/$TAG/glpi-agent-$TAG-linux-installer.pl && sudo perl ./glpi-agent-$TAG-linux-installer.pl -s https://inventory.domain.com/ && glpi-agent

```

Question: how to let this run on a range of systems like defined in /etc/ansible/hosts: blunt or best practice, are all fine,

Maybe someone made already an ansible “glpi-agent\_install” with “ensure latest”?

ansible cli can retrieve the $TAG, if I escape the ""  
first one doesnt work

```auto

ansible testhost -m shell -a "curl -s https://api.github.com/repos/glpi-project/glpi-agent/releases/latest | grep tag_name | c
ut -d \" -f 4"
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. Current version: 3.6.8 (default, Nov 16 2020, 
16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]. This feature will be removed from ansible-core in version 2.12. Deprecation warnings can be disabled by 
setting deprecation_warnings=False in ansible.cfg.
ERROR! failed at splitting arguments, either an unbalanced jinja2 block or quotes: curl -s https://api.github.com/repos/glpi-project/glpi-agent/releases/late
st | grep tag_name | cut -d " -f 4

```

but second is fine

```auto
ansible testhost -m shell -a "curl -s https://api.github.com/repos/glpi-project/glpi-agent/releases/latest | grep tag_name | c
ut -d \\\" -f 4"
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. Current version: 3.6.8 (default, Nov 16 2020, 
16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]. This feature will be removed from ansible-core in version 2.12. Deprecation warnings can be disabled by 
setting deprecation_warnings=False in ansible.cfg.
testhost | CHANGED | rc=0 >>
1.10

```

Reusing that value in a variable appears problematic:

```auto
[ansible-user@ansible-server ansible]$ ansible opti-014 -m shell -a "TAG=`curl -s https://api.github.com/repos/glpi-project/glpi-agent/releases/latest | grep tag_name | cut -d \\\" -f 4`"                                                                                                                                       
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. Current version: 3.6.8 (default, Nov 16 2020, 
16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]. This feature will be removed from ansible-core in version 2.12. Deprecation warnings can be disabled by 
setting deprecation_warnings=False in ansible.cfg.
opti-014 | CHANGED | rc=0 >>

[ansibleuser@ansible-server ansible]$ ansible opti-014 -m shell -a "echo $TAG"                          
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. Current version: 3.6.8 (default, Nov 16 2020, 
16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]. This feature will be removed from ansible-core in version 2.12. Deprecation warnings can be disabled by 
setting deprecation_warnings=False in ansible.cfg.
opti-014 | CHANGED | rc=0 >>

[ansible-user@ansible-server ansible]$ ansible opti-014 -m shell -a "TAG=`curl -s https://api.github.com/repos/glpi-project/glpi-agent/releases/latest | grep tag_nam
e | cut -d \\\" -f 4` && echo $TAG"
[DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. Current version: 3.6.8 (default, Nov 16 2020, 
16:55:22) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]. This feature will be removed from ansible-core in version 2.12. Deprecation warnings can be disabled by 
setting deprecation_warnings=False in ansible.cfg.
opti-014 | CHANGED | rc=0 >>

[ansible-user@ansible-server ansible]$ 

```

BTW: I used this ansible a lot in CLI mode for ansible one-liners, very usefull.  
Even though it is old, I think that the old version of ansible is not the issue.  
Anyway I will shedule a new install of ansible on a current debian 12, to see if that works better  
BBTW: I was reading the “facts” section of playbooks, but I didn’t get to a solution yet, not sure if it will help.

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [September 18, 2024, 5:57pm UTC](https://forum.ansible.com/t/seeking-advice-on-converting-bash-oneliners-into-ansible/8600/2 "2024-09-18T17:57:04Z")

</div>

If you’re willing to take a step back from the one-liner, save the script as, say, `glpi-agent_install.sh`, then run (something like this untested bit):

```sh
ansible testhost -m ansible.builtin.script -bK -a 'cmd=/path/to/glpi-agent_install.sh'

```

There are other parameters to [ansible.builtin.script](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/script_module.html#ansible-collections-ansible-builtin-script-module) you may want or need to take advantage of. That’s a quick-n-dirty way to get from where you are to having it installed on a bunch of hosts.

Alternatively, Ansible has modules for pulling down resources from the web, running commands, and managing new (or old) services. But then you’re talking about making a proper playbook.

---

<div class="post-metadata">

**Author:** ![jringoot](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jringoot/32/4791_2.png) [@jringoot](https://forum.ansible.com/u/jringoot)\
**Post date:** [September 18, 2024, 10:47pm UTC](https://forum.ansible.com/t/seeking-advice-on-converting-bash-oneliners-into-ansible/8600/3 "2024-09-18T22:47:30Z")

</div>

Thanks Todd,  
This works as a jiffy

```auto
ansible testhost -m ansible.builtin.script -a '/etc/ansible/scripts/glpi-agent_install.bash

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 18, 2024, 10:48pm UTC](https://forum.ansible.com/t/seeking-advice-on-converting-bash-oneliners-into-ansible/8600/4 "2024-10-18T22:48:21Z")

</div>

Closed on October 18, 2024, 10:48pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
