i am using folloing task in YAMl file.
- name: Run deployment of RWI Artifact
command: "{{ deploy_script }} /home/scripts/lite /application/ a-CL '--Home=/opt/AppServer --appClassLoaderMode=abc'"
but typically , i am getting following error
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
i tried all combination but dont know how to put those quotes correctly.
please suggest
In YAML if you start string with quote, it is considered as quoted string, so you must end the string with the same quote.
Try:
- name: Run deployment of RWI Artifact
command: "{{ deploy_script }} /home/scripts/lite /application/ a-CL '--Home=/opt/AppServer --appClassLoaderMode=abc'"
I assume that deploy_script has no spaces.
Related
I define a yml variable files for ansible with the following structure:
appserver:
root_directory: C:\app
config_directory: '{{ root_directory }}\config'
it seems the second variable config_directory cannot be interpreted correctly, I get a VARIABLE NOT FOUND ERROR.
I tried with:
appserver:
root_directory: C:\app
config_directory: '{{ appserver.root_directory }}\config'
It does not work either, I have a very long trace of error, the most interesting part is :
recursive loop detected in template string:{{ appserver.root_directory }}\config
When I use double quotes instead of simple quotes,
appserver:
root_directory: C:\app
config_directory: "{{ appserver.root_directory }}\config"
I get the following error:
The offending line appears to be:
app_root: D:\WynsureEnvironments\Application
wynsure_root: "{{ appserver.root_directory }}\config"
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
When using variable blocks, how can I reuse variables to assign new variables?
Thanks!
You cannot use such a recursive jinja2 variable declaration in ansible.
Here are 2 (non exhaustive list) alternative solutions:
Don't use a hash. Prepend your vars names. You will typically find this type of naming conventions in e.g. reusable roles on ansible galaxy
appserver_root_directory: C:\app
appserver_config_directory: '{{ appserver_root_directory }}\config'
If you really need a hash of this kind, declare a "private" variable outside of your hash and reuse it inside.
_appserver_root: C:\app
appserver:
root_directory: "{{ _appserver_root }}"
config_directory: "{{ _appserver_root }}\config"
I use Ansible 2.3.2.0 for my project. In this project, I have a task using is_file test:
- name: Create repository.ini file
template: src="{{ playbook_dir }}/../../../repository.ini" dest="/{{ repository_dir }}/repository.ini"
when: "{{ playbook_dir }}/../../..//repository.ini"|is_file
This task produces the following error:
The offending line appears to be:
template: src="{{ playbook_dir }}/../../../repository.ini" dest="/{{ jse_repository_dir }}/repository.ini"
when: "{{ playbook_dir }}/../../../repository.ini"|is_file
^ here
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
. . . .
I tried everything I could with no avail.
When you start a YAML key value with a quote, you must quote the whole value, so if there are any other strings inside, you should use a different quotation mark (e.g., single quote + double quote).
Ansible expects the value of when to be a Jinja2 statement, so there is no need to use {{ inside.
Because 2. that you need to use Jinja2 syntax for string concatenation variable + 'string'.
Combined, your conditional should be:
when: "(playbook_dir+'/../../../repository.ini')|is_file"
I feel like this should be really straight-forward, but I've been pouring over the ansible docs and other answers here, and I'm stuck.
What I want to do is to set up an array or list of composer packages to be used in a composer require statement using the ansible composer object.
So, I've got a set fact like so to define the modules:
- name: Define modules to be required
set_fact:
modules:
- vendor1/package1
- vendor2/package2
- vendorN/packageN
I've put this in the top of my ansible project so it's easily accessible and editable (It'd be sweet to figure out how to simply define the packages in a yaml file as vars, and then use set_fact in the playbook, but I'll stick to scope here)
With this in mind, I then have a role in the project with a composer task where I want to require each of these:
- name: "Require packages"
composer:
command: require
arguments: {{ modules_to_be_required }}
working_dir: "{{ app['directory'] }}"
Originally, I did this using with_items like so:
- name: "Require packages"
composer:
command: require
arguments: "{{ item }}"
working_dir: "{{ app['directory'] }}"
with_items
- vendor1/package1
- vendor2/package2
- vendorN/packageN
This does work; however, it causes a composer update to run with each iteration of with_items which takes far too long. To me, it makes more sense to take the list of packages to be required, convert them to a string separated by spaces, and then pass that as an argument.
In my research I found that I could convert a list to a string with the join filter, so I tried this:
- name: "Require packages"
composer:
command: require
arguments: {{ modules | join(" " ) }}
working_dir: "{{ app['directory'] }}"
But, this gives me a YAML syntax error:
The offending line appears to be:
command: require
arguments: {{ modules | join(" ") }}
^ here
I've tried wrapping "{{ modules | join(" ") }}" but I get yelled at for the quotes in the parenthesis.
Am I approaching this idea in the right way, or what questions do I need to ask to get to the answer? Any guidance is appreciated.
Your join filter can use single quotes
arguments: "{{ modules | join(' ') }}"
or you can escape the double quotes
arguments: "{{ modules | join(\" \") }}"
or you can use single quotes for the variable and double for the join
arguments: '{{ modules | join(" ") }}'
I'm trying multiple concatenation when preforming with_items for the destination section.
Right now it looks like this:
- name: create app except+lookup
copy: content="" dest="{{ dir.comp ~ '/config/con2dd/' ~ item.name ~ 'File.txt' }}" force=no group=devops owner=devops mode: 0755
with_items:
...
I get:
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
with_items:
- {{ foo }}
Should be written as:
with_items:
- "{{ foo }}"
Tried couple of approaches but none resulted something that's working.
Is it possible to concat the variables with the strings?
Don't mix pure YAML and key=value syntaxes for parameters. And always use YAML syntax for complex arguments:
- name: create app except+lookup
copy:
content: ""
dest: "{{ dir.comp }}/config/con2dd/{{ item.name }}File.txt"
force: no
group: devops
owner: devops
mode: 0755
with_items:
...
You are not quoting the value associated with the key copy. For that to happen the first character has to be double (or single) quote. The example, given in the feedback, does this correctly, but is not explicit about it. Once a scalar start with a non-quote (yours start with the c from content quotes occurring in the scalar will no longer have a special meaning.
Because of a bug in the parser that Ansible uses, the : (colon space) in that scalar (mode: 0755) causes trouble, you should double quote the whole scalar and escape the double quotes that occur within it:
copy: "content=\"\" dest=\"{{ dir.comp ~ '/config/con2dd/' ~ item.name ~ 'File.txt' }}\" force=no group=devops owner=devops mode: 0755"
or alternatively use single quotes (which have different escape rules:
copy: 'content="" dest="{{ dir.comp ~ ''/config/con2dd/'' ~ item.name ~ ''File.txt'' }}" force=no group=devops owner=devops mode: 0755'
You can test scalars yourself on this online YAML parser, it has the same bug as what causes Ansible not to parse your YAML correctly.
This parser, handles the in scalar : correctly and will not produce an error with your input (but it has other problems).
I have an ansible playbook in which one variable I am passing from the command. I am trying to append a windows folder path to it. One way I am able to find out is to add the path to another variable and then join the two variable. I would like to know if it is possible to avoid the variable and put the path like this:
"{{ variable2 }} \build\dist\package\ui.msi"
variable1 has value "d:\install"
var_build_file_name is entered by the user
variable2 is formed by combining variable1 and var_build_number.
This is the actual content in the playbook which works:
vars:
installerFolder: "{{ UploadFolder }}{{ var_build_file_name | regex_replace('(\\.zip)','\') }}"
packagePath: '\build\dist\package\UI.msi'
- name: Install new version.
debug:
msg: "{{ installerFolder }}{{ packagePath }}"
And this is the playbook command:
ansible-playbook Install.yml -i ../inventory/hosts.ini --extra-vars "target=servername var_build_file_name=16.3.0.zip UploadFolder=D:\Install\\"
The output I am getting is:
"msg": "D:\\Install\\16.3.0\\build\\dist\\package\\UI.msi"
In the above output, how come two backslashes are showing instead of one?
Is it possible to do
msg: "{{ installerFolder }} '\build\dist\package\UI.msi' "
I have tried many combinations but the backslashes are not getting properly escaped for above. If it is not possible then can someone enlighten on the reason.
Thanks.
"msg": "D:\\Install\\16.3.0\\build\\dist\\package\\UI.msi"
It is the output of debug module, which is a JSON string, so every \ is escaped.
The actual value of the msg here is D:\Install\16.3.0\build\dist\package\UI.msi, as you expect it.
And you can definitely use this syntax: msg: '{{ installerFolder }}\build\dist\package\UI.msi'