You can use package facts for that:
https://docs.ansible.com/ansible/latest/modules/package_facts_module.html

On Wednesday, February 5, 2020 at 10:27:01 AM UTC+1, Stefan Hornburg 
(Racke) wrote:
>
> On 2/5/20 9:48 AM, Adrian Sebastian Dutu wrote: 
> > Why are you using the command module? 
> > 
> > Use the package module. Then it will not matter what package manager the 
> distro is running. 
> > If you have ansible module available for what you want to do, never use 
> command. I would rewrite the entire play. 
> > 
>
> You can use the package module, but it will not be able to list packages 
> and the names of packages might 
> differ between different distros etc. 
>
> Regards 
>          Racke 
>
> > 
> > 
> > On Wednesday, February 5, 2020 at 2:08:01 AM UTC+1, Work-Hard wrote: 
> > 
> >     Hello, 
> >     Do you recommend Migrating playbook from ubuntu to amazon Linux 2 
> (as it uses *yum instead of apt*). If so, how can 
> >     I check if missing add a package to a new instance to make sure it 
> installs python-apt*?* 
> >     *Ansible-Playbook* 
> >     --- 
> >     - hosts: amazonlinux2 
> >       become: true 
> >       any_errors_fatal: true 
> >       serial: 1 
> >       max_fail_percentage: 0 
> >       vars: 
> >         ansible_user: ec2-user 
> >       tasks: 
> >         # do an "apt-get update", to ensure latest package lists 
> >         - name: apt-get update 
> >           apt: 
> >             update-cache: yes 
> >           changed_when: 0 
> > 
> >         # get a list of packages that have updates 
> >         - name: get list of pending upgrades 
> >           command: apt-get --simulate dist-upgrade 
> >           args: 
> >             warn: false # don't warn us about apt having its own plugin 
> >           register: apt_simulate 
> >           changed_when: 0 
> > 
> >         - name: Update cache 
> >           apt: 
> >             update-cache: yes 
> >           changed_when: false 
> > 
> >         - name: Fetch package list of updates 
> >           command: apt list --upgradable 
> >           register: aptlist 
> > 
> >         - set_fact: 
> >             updates: "{{ aptlist.stdout_lines | 
> difference(['Listing...']) 
> >     | map('regex_replace', '^(.*?)/(.*)', '\\1') | list }}" 
> > 
> >         - debug: var=updates 
> > 
> >         # tell user about packages being updated 
> >         - name: show pending updates 
> >           debug: 
> >             var: updates 
> >           when: updates.0 is defined 
> > 
> >         # comment this part in if you want to manually ack each server 
> update 
> >         - pause: 
> >           when: updates.0 is defined 
> > 
> >         # if a new kernel is incoming, remove old ones to avoid full 
> /boot 
> >         - name: apt-get autoremove 
> >           command: apt-get -y autoremove 
> >           args: 
> >             warn: false 
> >           when: '"Inst linux-image-" in apt_simulate.stdout' 
> >           changed_when: 0 
> > 
> >         # do the actual apt-get dist-upgrade 
> >         - name: apt-get dist-upgrade 
> >           apt: 
> >             upgrade: dist # upgrade all packages to latest version 
> >           register: upgrade_output 
> > 
> >         # check if we need a reboot 
> >         - name: check if reboot needed 
> >           stat: path=/var/run/reboot-required 
> >           register: file_reboot_required 
> > 
> >         # "meta: end_play" aborts the rest of the tasks in the current 
> «tasks:» 
> >         # section, for the current webserver 
> >         # "when:" clause ensures that the "meta: end_play" only triggers 
> if the 
> >         # current server does _not_ need a reboot 
> >         - meta: end_play 
> >           when: not file_reboot_required.stat.exists 
> > 
> >         # because of the above meta/when we at this point know that the 
> current 
> >         # host needs a reboot 
> > 
> >         # prompt for manual input before doing the actual reboot 
> >         - name: Confirm reboot of ec2-user 
> >           pause: 
> > 
> >         - name: reboot node 
> >           shell: sleep 2 && shutdown -r now "Reboot triggered by 
> ansible" 
> >           async: 1 
> >           poll: 0 
> >           ignore_errors: true 
> > 
> >         # poll ssh port until we get a tcp connect 
> >         - name: wait for node to finish booting 
> >           become: false 
> >           local_action: wait_for host=ec2-user 
> >               port=22 
> >               state=started 
> >               delay=5 
> >               timeout=600 
> > 
> >         # give sshd time to start fully 
> >         - name: wait for ssh to start fully 
> >           pause: 
> >             seconds: 15 
> > 
> >         # wait a few minutes between hosts, unless we're on the last 
> >         - name: waiting between hosts 
> >           pause: 
> >             minutes: 10 
> >           when: inventory_hostname != ansible_play_hosts[-1] 
> > 
> >     * 
> >     * 
> >     *Ansible-Output* 
> >     PLAY [amazonlinux2] 
> >     
> ******************************************************************************************************************************************
>  
>
> > 
> >     TASK [Gathering Facts] 
> >     
> ***************************************************************************************************************************************
>  
>
> >     [WARNING]: Platform linux on host 10.11.12.13 is using the 
> discovered Python interpreter at /usr/bin/python, but 
> >     future installation of another Python 
> >     interpreter could change this. See 
> >     
> https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html
>  
> >     <
> https://docs.ansible.com/ansible/2.9/reference_appendices/interpreter_discovery.html>
>  
> for more information. 
> > 
> >     ok: [10.11.12.13] 
> > 
> >     TASK [apt-get update] 
> >     
> ****************************************************************************************************************************************
>  
>
> >     [WARNING]: Updating cache and auto-installing *missing dependency: 
> python-apt* 
> > 
> >     fatal: [10.11.12.13]: FAILED! => {"changed": false, "cmd": "apt-get 
> update", "msg": "[Errno 2] No such file or 
> >     directory", "rc": 2} 
> > 
> >     NO MORE HOSTS LEFT 
> >     
> *******************************************************************************************************************************************
>  
>
> > 
> >     NO MORE HOSTS LEFT 
> >     
> *******************************************************************************************************************************************
>  
>
> > 
> >     PLAY RECAP 
> >     
> ***************************************************************************************************************************************************
>  
>
> >     10.11.12.13                 : ok=1    changed=0    unreachable=0    
> failed=1    skipped=0    rescued=0    ignored=0 
> > 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> Groups "Ansible Project" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an email to 
> > ansible...@googlegroups.com <javascript:> <mailto:
> ansible-project+unsubscr...@googlegroups.com <javascript:>>. 
> > To view this discussion on the web visit 
> > 
> https://groups.google.com/d/msgid/ansible-project/21203f8f-5383-47e9-ad8e-feb8cf8cb34f%40googlegroups.com
>  
> > <
> https://groups.google.com/d/msgid/ansible-project/21203f8f-5383-47e9-ad8e-feb8cf8cb34f%40googlegroups.com?utm_medium=email&utm_source=footer>.
>  
>
>
>
> -- 
> Ecommerce and Linux consulting + Perl and web application programming. 
> Debian and Sympa administration. Provisioning with Ansible. 
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/cd5f05b9-fc16-4599-b2c8-56b8bc825ab3%40googlegroups.com.

Reply via email to