is_file produces a syntax error in Ansible - ansible

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"

Related

How to get the symlink target path in ansible

Want to check if the symlink destination is having the folder name which is passed as extra vars. The below is not working:
- name: Print a debug message
debug:
msg: {{package_version}} contains ab_slink.stat.lnk_target
register: ab_slink_exist
when: "ab_slink.stat.lnk_target is search '{{package_version}}'"
getting this error:
\[WARNING\]: conditional statements should not include jinja2 templating
delimiters such as {{ }} or {% %}.
The contents of a when directive are evaluated in an implicit Jinja context -- that is, you can pretend they are already surrounded by {{...}} markers. You never nest {{...}} markers; if you want to reference a variable inside a Jinja expression, you just use the variable name. Your task should look something like this:
- name: Print a debug message
debug:
msg: "{{package_version}} contains ab_slink.stat.lnk_target"
register: ab_slink_exist
when: "ab_slink.stat.lnk_target is search(package_version)"
Except this is still problematic, because you don't generally use debug tasks to set values. I would write something like this:
- name: set ab_slink_exist
set_fact:
ab_slink_exist: "{{ ab_slink.stat.lnk_target is search(package_version) }}"

How to access an ansible block variable in a file from the same block

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"

Concat multiple variables and strings in ansible playbook

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

How to pass variable in command module in ansible

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.

Splitting variable not working in Ansible

I am trying to split the variable based on delimiter. How can I achieve it?
some_module: {{item}}.split('#')[1]
with_items:
- git#someversionxxx
- gradle#someversionxxx
I get following error:
list object' has no attribute 'split ansible
I want to consider only first part of variable i.e. before '#'
some_module: "{{ item.split('#')[0] }}"
{{ ... }} is used to indicate Jinja2 expressions and everything you have is a Jinja2 expression
with YAML syntax in Ansible you must quote a string if it starts with { (unless it was a JSON object, here it's not)
the first element of the split result will have an index of 0

Resources