Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run apt update and upgrade via Ansible shell

I'm trying to use Ansible to run the following two commands:

sudo apt-get update && sudo apt-get upgrade -y

I know with ansible you can use:

ansible all -m shell -u user -K -a "uptime"

Would running the following command do it? Or do I have to use some sort of raw command

ansible all -m shell -u user -K -a "sudo apt-get update && sudo apt-get upgrade -y"

like image 862
nadermx Avatar asked Jan 08 '17 17:01

nadermx


People also ask

How do I upgrade apt-get?

How to Use the sudo apt-get upgrade Command. After running the sudo apt-get update command, in the same terminal window, type in sudo apt-get upgrade , enter your password if necessary, and hit enter.

What is apt-get update and upgrade?

What is the difference between apt update and apt upgrade? “apt-get update” updates the package sources list to get the latest list of available packages in the repositories and “apt-get upgrade” updates all the packages presently installed in our Linux system to their latest versions.

What is apt in Ansible playbook?

Ansible APT Package manager is an Ubuntu equivalent for RedHat yum package manager. Just like all other ansible modules apt ansible module is built after one specific unix command of Debian apt-get.


1 Answers

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.

like image 182
Tom Manterfield Avatar answered Oct 31 '22 01:10

Tom Manterfield