Ansible variable length command line options - ansible

I'm trying to create a directive using shell or command to execute something that could have any number of command line arguments.
So something like
- Name: Runs MediaWiki command line setup.
command: "php /opt/wiki/maintanance/install.php {{ arguments }}"
I'm looking for something like a [item for "%s=%s % (key, value) in arguments"]
Looking at ansible variables, everything I see there wants to loop each command on any of the datastruture.
Does anyone know what is the best way to join an arbitrary list of arguments for a command and the best way to structure that in a variables file?

If arguments is a list you can use the join filter.
- Name: Runs MediaWiki command line setup.
command: "php /opt/wiki/maintanance/install.php {{ arguments | join }}"
If arguments is a dict you might be able to do something like this:
- Name: Runs MediaWiki command line setup.
command: "php /opt/wiki/maintanance/install.php {{ arguments | urlencode | replace('&', ' ') }}"

Related

Expand complex ansible variable

I have distinct ansible variables which are combined in a single command. More precisely, the system defines a variable with default value:
# This exists in an ansible variable definition file
instrumentor = ''
At some the python script that "creates" the ansible commands specifies the extra arguments to override the default values. I'm jumping through some hoops (using \" and \') because the desired value contain spaces:
# This is a line from python script
instrumentation_specs =
'-e \'instrumentor=\"/software/cuda-memcheck --tool racecheck --log-file /tmp/memlog\"\''
On the ansible side then the command is structured as
;; This exists in a jinja template (.j2 extension)
;; command is what the start.yml script will be running
command="{{ instrumentor }}" my_program
Unfortunately I cannot get this to work. The command is reported to expand to something that contains extra ' and ":
start.yml (...) -e 'instrumentor="/software/cuda-memcheck --tool racecheck --log-file /tmp/memlog"' (...)
The weird thing is that
if I create another variable before command that only contains instrumentor, it is reported to have the exact value that I need
instr_tmp = "{{ instrumentor }}"
command = "{{ instr_tmp }}" my_program
i.e. the extra ' and " don't appear in instr_tmp but do appear in command
If I ommit the \" and \' on the python side (when creating the string that is used to provide the extra arguments) everything that's after a space gets omitted.
Anyone knows how to specify such a variable override, i.e. how the string should look like when I specify a variable that contains multiple space separated strings (instrumentor), but that variable is supposed to be plugged into another ansible variable (command).

Pass map to ansible via Jenkins ansible playbook

How can I get jenkin ansible-playbook plugin to pass a list of strings in the same way I would on the command line?
ansible-playbook ... \
-e '{"package_urls": ["http...windows.exe", "http...linux.rpm", "http...babbage.steam"]}'
In jenkins the playbook seems to take a map for extraVars and my escaping attempts haven't yet worked
ansiblePlaybook (..., extraVars: [
package_urls: """["http...windows.exe", "http...linux.rpm", "http...babbage.steam"]""" )
Results in the following which lacks ' and ' and is not recognized as a list
... -e package_urls=["http...windows.exe", "http...linux.rpm", "http...babbage.steam"]
This works in that it produces the correct command line. The playbook plugin has an extras param which can be used to pass a variety of text down the the command line. I usually use this for the '-vvv' verbosity modification.
ansiblePlaybook ( ...,
extras: """-e '{"package_urls": ["http...windows.exe", "http...linux.rpm", "http...babbage.steam"]}'""")
The desired text is passed down to the command line with no modifications and the job pulls in its list.
This is probably not the optimal approach and either populating a var file or a json file which is read by the playbook feels like a cleaner approach

How to use wildcard with variable [duplicate]

This question already has answers here:
Ansible Command module says that '|' is illegal character
(2 answers)
Closed 5 years ago.
I want to list the files as per the host name.But problem is i not able to use the wildcard with variable properly.Can someone suggest me on this.
---
- hosts: local
become_user: yes
vars:
filename: /root/stuff
tasks:
- name: list files
action: command ls -lrt {{ filename }}/'*{{ansible_hostname}}'
register: listfiles
- debug: var=listfiles
If your question is why * doesn't expand?, then:
command module:
The command module takes the command name followed by a list of space-delimited arguments. The given command will be executed on all selected nodes. It will not be processed through the shell, so variables like $HOME and operations like "<", ">", "|", and "&" will not work
shell module:
The shell module takes the command name followed by a list of space-delimited arguments. It is almost exactly like the command module but runs the command through a shell (/bin/sh) on the remote node.
So if you need any shell tricks, like wildcard expansion or access to environment variables, use shell module.

Running script with arguments through ansible

[Ansible version == 2.1.0]
In order to run a script which is present locally on the target server, we can use Ansible's "command" module. Following can be done easily:
- name: Executing getpkgs.sh to download packages.
command: sh "/path/to /dir/scriptName.sh" arg1 arg2 arg3 arg4
I have my script names and the arguments stored in ansible variables. For example, the following variable contains all the script names and the arguments to be passed to those scripts:
scripts_to_execute:
- { filename: "/path/to/file/file1.sh", args: "arg11 arg12 arg13"}
- { filename: "/path/to/file/file2.sh", args: "arg21 arg22"}
- { filename: "/path/to/file/file3.sh", args: "arg31 arg32 arg33 arg34"}
And i want all these files which are already present on the target server, to be executed using with_items. Trying to achieve something like the following:
- name: Executing all files.
command: sh "{{item.filename}}" "{{item.args}}"
with_items: scripts_to_execute
I am trying to pass the script name followed by the string containing all arguments that are to be passed into the script. But it is considering that string of arguments as a single argument.
But it is considering that string of arguments as a single argument.
I think that makes sense since you pass the args in quotes. Did you try without quotes?
- name: Executing all files.
command: sh "{{item.filename}}" {{item.args}}
with_items: scripts_to_execute

How to make Ansible YAML parsing accept this command?

Question:
How to make Ansible YAML parsing accept this command ?
Details below:
This YAML:
-shell: "/home/developer/eclipse/eclipse -application org.eclipse.equinox.p2.director -noSplash -repository
'http://moreunit.sourceforge.net/update-site' -installIUs
org.moreunit.feature.group"
is validated by:
http://yaml-online-parser.appspot.com/
http://www.yamllint.com/
http://codebeautify.org/yaml-validator
but ansible says:
This one looks easy to fix. It seems that there is a value started
with a quote, and the YAML parser is expecting to see the line ended
with the same kind of quote. For instance:
when: "ok" in result.stdout
Could be written as:
when: '"ok" in result.stdout'
or equivalently:
when: "'ok' in result.stdout"
As a reference this YAML works perfectly:
-shell: "wget 'http://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gzip'"
Try
- shell: >
/home/developer/eclipse/eclipse
-application org.eclipse.equinox.p2.director
-noSplash
-repository 'http://moreunit.sourceforge.net/update-site'
-installIUs org.moreunit.feature.group
Do not trust the editor or syntax highlighter. Let ansible tell you if there is a problem (Run it with -C flag to simulate a dry-run if u want). Also try replacing single with double quotes around the repo URL.

Resources