Ignore a module option

21 views
Skip to first unread message

Mateus Dubiela Oliveira

unread,
Feb 17, 2015, 10:02:05 AM2/17/15
to ansible...@googlegroups.com
Hello,

I'm trying to create a helper role to raise ec2 instances, and I stumbled in a problem where I wanted to deleted or ignore some module options, based on the role vars.
First, let's say I have a playbook like this.

---
- name: Raise ec2 instance
    ec2:
      key_name: my_key
      instance_type: m1.medium
      image: i999999
      region: us-east-1
      zone: us-east-1e
      wait: 1000
      count_tags:
        tag: value
      exact_count: "{{my_count}}"

If I wanted to ignore the 'exact_count' option one may think that if you set 'my_count' to 'null' or '!!null' it will be interpreted as 'None', the default value for that option, and the module will behave as if it was not set. But because of this issue, which created this codethe interpreter now translates the None values to a empty string, which then makes the interpreter crash like so.

TASK: [aws-instance | Raise ec2 instance] ***************************
failed: [localhost] => {"failed": true, "parsed": false}
Traceback (most recent call last):
  File "/home/user/.ansible/tmp/ansible-tmp-9999999999.99-999999999999999/ec2", line 2962, in <module>
    main()
  File "/Users/user/.ansible/tmp/ansible-tmp-9999999999.99-999999999999999/ec2", line 1164, in main
    ['exact_count', 'instance_ids']
  File "/home/user/.ansible/tmp/ansible-tmp-9999999999.99-999999999999999/ec2", line 1504, in __init__
    self._check_argument_types()
  File "/home/user/.ansible/tmp/ansible-tmp-9999999999.99-999999999999999/ec2", line 2160, in _check_argument_types
    self.params[k] = int(value)
ValueError: invalid literal for int() with base 10: ''


FATAL: all hosts have already failed -- aborting

IMHO this is unwanted behaviour, since there is no way to unset a module option or even ignore then. I don't know if I'm missing something, but if I do, please show me :)

Thanks,

Brian Coca

unread,
Feb 17, 2015, 10:03:39 AM2/17/15
to ansible...@googlegroups.com
try:

exact_count: "{{my_count|default(omit)}}"

---------------
Brian Coca

Mateus Dubiela Oliveira

unread,
Feb 17, 2015, 10:19:24 AM2/17/15
to ansible...@googlegroups.com
Thanks! 

Sorry for the stupid question, I should've looked up a little better :(
I've managed to obtain the behaviour I wanted like this:

my_aws_role/tasks/main.yml
- name: Raise ec2 instance
    ec2:
      key_name: my_key
      instance_type: m1.medium
      image: i999999
      region: us-east-1
      zone: us-east-1e
      wait: 1000
      count_tags: "{{my_tags}}"
      exact_count: "{{my_count}}"

my_aws_role/defaults/main.yml
exact_count: "{{ omit }}"
my_tags: "{{mandatory}}"

create-instance.yml
- name: Create a pong server
  hosts: localhost:
  vars:
     exact_count: 1
     tags:
       server_app: pong
  roles:
    - my_aws_role


Thank you again!
Reply all
Reply to author
Forward
0 new messages