Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ansible, is there a way to force variable values to integer?

I'm trying to use ecs_taskdefinition module for Ansible (v2.0), and I think I'm stuck in the basic Ansible YAML gotcha.

As per the example for the module, if I provide integer values for cpu and memory, this works as expected:

- name: "Create task definition"
  ecs_taskdefinition:
    containers:
    - name: simple-app
      cpu: 10
      memory: 300
      essential: true
      image: "httpd:2.4"
      portMappings:
      - containerPort: 80
        hostPort: 80

Although, I would like the memory and cpu to be templatable. So that I will be able to use same container definition for different environments.

APP_ENV: "test"
test:
  containers:
    simple_app:
      memory: 1920
      cpu: 2560

- name: "Create task definition"
  ecs_taskdefinition:
    containers:
    - name: simple-app
      cpu: "{{vars.get(APP_ENV).containers.simple_app.cpu | int}}"
      memory: "{{vars.get(APP_ENV).containers.simple_app.memory | int}}"
      essential: true
      image: "httpd:2.4"
      portMappings:
      - containerPort: 80
        hostPort: 80

With above, I get error from the botocore API:

botocore.exceptions.ParamValidationError: Parameter validation failed:
Invalid type for parameter containerDefinitions[0].memory, value: 1920, type: <type 'str'>, valid types: <type 'int'>, <type 'long'>
Invalid type for parameter containerDefinitions[0].cpu, value: 2560, type: <type 'str'>, valid types: <type 'int'>, <type 'long'>

Is this fixable without having to update the Ansible module to actually try to convert these values to integers?

like image 303
Nikhil Owalekar Avatar asked Feb 20 '16 01:02

Nikhil Owalekar


People also ask

What does {{ }} mean in Ansible?

Ansible uses the jinja2 template. the {{ }} are used to evaluate the expression inside them from the context passed. So {{ '{{' }} evaluates to the string {{ And the while expression {{ docroot }} is written to a template, where docroot could be another template variable.

How do I use dynamic variables in Ansible?

Create an Ansible Playbook which will dynamically load the variable file named the same as OS_name and just by using the variable names we can Configure our target node. (Note: No need to use when keyword here.)

What is variable precedence in Ansible?

In general, Ansible gives precedence to variables that were defined more recently, more actively, and with more explicit scope. Variables in the defaults folder inside a role are easily overridden. Anything in the vars directory of the role overrides previous versions of that variable in the namespace.

Can we pass variables in playbook?

With Ansible, users have the flexibility to accept external input while executing their Ansible playbooks without changing the Ansible playbook content. This can be done via the ability to pass extra variables to an Ansible playbook. This feature is available when using the Ansible CLI or Ansible Tower.


1 Answers

It seems to be working in Ansible version 2.1.1.0. If you can't get it to work, a possible solution is to define the variables a the top level of the dictionary and not using the int filter…

vars:
  APP_ENV: test
  simple_app_container_cpu: 2560
  simple_app_container_ram: 1920

tasks:
  - name: Create task definition
    ecs_taskdefinition:
      containers:
        - name: simple-app
          cpu: "{{simple_app_container_cpu}}"
          memory: "{{simple_app_container_ram}}"

Note: I used ram instead of memory because I like how it lines up :)

like image 174
Shammel Lee Avatar answered Oct 08 '22 21:10

Shammel Lee