Seeking advice on converting bash oneliners into ansible

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

#!/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:

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


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

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:

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

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

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

There are other parameters to ansible.builtin.script 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.

2 Likes

Thanks Todd,
This works as a jiffy

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

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