Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System specific variables in ansible

Tags:

ansible

Ansible expects python 2. On my system (Arch Linux), "python" is Python 3, so I have to pass -e "ansible_python_interpreter=/usr/bin/python2" with every command.

ansible-playbook my-playbook.yml -e "ansible_python_interpreter=/usr/bin/python2" 

Is there a away to set ansible_python_interpreter globally on my system, so I don't have to pass it to every command? I don't want to add it to my playbooks, as not all systems that runs the playbook has a setup similar to mine.

like image 599
August Lilleaas Avatar asked Mar 31 '14 18:03

August Lilleaas


People also ask

How do you specify variables in Ansible?

Ansible has a strict set of rules to create valid variable names. Variable names can contain only letters, numbers, and underscores and must start with a letter or underscore. Some strings are reserved for other purposes and aren't valid variable names, such as Python Keywords or Playbook Keywords.

What are special variables in Ansible?

These are variables that contain information pertinent to the current host ( inventory_hostname ). They are only available if gathered first.

How to define variables in Ansible playbook?

Playbook Variables To define a variable in a playbook, simply use the keyword vars before writing your variables with indentation. To access the value of the variable, place it between the double curly braces enclosed with quotation marks. Here's a simple playbook example: - hosts: all vars: greeting: Hello world!


2 Answers

I opted to use Ansible's ability to source inventory from a directory. In this manner I could define the ansible_python_interpreter for localhost only for the local machine

inventory_dir/local

[local] localhost ansible_python_interpreter="/path/to/alternate/bin/python" 

And then just use the directory as you would an inventory file.

ansible-playbook -i inventory_dir playbook.yml

like image 27
TomDotTom Avatar answered Sep 21 '22 13:09

TomDotTom


Well you can set in three ways

  1. http://docs.ansible.com/intro_inventory.html#list-of-behavioral-inventory-parameters ansible_python_interpreter=/usr/bin/python2 this will set it per host
  2. Set it host_vars/ ansible_python_interpreter: "/usr/bin/python2" this will set it per host
  3. set it for all nodes in the file group_vars/all (you may need to create the directory group_vars and the file all) as ansible_python_interpreter: "/usr/bin/python2"

Hope that helps

like image 51
DomaNitro Avatar answered Sep 20 '22 13:09

DomaNitro