DEV Community


Posted on

Additional tips for writing Playbooks

Below are a few additional tips for when writing your first few playbooks. These will show you some of the peculiarities of Ansible.

Management of software

Installation is dependent upon the platform. For Unix-like systems you can use the package manager of the distribution and for Windows there is no package manager provided. Ansible has the chocolatey module, which is a package manager, that allows you to manage software automatically.

- name: install chocolatey
      - chocolatey
      - chocolatey-core.extension
    state: present
Enter fullscreen mode Exit fullscreen mode


In general the shell and command modules, or win_shell and win_command for Windows respectively, should not be used as they are not able to use exception and error handling. Even though modules can do a lot, in some cases you must use the shell or command modules.

There is very little difference between the shell and command modules. However the shell module runs the command in a shell and therefore variables and operations like $HOME, >, >>, and | are available. On the other hand, command is safer as it is not dependent on the host environment.

- name: npm install
    command: npm install /<path_to_app_dir>
Enter fullscreen mode Exit fullscreen mode

Running software

There are a couple of options when trying to run a program. One of the options is to run an Ansible shell command. This runs the program in a shell, the same way you would do it from the command line of the device. However many programs have their own modules, which are usually easier to read for humans in the YAML files. If a program needs to keep running after the Ansible configuration, the forever npm package can be used to prevent the program from stopping after the configuration. Other options do exist to keep software running.

- name: start PiCalc
    shell: forever start ./PiCalc/server.js
Enter fullscreen mode Exit fullscreen mode

Top comments (0)