Ansible play fails with_items FOO - ansible

- name: Debug msg for imagepull {{ ansible_operator_meta.name }}
debug:
msg: "{{ final_resource_checks_pod_status_check_filtered_image }}"
I got an error with below
- name: Debug msg for imagepull {{ ansible_operator_meta.name }}
^ 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 }}"

Related

condition in variable type list

I would like to add certain lines in a "list" type variable.
I do that
condition: 'true'
var_list:
- template: "{{ playbook_dir }}/file1.j2", dest_dir: "{{ docker_volume_basedir }}/file1.txt"
- template: "{{ playbook_dir }}/file2.j2", dest_dir: "{{ docker_volume_basedir }}/file2.txt"
{% if condition == 'true' %}
- template: "{{ playbook_dir }}/file3.j2", dest_dir: "{{ docker_volume_basedir }}/file3.txt"
{% endif %}
but I have a nice style error ...
"message": "Syntax Error while loading YAML.\n found character '%' that cannot start any token
have you an idea ?
The problem is that you are using templated syntax in a vars file and that fails.
One of the ways i can think would be to do so at the runtime, for example
- hosts: all
vars:
default_list:
- template: "foo"
- template: "bar"
baz_list:
- template: "baz"
condition: false
tasks:
- name: append baz
set_fact:
default_list: "{{ default_list + baz_list }}"
when: condition

How to use ansible when condition when string contains '#'

I am writing playbook to check user principal in kerberos servers. If principal exists it should skip task and if not it should create user principal. I am not sure how to use string with when condition I am trying below but getting errors
"ERROR! Syntax Error while loading YAML.
expected <block end>, but found '<scalar>'
The error appears to be in '/home/revyas/RHELProjects/Atlas/eda-datalake/playbooks/provision-emr.yml': line 42, column 31, but may
be elsewhere in the file depending on the exact syntax problem.
The offending line appears to be:
stat:
path: "{{ user_dir }}"/"{{ keytab_name }}"
^ 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 }}"
Playbook:
- name: Check if user principals exist
command: 'kadmin -w "{{ emr_kdc_admin_password }}" -p kadmin/admin listprincs'
register: user_princs
delegate_to : "{{ emr_kerberos_master }}"
tags: "emr_acct"
- name: Create user kerberos principal if not exist
command: 'kadmin -w {{ emr_kdc_admin_password }} -p kadmin/admin addprinc -randkey {{ kerberos_username }}#{{ emr_kerberos_realm }}'
when: "{{ kerberos_username }}#{{ emr_kerberos_realm }}" not in user_princs.stdout
delegate_to: "{{ emr_kerberos_master }}"
tags: "emr_acct"
User principal from kdc have format given below:
emr-test1-aren-reetika#abd.xyz.com
emr-test-aren#bd.xyz.com
emr-test-integration-test#bd.xyz.com
For the first cited issue, yaml doesn't behave like python or shell which automatically concatenate string literals together
You'll want:
stat:
path: "{{ user_dir }}/{{ keytab_name }}"
And the second error is because yaml believes the leading " is the start of a YAML literal, but in fact it's the start of a Jinja2 literal, thus:
when: '"{{ kerberos_username }}#{{ emr_kerberos_realm }}" not in user_princs.stdout'
Or you can use any of the scalar folding syntaxes, if you prefer that:
when: >-
"{{ kerberos_username }}#{{ emr_kerberos_realm }}"
not in user_princs.stdout
when: "{{ kerberos_username }}#{{ emr_kerberos_realm }}" not in user_princs.stdout
change to
when: "{{ kerberos_username }}\\#{{ emr_kerberos_realm }}" not in user_princs.stdout

Ansible Registers - Dynamic naming

I am trying to use a register in Ansible playbook to store my output. Below is the code which i am using.
I have tried below code
- name: Check if Service Exists
stat: path=/etc/init.d/{{ item }}
register: {{ item }}_service_status
with_items:
- XXX
- YYY
- ZZZ
I need different outputs to be stored in different register variables based on the items as mentioned in the code. It is failing and not able to proceed. Any help would be appreciated.
Updated answer
I think you need to put quotes around it:
register: "{{ item }}_service_status"
Or you can use set_fact (1, 2, 3, 4)
register all the output to a single static variable output and then use a loop to iteratively build a new variable service_status (a list) by looping over each item in the static variable output
- name: Check if Service Exists
stat: path=/etc/init.d/{{ item }}
register: output
with_items:
- XXX
- YYY
- ZZZ
- name: Setting fact using output of loop
set_fact:
service_status:
- rc: "{{ item.rc }}"
stdout: "{{ item.stdout }}"
id: "{{ item.id }}"
with_items:
- "{{ output }}"
- debug:
msg: "ID and stdout: {{ item.id }} - {{ item.stdout }}"
with_items:
- "{{ service_status }}"
Initial Answer
IIUC, this link from the Ansible docs shows how to use register inside a loop (see another example in this SO post).
A couple of points
it may be more convenient to assign the list (XXX, YYY, ZZZ) to a separate variable (eg. 1, 2)
I don't know if this is part of the problem, but with_items is no longer the recommended approach to loop over a variable: instead use loop - see here for an example
vars:
items:
- XXX
- YYY
- ZZZ
- name: Check if Service Exists
stat: path=/etc/init.d/{{ item }}
register: service_status
loop: "{{ items|flatten(levels=1) }}"
- name: Show the return code and stdout
debug:
msg: "Cmd {{ item.cmd }}, return code {{ item.rc }}, stdout {{ item.stdout }}"
when: item.rc != 0
with_items: "{{ service_status.results }}"

Ansible condition on dictionary item

I have one dictionary list defined:
members_list:
any:
name: any
username: any
anyone:
name:
username: anyone
In my Ansible playbook, I am running multiple docker containers:
docker_container:
name: "{{ item.key }}"
when: "{{ item.key }} != any"
with_dict: "{{ members_list }}"
But I am getting following error:
The conditional check '{{ item.key }} != any' failed. The
error was: error while evaluating conditional ({{ item.key
}} != any): 'any' is undefined
When I run the playbook without when conditional operator it works fine.
Thanks in advance.
You need to quote the word any, because otherwise it is interpreted as a variable reference. Also, you don't need Jinja templating markers in a when expression (the expression is already evaluated as a jinja expression):
- docker_container:
name: "{{ item.key }}"
when: "item.key != 'any'"
with_dict: "{{ members_list }}"
I find nested quotes like this can be confusing, and often opt for one
of YAMLs alternative quoting mechanisms, e.g.:
- docker_container:
name: "{{ item.key }}"
when: >-
item.key != 'any'
with_dict: "{{ members_list }}"
This is identical to the previous version; I just find it easier to
read and understand.

Is possible define a var at run-time and use it to access another var?

I'm not sure that this is possible.
I want to define a var at run-time and use it to access another var(defined in file,playbook..).
defined at run-time :
typeConfig (possible values: "in_config" or "out_config")
defined in playbook:
in_config:
url_config: http://localhost/configuration
out_config:
url_config: http://config.pi.dyn-dns.org/configuration
I need to resolve something similar to this:
{{ {{ typeConfig }}.url_config }}
I try with:
- name: Mytest
hosts: all
gather_facts: false
sudo: yes
vars:
- in_config:
url_config: http://localhost/configuration
- out_config:
url_config: http://config.pi.dyn-dns.org/configuration
tasks:
- set_fact:
typeConfig: in_config
- name: Value in_config.url_config
debug: msg=" {{in_config.url_config}}"
- name: Value out_config.url_config
debug: msg=" {{out_config.url_config}}"
- name: Value typeConfig
debug: var=typeConfig
- debug: msg="{{ {{ typeConfig }}.url_config }} "
ACTUAL RESULTS
task path: /home/nor/gitrepos/iiot-iac/ansible/myUnitTest.yml:19
fatal: [node1]: FAILED! => {
"failed": true,
"msg": "template error while templating string: expected token ':', got '}'. String: {{ {{ typeConfig }}.url_config }} " } " }
You can access the value using:
- debug:
msg: "{{ vars[typeConfig].url_config }}"
Remember that {{ ... }} is not a way to write a variable name, but to start a Jinja2 expression. And when querying values, variables are referenced using Jinja2 expressions in Ansible, thus using {{ {{ ... }} }} makes no sense.

Resources