如何使用Ansible更新远程主机

时间:2019-04-02 17:50:54

标签: linux ubuntu ansible

我要在每个远程服务器上运行相当于:

sudo apt-get update -y
sudo apt-get upgrade -y 
sudo apt-get dist-upgrade -y

在运行任何命令之前,登录服务器时,我会收到以下消息:

Welcome to Ubuntu 18.10 (GNU/Linux 4.18.0-10-generic x86_64)
...

...
132 packages can be updated.
79 updates are security updates.

然后,我运行以下剧本:

---
- hosts: myserver
  remote_user: root
  become: yes
  become_method: sudo
  tasks:

    - name: "Update packages"
      apt:
        update_cache: yes # apt-get update
        upgrade: full

    - name: "Update dist"
      apt:
         upgrade: dist

    - name: UpdateRaw
      shell: apt-get update -y
    - name: UpgradeRaw
      shell: apt-get upgrade -y
    - name: DistUpgradeRaw
      shell: sudo apt-get dist-upgrade -y

使用命令

ansible-playbook -i hosts update.yml --check

但是当我回到服务器时,我仍然看到相同的消息:

Welcome to Ubuntu 18.10 (GNU/Linux 4.18.0-10-generic x86_64)
...

...
132 packages can be updated.
79 updates are security updates.

如何使用Ansible更新服务器?

1 个答案:

答案 0 :(得分:1)

--check选项在空运行模式下执行。您需要删除--check标志,以便在远程主机上实际执行播放。 正确的命令是:

ansible-playbook -i hosts update.yml

请参见下面的链接

Ansible Dry Run