0

I've recently started using Ansible and trying to apply it in an environment that doesn't have access to the Internet.

I've managed to create a playbook that installs Chocolatey using files and templates but at the moment it installs Chocolatey every time I run the playbook. The tasks that I'm currently using are:

---
- 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

Is there a way to check if Chocolatey is already installed? Using this, I will be able to use a block and when to avoid performing the actions repeatedly.

Thanks for any recommendations people may have :)

Sam Jenkins
  • 1,284
  • 1
  • 12
  • 30
  • 2
    There is a `creates` parameter for [win_shell module](https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_shell_module.html). You can specify the path which will be present when Chocolatey is installed. – seshadri_c Jan 16 '21 at 16:59
  • 1
    [the fine manual for `win_chocolatey:`](https://docs.ansible.com/ansible/2.10/collections/chocolatey/chocolatey/win_chocolatey_module.html) says it will install chocolatey if is missing, so presuming the _next_ thing you're going to do with ansible is install some chocolety packages, then this question becomes moot – mdaniel Jan 17 '21 at 01:56
  • @mdaniel, As mentioned in the original question, this is an environment without Internet access. The next thing is to install some chocolatey packages from an **offline repository** (I'm using Nexus in this case). – Sam Jenkins Jan 17 '21 at 08:25

1 Answers1

1

You can add a task to check choco command is ready. And execute script InstallChocolatey.ps1 when choco is not available.

---
- 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 == ""
Jean-Pierre Matsumoto
  • 1,917
  • 1
  • 18
  • 26
  • That's perfect, thanks. I had started doing a check for the default Choco install location but your approach would be more robust. I've also put all the other steps inside of a block so I don't create the dir or put the files there unless necessary. – Sam Jenkins Jan 17 '21 at 11:06