Stuart Longland
unread,Dec 10, 2018, 5:27:14 PM12/10/18Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to ansible...@googlegroups.com
Hi all,
I ran into a problem yesterday trying to set up a AWS instance via an
Ansible playbook. I'm running Ansible 2.7.1 on Gentoo (AMD64).
I have a host variable, `volume_size`, which is the size of the volume
in GB… and in my playbook, I tried putting the following code:
> - name: Provision instances
> ec2_instance:
> state: present
> security_group: "{{hostvars[item]['security_group']}}"
> key_name: "{{aws_key_name}}"
> region: "{{hostvars[item]['region']}}"
> instance_type: "{{hostvars[item]['instance_type']}}"
> image_id: "{{hostvars[item]['image_id']}}"
> wait: true
> wait_timeout: "{{hostvars[item]['wait_timeout']}}"
> network:
> assign_public_ip: true
> tags:
> Name: '{{item}}'
> JiraStory: "{{hostvars[item].get('jira_story','N/A')}}"
> volumes:
> - device_name: /dev/sdb
> ebs:
> encrypted: "{{hostvars[item].get('encrypted','false') == 'true'}}"
> volume_size: "{{hostvars[item].get('volume_size',100)}}"
> volume_type: "{{hostvars[item].get('volume_type','gp2')}}"
> loop: "{{ groups.aws }}"
> when: "'aws' in groups"
> register: ec2
> tags:
> - aws
That yielded this error:
> An exception occurred during task execution. To see the full traceback, use -vvv. The error was: Invalid type for parameter BlockDeviceMappings[0].Ebs.VolumeSize, value: 100, type: <type 'str'>, valid types: <type 'int'>, <type 'long'>
> failed: [localhost] (item=awstest.on.widesky.cloud) => {"boto3_version": "1.7.19", "botocore_version": "1.10.19", "changed": false, "item": "awstest.on.widesky.cloud", "msg": "Failed to create new EC2 instance: Parameter validation failed:\nInvalid type for parameter BlockDeviceMappings[0].Ebs.Encrypted, value: true, type: <type 'str'>, valid types: <type 'bool'>\nInvalid type for parameter BlockDeviceMappings[0].Ebs.VolumeSize, value: 100, type: <type 'str'>, valid types: <type 'int'>, <type 'long'>"}
> to retry, use: --limit @/home/stuartl/vrt/projects/widesky/ops/deploy/site.retry
I thought, maybe it's being read from the hosts file as a string and
needs to be passed through the `int` constructor. Tried wrapping the
variable in `int()`, that failed, but then I read the jinja2 docs which
said to use `{{variable | int}}`. No problem…
> volumes:
> - device_name: /dev/sdb
> ebs:
> encrypted: "{{hostvars[item].get('encrypted','false') == 'true'}}"
> volume_size: "{{hostvars[item].get('volume_size',100) | int}}"
> volume_type: "{{hostvars[item].get('volume_type','gp2')}}"
I got the same error.
I suspect that the integer is then getting cast *back* to a string
before being passed onto `boto3`, which is then barfing on it.
Is there a way to tell Ansible that a particular template expression is
to be interpreted as an integer and not as a string?
--
Stuart Longland (aka Redhatter, VK4MSL)
I haven't lost my mind...
...it's backed up on a tape somewhere.