In Ansible, can I re-use a role like a subroutine? - ansible

Ansible v2.11
I install a lot of applications on a Windows target, and most of them follow the same routine - download, unzip, for example.
- name: Download the zip file
win_url:
url_username: user
url_password: password
url: "{{ some_url }}"
dest: "C:\\temp\\{{ some_archive }}"
- name: Unzip the archive
win_unzip:
src: "C:\\temp\\{{ some_archive }}"
dest: "{{ archive_install_dir }}"
Is it possible to re-use the above role in "other roles" where I just pass in the some_archive and archive_install_dir variables for example? I set up my roles so each one has its own Git repository.

You can use include_role or import_role to pull it in.

Related

It's possible to parse template file on ansible role having the role itself as destination

I'm trying to parse a template file with Ansible, but I don't want this file to be created in any of my remote hosts, but instead I just want to create in my role_path.
I have the following in my role.
---
- name: Create configuration.yml on ansible role
ansible.builtin.template:
src: configuration.j2
dest: "{{role_path | default('')}}{{stack_name | mandatory}}/configuration.yml"
vars:
stack_env: "dev"
app_network: "my_network"
- name: Run tasks/main.yml from compose role
ansible.builtin.include_role:
name: compose
vars:
stack_name: "logging"
stack_path: "{{ ansible_base_path }}/"
When I run, my pipeline says that the directory doesn't exist, which is correct, because this directory exists outside my host, and not inside.
I basically want to parse this template file into my role, to be used by another role dependency.
Anyone knows if this is possible?
I found by myself the solution. It's possible to make use of local_action.
This is how my final playbook looks like.
- name: Create configuration.yml parsing variables
local_action:
module: template
src: configuration.j2
dest: "{{ role_path }}/logging/configuration.yml"
- name: Run tasks/main.yml from compose role
ansible.builtin.include_role:
name: compose
vars:
stack_name: "logging"
stack_path: "{{ ansible_base_path }}/"

Using Ansible to decrypt remote files?

Maybe it's a silly question but...
I would like to have an Ansible task that simply decrypts some remote files that have been encrypted with ansible-vault
I had in mind to use the copy module with the option decrypt: yes but it is not working for me because remote_src: yes don't support decrypt: yes
Autodecryption of files does not work when remote_src=yes.
https://docs.ansible.com/ansible/2.9/modules/copy_module.html
Is there any other way to achieve this?
- name: Decrypting secrets
become: true
copy:
src: "{{ path }}"
dest: "{{ path }}"
owner: "{{ user }}"
group: "{{ user }}"
decrypt: yes
remote_src: yes
Ansible version --> 2.9

Is there a way to take a .zip file, copy it to a directory on a Windows target and extract the .zip in the directory using Ansible?

I am trying to take a .zip file and copy it to a directory and extract the .zip file afterwards on a Windows target using an Ansible playbook. I was wondering if there's a module or task that can be written to do this nice and clean.
The .zip file itself is stored on another server so the playbook task would be pulling from this location to get the file to copy to the target.
I use these two modules to accomplish that task. win_copy: could be used if the file is on your ansible control host.
- name: download zlib source
win_get_url:
url: "{{ zlib_url }}"
dest: "{{ boost_workdir }}"
register: download_zlib
until: download_zlib is succeeded
retries: 10
delay: 2
- name: unzip zlib source
win_unzip:
src: "{{ download_zlib.dest }}"
dest: "{{ boost_prefix }}"
delete_archive: true
creates: "{{ boost_prefix }}/zlib-{{ zlib_version }}/contrib"
copy: false

How to override some specific variables values in Ansible playbook

I have some multiple war files which I am downloading from nexus and creating the respective app folder on my remote host and copying the files.
Now there are 2 files which have a different name as war file but need to create a directory with different name. What I am doing right now is this -
- name: Ensuring the web deployment folder is created on tomcat nodes for each web application
file:
path: "{{tomcat_server_release_location}}/Release{{my_release_version}}/{{item}}"
state: directory
mode: 0777
with_items:
- "{{ apps }}"
- name: Copying the release from Admin server to tomcat nodes
copy: src={{admin_server_release_location}}/{{my_release_version}}/{{item}}-{{my_release_version}}.war dest={{tomcat_server_release_location}}/Release{{my_release_version}}/{{item}}
with_items:
- "{{ apps }}"
apps variable is defined like this -
webapps: ['test1','test2','test3','test4'].
Now test2 has a different name on the nexus it says test2-web.war but on the remote node I have to create a folder called just test2 and copy the war file in there.
Is there a way to override some variables at run time of playbook by using some condition or anything
If you want to override at runtime use extra vars.
From the docs --extra-vars "version=1.23.45"
Don't use conditions if you know a specific host should always be overwritten. I think placing it as a host var is best.
hosts:
myhost:
apps:
- test1
- trst2
See https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable for all of the various locations you can put variables and how to determine which will be used
It is possible to override variables by doing something like this:
- name: copy war
copy:
src: "{{ src_war | default('/path/to/my/war') }}"
dest: "{{ dest_war | default(src_war) }}"
Then at runtime you can set the variables src_war and dest_war either with extra vars, host/group_vars or any other way documented here: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html
In your case I think it would be helpful to set a src & dest mapping:
- set_fact:
apps:
- {src: "test1"}
- {src: "test2", dest: "test2-web"}
- {src: "test3"}
- {src: "test4"}
Then you can use this mapping to achieve your goal:
- name: Ensuring the web deployment...
file:
path: {{ tomcat_server_release_location }}/Release{{ my_release_version }}/{{ item.src }}
state: directory
mode: 0777
with_items: "{{ apps }}"
- name: Copying the release...
copy:
src: "{{ admin_server_release_location }}/{{ my_release_version }}/{{ item.src }}-{{ my_release_version }}.war"
dest: "{{ tomcat_server_release_location }}/Release{{ my_release_version }}/{{ item.dest | default(item.src) }}"
with_items: "{{ apps }}"

Is it possible to reuse ansible tasks with some parameters override?

I want to create playbook to download only any package installed by ansible.
There is a lot of yum state=installed tasks in my ansible roles. Is there a way to include that roles without state=installed, but with download_only and download_dir instead?
ansible 2.8
No. It is not possible. Use variables. For example
- yum:
state: "{{ my_role1_yum_state|default(omit) }}"
download_only: "{{ my_role1_yum_download_only|default(omit) }}"
download_dir: "{{ my_role1_yum_download_dir|default(omit) }}"
...

Resources