TL;DR: Ansible modules like
apt can be mostly implemented with
command module, 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
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
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