Consider these YAML tasks which are based on two different parts of some locally-executed Ansible playbook I read here:

- name: Update the apt package-index cache i.e. apt-get update
  apt: update_cache=yes

- name: Ensure aptitude is installed
  command: apt-get install -y aptitude

As an Ansible newbie, I recognize two different Ansible modules here: apt and command.

Why did the OP use both apt: update_cache=yes and command: apt-get install -y instead using the apt module in both tasks?

Note: I myself no longer use apt-get but apt even in regular Bash scripting.

  • I don't think so and I won't argue. I want no discussion with you after I got the impression you don't want to talk seriously with me here given the lies you wrote about me. FYI, I ask questions here and in Unix&Linux Stackexhcange while Reading the good book by Geerling "Ansible for DevOps" although I'm not a DevOp and learning Ansible bits by bits in an orderly way and had successions and will succeed in what I'll succeed. I have no time for sarcasm and what I recognize as violations of StackExhcnage code of conduct. I call upon you not to reply to my questions are. I missed the -y there. – JohnDoea Dec 19 '18 at 18:35
  • That's no big deal. I actually improved the question making the tasks more loyal to the OP. I don't want to go on with this conversation after your attitude towards me which I found uncommunal. – JohnDoea Dec 19 '18 at 18:36

 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 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 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
    name: aptitude
  • I think I misunderstood the question there. Xiong is giving the simple answer. There is no reason for the 2nd to be done through command. – Jiri Klouda Dec 18 '18 at 21:16

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.