How to install ansible in your Linux or Mac OS system ?
Today we will look how to install ansible on our Linux system using pip or pip3 command.
To understand the installation process, lets have a look step by step.
1. Upgrade your PIP install.
user@ngelinux$ pip3 install --upgrade pip Collecting pip Downloading https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl (1.3MB) 100% |################################| 1.3MB 333kB/s Installing collected packages: pip Found existing installation: pip 9.0.3 Uninstalling pip-9.0.3: Successfully uninstalled pip-9.0.3 Successfully installed pip-10.0.1 user@ngelinux$
2. Uninstall any older version of ansible.
user@ngelinux$ pip3 uninstall ansible Uninstalling ansible-2.5.3: Would remove: /Library/Frameworks/Python.framework/Versions/3.6/bin/ansible /Library/Frameworks/Python.framework/Versions/3.6/bin/ansible-config /Library/Frameworks/Python.framework/Versions/3.6/bin/ansible-connection /Library/Frameworks/Python.framework/Versions/3.6/bin/ansible-console /Library/Frameworks/Python.framework/Versions/3.6/bin/ansible-doc /Library/Frameworks/Python.framework/Versions/3.6/bin/ansible-galaxy /Library/Frameworks/Python.framework/Versions/3.6/bin/ansible-inventory /Library/Frameworks/Python.framework/Versions/3.6/bin/ansible-playbook /Library/Frameworks/Python.framework/Versions/3.6/bin/ansible-pull /Library/Frameworks/Python.framework/Versions/3.6/bin/ansible-vault /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ansible-2.5.3-py3.6.egg-info /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ansible/* Proceed (y/n)? y Successfully uninstalled ansible-2.5.3 user@ngelinux$
3. Install latest version of ansible.
user@ngelinux$ pip3 install ansible Collecting ansible Downloading https://files.pythonhosted.org/packages/20/c1/2a1a6104f7660b5d6b9876ea351f24212c19ce17c03771baf94f1c81d13b/ansible-2.5.3.tar.gz (10.1MB) 100% |################################| 10.1MB 1.8MB/s Requirement already satisfied: jinja2 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from ansible) (2.10) Requirement already satisfied: PyYAML in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from ansible) (3.12) Requirement already satisfied: paramiko in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from ansible) (2.4.1) Requirement already satisfied: cryptography in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from ansible) (2.2.2) Requirement already satisfied: setuptools in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from ansible) (39.0.1) Requirement already satisfied: MarkupSafe>=0.23 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from jinja2->ansible) (1.0) Requirement already satisfied: pyasn1>=0.1.7 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from paramiko->ansible) (0.4.2) Requirement already satisfied: pynacl>=1.0.1 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from paramiko->ansible) (1.2.1) Requirement already satisfied: bcrypt>=3.1.3 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from paramiko->ansible) (3.1.4) Requirement already satisfied: idna>=2.1 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from cryptography->ansible) (2.6) Requirement already satisfied: asn1crypto>=0.21.0 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from cryptography->ansible) (0.24.0) Requirement already satisfied: six>=1.4.1 in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from cryptography->ansible) (1.11.0) Requirement already satisfied: cffi>=1.7; platform_python_implementation != "PyPy" in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from cryptography->ansible) (1.11.5) Requirement already satisfied: pycparser in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from cffi>=1.7; platform_python_implementation != "PyPy"->cryptography->ansible) (2.18) Installing collected packages: ansible Running setup.py install for ansible ... done Successfully installed ansible-2.5.3 user@ngelinux$
4. Create executable link of “ansible” binary in /usr/local/bin directory to have it in $PATH variable.
user@ngelinux$ ln -s /Library/Frameworks/Python.framework/Versions/3.6/bin/ansible /usr/local/bin/ansible user@ngelinux$ ansible --version ansible 2.5.3 config file = None configured module search path = ['/var/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ansible executable location = /usr/local/bin/ansible python version = 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] user@ngelinux$
5. Create link of ansible-playbook to /usr/local/bin.
user@ngelinux$ ln -s /Library/Frameworks/Python.framework/Versions/3.6/bin/ansible-playbook /usr/local/bin/ansible-playbook user@ngelinux$ ansible-playbook --version ansible-playbook 2.5.3 config file = None configured module search path = ['/var/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/ansible executable location = /usr/local/bin/ansible-playbook python version = 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 03:03:55) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] user@ngelinux$
6.Now go to your home directory and create two files say “host” containing list of hosts, and “test.yml” i.e. the YML script file containing the instructions.
user@ngelinux$ ls -ltr total 8 -rw-r--r-- 1 saket1447583 staff 19 May 21 21:33 host -rw-r--r-- 1 saket1447583 staff 0 May 21 21:33 test1.yml user@ngelinux$
Now you can write instructions in the yml file and let the ansible-playbook command execute it.
In case pip is not available, we need to install it like below in Mac OS where python 2.7.10 is installed. $ curl https://bootstrap.pypa.io/get-pip.py -o getpip.py % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1604k 100 1604k 0 0 224k 0 0:00:07 0:00:07 –:–:– 407k $ ls getpip.py $ $ sudo python getpip.py The directory ‘/Users/ngelinux/Library/Caches/pip/http’ or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may… Read more »