Ansible has an installation guide ready which will guide you through any missing dependencies as well: https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
There is also the development version on GitHub: https://github.com/ansible/ansible
Here are the basic guidelines, taken from the guide above:
Locate where Python is installed using the following command:
which python3Ensure that
pip(part of Python) is installed by running this command:python3 -m pip -VIf all is well, you should see something like the following:
pip 21.0.1 from /usr/lib/python3.9/site-packages/pip (python 3.9)If so,
pipis available, and you can move on to the next step.If you see an error like
No module named pip, you’ll need to installpipunder your chosen Python interpreter before proceeding. This may mean installing an additional OS package (for example,python3-pip), or installing the latestpipdirectly from the Python Packaging Authority by running the following:curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python3 get-pip.py --userInstall Ansible for the current user using
pipin your selected Python environment:python3 -m pip install --user ansible
You haven't mentioned your Ubuntu release installation, so I assume you can have something above 18.04, for which the above should work out fine.
For anything extra, refer to the guide and Ansible's official website, mentioned in the start of this answer.
Good luck.
Answer from Giorgos Saridakis on askubuntu.comHow to run apt update and upgrade via Ansible shell - Stack Overflow
Does the `ansible.builtin.apt` module provide parameters that only downloads the debian packages and not install it?
APT and packages with interactive configuraton?
Ansible: Difference between the apt module and command: apt-get -y install X (module) - DevOps Stack Exchange
Videos
Ansible has an installation guide ready which will guide you through any missing dependencies as well: https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html
There is also the development version on GitHub: https://github.com/ansible/ansible
Here are the basic guidelines, taken from the guide above:
Locate where Python is installed using the following command:
which python3Ensure that
pip(part of Python) is installed by running this command:python3 -m pip -VIf all is well, you should see something like the following:
pip 21.0.1 from /usr/lib/python3.9/site-packages/pip (python 3.9)If so,
pipis available, and you can move on to the next step.If you see an error like
No module named pip, you’ll need to installpipunder your chosen Python interpreter before proceeding. This may mean installing an additional OS package (for example,python3-pip), or installing the latestpipdirectly from the Python Packaging Authority by running the following:curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python3 get-pip.py --userInstall Ansible for the current user using
pipin your selected Python environment:python3 -m pip install --user ansible
You haven't mentioned your Ubuntu release installation, so I assume you can have something above 18.04, for which the above should work out fine.
For anything extra, refer to the guide and Ansible's official website, mentioned in the start of this answer.
Good luck.
If you are looking to use a package manager to maintain/install Ansible, you will want to use the directions provided in the Ansible documentation. This requires you to install and enable the Ansible ppa repository.
$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible
Source: https://docs.ansible.com/ansible/latest/installation_guide/installation_distros.html#installing-ansible-on-ubuntu
I wouldn't recommend using shell for this, as Ansible has the apt module designed for just this purpose. I've detailed using apt below.
In a playbook, you can update and upgrade like so:
- name: Update and upgrade apt packages
become: true
apt:
upgrade: yes
update_cache: yes
cache_valid_time: 86400 #One day
The cache_valid_time value can be omitted. Its purpose from the docs:
Update the apt cache if its older than the cache_valid_time. This option is set in seconds.
So it's good to include if you don't want to update the cache when it has only recently been updated.
To do this as an ad-hoc command you can run:
$ ansible all -m apt -a "upgrade=yes update_cache=yes cache_valid_time=86400" --become
ad-hoc commands are described in detail here
Note that I am using --become and become: true. This is an example of typical privilege escalation through Ansible. You use -u user and -K (ask for privilege escalation password). Use whichever works for you, this is just to show you the most common form.
Just to add a flavour on the answer. This one is an executable playbook in all the hosts specified in your inventory file.
- hosts: all
become: true
tasks:
- name: Update and upgrade apt packages
apt:
upgrade: yes
update_cache: yes
cache_valid_time: 86400
In other words, why not both be done with an apt module?
We can't say for certain, but likely it's just a mistake coming from ignorance. The latter command can be fully implemented using the Ansible apt module, and you should always prefer that over command; command exists as an escape for when there isn't a module for what you want to do.
TL;DR: Ansible modules like
aptcan be mostly implemented withcommandmodule, but you lose error checking. Using command is usually sign of a lack of experience with ansible.
To answer this question it is best to look at the code implementing the apt module in ansible. What you can see is that most of the features of the module are implemented by constructing an appropriate apt-get command and executing it, then handling error conditions and presenting them in ansible data structures. Sometimes depending on options the command can be quite complex. In two exceptions for upgrade, in case it is full-upgrade or safe-upgrade it would use aptitude command instead of apt-get.
Of course, the implementation of the module can change going forward, but you should be able to rely on it doing essentially the same thing. On the other hand, if apt-get changes in some way or returns new unhandled type of errors, you will need to change your ansible code in case you use command module directly. While if you use apt module, it is likely the maintainer of the module will implement the new error checking for you.
So in general it is better to use apt to command: apt-get unless the module won't do something that you absolutely need to do and there is no other way around it.
To do the 2nd task in ansible using apt module do:
- name: Ensure some basics
apt:
name: aptitude
In my Kubernetes deployment playbook I need to install specific package versions, as follows:
containerd.io=1.2.13-2
docker-ce=5:19.03.11~3-0~ubuntu-focal
docker-ce-cli=5:19.03.11~3-0~ubuntu-focal
If I install them manually, everything works as expected:
apt-get install -y containerd.io=1.2.13-2 docker-ce=5:19.03.11~3-0~ubuntu-focal docker-ce-cli=5:19.03.11~3-0~ubuntu-focal
If I specify the package versions as part of the playbook, I get the correct versions of containerd.io and docker-ce but I get a later version of docker-ce-cli. This seems to have happened by the time Ansible gets to installing docker-ce-cli, although I can't figure out why.
My task is as follows:
- name: "Install Packages"
package: name={{ item }} state=present
with_items:
- containerd.io=1.2.13-2
- docker-ce=5:19.03.11~3-0~ubuntu-focal
- docker-ce-cli=5:19.03.11~3-0~ubuntu-focal
become: yes
register: install_k8s_common_packages
tags: k8s_commonThe error says the task fails because the requirements would end up downgrading docker-ce-cli, so I'm assuming one of the previous packages (containerd.io or docker-ce) has installed docker-ce-cli along with it, although there's nothing in the Ansible messages to suggest that.
What am I doing wrong? Obviously Ansible complains about using apt-get if I use the working command above via the command module.