Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Providing integer variables from command line (via --extra-vars) recognized as string #14179

Closed
ulucaydin opened this issue Jan 28, 2016 · 2 comments

Comments

@ulucaydin
Copy link

This is for Ansible 2.X:
When I run a playbook via CLI and providing a such extra variable, node_number as integer:

ansible-playbook my_playbook.yml --extra-vars "node_number=10"
I have a array slice operation within a role of my to take a portion of the playbook:

- debug: msg="{{ item }}"
  with_items: `groups.mygroup[:node_number]`

Here is the error I receive:

ok: [localhost]
ERROR! an unexpected type error occurred. Error was slice indices must be integers or None or have an __index__ method

Interestingly, when I remove the variable from CLI and provide the same variable from group_vars/all/common.yml, it works:


---
node_number:10
@bcoca
Copy link
Member

bcoca commented Jan 28, 2016

the key/val parser there does not try to do typing, YAML itself does do typing, if you pass as a json string it would also preserve type as the JSON parser also does typing, but to avoid any input problems you could just do this:
with_items: "{{groups.mygroup[:node_number|int]}}"

@jimi-c
Copy link
Member

jimi-c commented Jan 29, 2016

As @bcoca indicated above, if you want to pass in specific types as extra vars, you should use the ability to pass in JSON or YAML via extra args: http://docs.ansible.com/ansible/playbooks_variables.html#passing-variables-on-the-command-line

If you have any further questions, please let us know by stopping by one of the two mailing lists, as appropriate:

Because this project is very active, we're unlikely to see comments made on closed tickets, but the mailing list is a great way to ask questions, or post if you don't think this particular issue is resolved.

Thank you!

@jimi-c jimi-c closed this as completed Jan 29, 2016
@ansible ansible locked and limited conversation to collaborators Apr 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants