How can I pass yaml array to --extra-vars
in Ansible playbook. Ansible documentation does not declares its syntax nor I can find that on any internet resource.
I mean if I have a playbook:
--- - hosts: {{hostName}} - remote_user: admin ...
Then I should call my playbook like
ansible-playbook DeployWar.yml --extra-vars="hostName=tomcat-webApp"
But I want to run this playbook on two servers say tomcat-webApp
and tomcat-all
, and I want to control it from out side i.e. using --extra-vars
. What I have tried to do is:
ansible-playbook DeployWar.yml --extra-vars="hostName=[tomcat-webApp, tomcat-all]" ansible-playbook DeployWar.yml --extra-vars="hostName={tomcat-webApp, tomcat-all}" ansible-playbook DeployWar.yml --extra-vars="[{hostName: tomcat-webApp}, {hostName: tomcat-all}]"
But in all cases playbook fails declaring a syntax error in my call. Any help appreciated.
To pass values that are not strings, we need to use JSON format. To pass extra vars in JSON format we need to enclose JSON in quotation marks: ansible-playbook extra_var_json. yml -e '{"my_float": 1.618, "my_list": ["HGTTG", 42], "my_string": "Time is an illusion."}'
The group_vars in Ansible are a convenient way to apply variables to multiple hosts at once. Group_vars is an Ansible-specific folder as part of the repository structure. This folder contains YAML files created to have data models, and these data models apply to all the devices listed in the hosts. ini file.
The easiest way to pass Pass Variables value to Ansible Playbook in the command line is using the extra variables parameter of the “ansible-playbook” command. This is very useful to combine your Ansible Playbook with some pre-existent automation or script.
include_vars module – Load variables from files, dynamically within a task. Note. This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name include_vars even without specifying the collections: keyword.
To answer your first question "How can I pass yaml array to --extra-vars in Ansible playbook." you can pass in a json formatted string to extra-vars.
Here is an example play:
- hosts: all gather_facts: no tasks: - debug: var=test_list
And how to pass in test_list to ansible-playbook:
ansible-playbook -c local -i localhost, test.yml --extra-vars='{"test_list": [1,2,3]}'
Though you can use a variable for hosts I recommend checking out Ansible's mechanism for host management which is inventory in conjunction with the --limit
option.
As of Ansible 1.3, extra vars can be formatted as YAML, either on the command line or in a file. See the Ansible documentation titled Defining Variables At Runtime.
Example:
--extra-vars "@some_file.yaml"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With