Best Approach- Writing powershell scripts or using Modules?

372 views
Skip to first unread message

Suporter

unread,
Mar 27, 2017, 1:33:15 AM3/27/17
to Ansible Project
I am new to ansible,  what is best approach , is it to use ansible's windows modules for all the things or writing powershell scripts and running that powershell script in all servers through ansible. For example: i have a flow
1) Download files from an url
2) Extract if it is zip
3) Deploy in IIS 

We can do this easily in powershell, only thing we cannot do in powershell is to install the scripts from a centralised server, but ansible does that. So what could be best approach for this?

J Hawkesworth

unread,
Mar 27, 2017, 3:44:37 AM3/27/17
to Ansible Project
Well you can use ansible as a way of delivering and running powershell scripts and if that's all you need to do, that's fine. 

However I think its worth making use of modules and roles and templates and group vars and filters and 'doing stuff the ansible way' as once you have deployment sorted out, there's maintenance and running updates and all manner of other activities that can make use of your modules and roles.

Modules and playbooks can be more readable than raw powershell scripts so if you need to have deployment plans, playbooks can be a good way to communicate with people who aren't comfortable with a lot of code too.

Ultimately it depends on what you are optimising for.

Incidentally you can do all the steps below without any powershell if you want using something like the below (untested).  If you can arrange things so the build contains a version number, you could also uncommente the 'creates' line and make the playbook pretty much idempotent, making it safe to re-run and


# playbook: deploy build to iis
# {{build_file}} would contain the file name of your built application.  You could pass into the playbook using -e extra_vars command line, or pick up from an included file.

---

- hosts: iis-webservers
  tasks:

  - name: download build
    win_get_url:
      url: "http:/build-server/{{build_file}}"
      dest: "C:\\staging\\{{build_file}}"
      creates: "C:\\staging\\{{build_file}}"
      register: download_state

  - name: unzip if a zip file
    win_unzip:
      src: "C:\\staging\\{{build_file}}"
      dest: C:\iis_site_location
  #  creates: C:\iis_site_location\build_version_{{version_number}}.txt"
    when: download_state.dest|search('.zip')

  - name: restart iis if a new file was downloaded
    win_shell: iisreset.exe /restart
    when: download_state|changed

## end

You might well be able to do the same in 22 lines of powershell, but I think the above would a/ get the job done and let you move on to other things, b/ (arguably) be easier for non-programmers/scripters to understand how the deployment works.

Hope this helps,

Jon

Suporter

unread,
Mar 27, 2017, 4:19:45 AM3/27/17
to Ansible Project
Intersting, but i am still new to ansible, will take some more days to get used to it.. What does creates do?

J Hawkesworth

unread,
Mar 27, 2017, 7:35:58 AM3/27/17
to Ansible Project
It tells the module that what the expected outcome of making the desired change is, so if the file is present, the module 'knows' it doesn't need to make any further changes.

If you make all your tasks work like this, you can re-run your playbooks in the knowledge that they will only make changes that haven't already been made.  This speeds things up and eventually sort of pulls your machines into the state described by your playbooks.  Read up on 'idempotency' and 'desired state' for a wider discussion of this idea and how it can be useful.

All the best,

Jon
Reply all
Reply to author
Forward
0 new messages