如何通过Ansible shell运行apt更新和升级

我正在尝试使用Ansible来运行以下两个命令:

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

我知道你可以使用ansible:

ansible all -m shell -u user -K -a“uptime”

会运行以下命令吗?或者我必须使用某种原始命令

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

我不建议使用shell,因为Ansible有专门为此目的设计的apt模块。我在下面详细使用了apt。

在剧本中,您可以像这样更新和升级

- name: Update and upgrade apt packages
  become: true
  apt:
    upgrade: yes
    update_cache: yes
    cache_valid_time: 86400 #One day

cache_valid_time值可以省略。其目的来自docs

Update the apt cache if its older than the cache_valid_time. This
option is set in seconds.

因此,如果您不想在最近才更新缓存时更新缓存,那么最好包括在内。

要以ad-hoc命令执行此操作,您可以运行:

$ ansible all -m apt -a“upgrade = yes update_cache = yes cache_valid_time = 86400” – become

ad-hoc命令在here中有详细描述

请注意,我正在使用–become并成为:true。这是通过Ansible进行典型权限提升的示例。您使用-u user和-K(请求权限提升密码)。使用适用于您的任何一种,这只是为了向您展示最常见的形式。

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...