检查 Ansible 中是否安装了 Chocolatey

Check if Chocolatey is installed in Ansible

我最近开始使用 Ansible 并尝试在无法访问 Internet 的环境中应用它。

我已经设法创建了一个使用文件和模板安装 Chocolatey 的剧本,但目前每次我 运行 该剧本时它都会安装 Chocolatey。我目前正在使用的任务是:

---
- name: Create C:\temp
  win_file:
    path: C:\temp
    state: directory

- name: Save InstallChocolatey.ps1 file
  template:
    src: InstallChocolatey.ps1.j2
    dest: c:\temp\InstallChocolatey.ps1

- name: Run InstallChocolatey.ps1
  win_shell: C:\temp\InstallChocolatey.ps1

有没有办法检查是否已经安装了 Chocolatey?使用它,我将能够使用块以及何时避免重复执行操作。

感谢大家提出的任何建议:)

您可以添加一个任务来检查 choco 命令是否准备就绪。并在 choco 不可用时执行脚本 InstallChocolatey.ps1

---
- name: Check if Chocolatey is already installed
  win_shell: (Get-Command choco).Path
  register: get_command_choco

- name: Create C:\temp
  win_file:
    path: C:\temp
    state: directory

- name: Save InstallChocolatey.ps1 file
  template:
    src: InstallChocolatey.ps1.j2
    dest: c:\temp\InstallChocolatey.ps1

- name: Run InstallChocolatey.ps1
  win_shell: C:\temp\InstallChocolatey.ps1
  when: not get_command_choco.stderr == ""