Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible integer variables in YAML

Tags:

I'm using Ansible to deploy a webapp. I'd like to wait for the application to be running by checking that a given page returns a JSON with a given key/value.

I want the task to be tried a few times before failing. I'm therefore using the combination of until/retries/delay keybwords.

Issue is, I want the number of retries to be taken from a variable. If I write :

  retries: {{apache_test_retries}} 

I fall into the usual Yaml Gotcha (http://docs.ansible.com/YAMLSyntax.html#gotchas).

If, instead, I write:

  retries: "{{apache_test_retries}}" 

I'm being said the value is not an integer.

ValueError: invalid literal for int() with base 10: '{{apache_test_retries}}'

Here is my full code:

- name: Wait for the application to be running   local_action:     uri     url=http://{{webapp_url}}/health     timeout=60   register: res   sudo: false   when: updated.changed and apache_test_url is defined   until: res.status == 200 and res['json'] is defined and res['json']['status'] == 'UP'   retries: "{{apache_test_retries}}"   delay: 1 

Any idea on how to work around this issue? Thanks.

like image 919
Alexis Seigneurin Avatar asked Jan 30 '15 11:01

Alexis Seigneurin


People also ask

How do you specify variables in Ansible?

Ansible has a strict set of rules to create valid variable names. Variable names can contain only letters, numbers, and underscores and must start with a letter or underscore. Some strings are reserved for other purposes and aren't valid variable names, such as Python Keywords or Playbook Keywords.

How are global variables defined in Ansible?

Variable Scopes Ansible has 3 main scopes: Global: this is set by config, environment variables and the command line. Play: each play and contained structures, vars entries, include_vars, role defaults and vars. Host: variables directly associated to a host, like inventory, facts or registered task outputs.

How do I use extra vars in Ansible?

Ansible treats values of the extra variables as strings. To pass values that are not strings, we need to use JSON format. To pass extra vars in JSON format we need to enclose JSON in quotation marks: ansible-playbook extra_var_json.


1 Answers

I had the same issue and tried a bunch of things that didn't work so for some time I just worked around without using a variable but found the answer so for everyone who has it.

Daniels solution indeed should work:

retries: "{{ apache_test_retries | int }}" 

However, if you are running a little older version of Ansible it won't work. So make sure you update Ansible. I tested on 1.8.4 and it works and it doesn't on 1.8.2

This was the original bug on ansible: https://github.com/ansible/ansible/issues/5865

like image 115
iblazevic Avatar answered Sep 18 '22 19:09

iblazevic