user11928470
user11928470

Reputation:

How to pass URLs as a variable to an ansible playbook

I created a playbook which clears cache, I'm trying to pass a url to a variable, and when I execute the playbook I get an empty array for that parameter.

In my playbook I have a vars module with this variable (environment), it gets defined when you pass in a variable to the ansible-playbook command

vars:
    environment: "{{testenv}}"
-e testenv=https://www.test1.com

When I execute the playbook I get this error.
Do I need to format the url in someway?

fatal: [localhost]: FAILED! => {"changed": false, "msg": "unknown url type: '[]/content/clear_cache?

Upvotes: 1

Views: 2197

Answers (1)

β.εηοιτ.βε
β.εηοιτ.βε

Reputation: 39194

Your issue is coming from the fact that environment is a reserved variable, as pointed in the second row of this table in the documentation:

Valid variable names  Not valid
 foo  *foo, Python keywords such as async and lambda
 foo_env  playbook keywords such as environment
 foo_port  foo-port, foo port, foo.port
 foo5, _foo  5foo, 12

Source: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#creating-valid-variable-names

So, you just need to change your variable name to something else and it will work.

Given the playbook:

- hosts: all
  gather_facts: no

  tasks:
    - debug:
        msg: "{{ _environment }}"
      vars:
        _environment: "{{ testenv }}"

When run:

$ ansible-playbook play.yml -e testenv=https://www.test1.com

PLAY [all] **********************************************************************************************************

TASK [debug] ********************************************************************************************************
ok: [localhost] => {
    "msg": "https://www.test1.com"
}

PLAY RECAP **********************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Upvotes: 1

Related Questions