Print Ansible variable to command line from ad-hoc command

Viewed 1023

I currently have a YAML file which defines several variables:

variable_a: some_value_1
variable_b: some_value_2
variable_c: some_value_3

Is there any ad hoc command I can run from the terminal that will allow me to selectively print the value of each of these variables? E.g.

ansible --file=my_file.yml --print-var=variable_a

would print

some_value_1

I need to access these variables inside a bash script, so I can't just use include_vars.

1 Answers

You have to pass your variables file with the --extra-vars or -e option for the ad-hoc command.

Given a YAML file with variables my_vars.yml

variable_a: some_value_1
variable_b: some_value_2
variable_c: some_value_3

Run ad-hoc command:

~$ ansible localhost -m debug -a "var=variable_a" -e @my_vars.yml

localhost | SUCCESS => {
  "variable_a": "some_value_1"
}

If you want to trim the output:

~$ ansible localhost -m debug -a "var=variable_a" -e @my_vars.yml | grep variable_a | awk '{print $2}'
"some_value_1"
Related