Ansible get_url not registering return values - syntax

I am using Ansible 2.8.1 to download from Nexus.
I want to register a variable so that in subsequent tasks, I will know what file I downloaded by looking at downloaded_file.dest.
- name: Download assembly file to /my/server/location/
get_url:
url: https://nexus.mycompany.com/service/rest/v1/search/assets/download?repository=repo-snapshots&group=group&name=name&sort=version&direction=desc
validate_certs: no
dest: /my/server/location/
force: yes
register: downloaded_file
But when running ansible-playbook 2.8.1
I get
fatal: [myserver]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (get_url) module: register Supported parameters include: attributes, backup, checksum, client_cert, client_key, content, delimiter, dest, directory_mode, follow, force, force_basic_auth, group, headers, http_agent, mode, owner, regexp, remote_src, selevel, serole, setype, seuser, sha256sum, src, timeout, tmp_dest, unsafe_writes, url, url_password, url_username, use_proxy, validate_certs"}
So it's considering the register as a parameter to get_url. Is my syntax correct here? How do I get the return values from the task?

The syntax is wrong. register is not a parameter of get_url. It's a task's level directive.
Correct
- name: Download assembly file to /my/server/location/
get_url:
url: https://nexus.mycompany.com/service/rest/v1/search/assets/download?repository=repo-snapshots&group=group&name=name&sort=version&direction=desc
validate_certs: no
dest: /my/server/location/
force: yes
register: downloaded_file

Related

Ansible: unsupported parameters for lineinfile module

I really don't see the error in the following ansible task:
- name: Ensure home directories are created upon login
lineinfile:
path: /etc/pam.d/common-session
search_string: 'pam_mkhomedir\.so'
insertafter: 'pam_sss.so'
line: 'session required pam_mkhomedir.so skel=/etc/skel/ umask=0022'
tags:
- ldap
- pam
- config
When executed I get the following error:
fatal: [ourcq]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (lineinfile) module: search_string Supported parameters include: attributes, backrefs, backup, create, firstmatch, group, insertafter, insertbefore, line, mode, owner, path, regexp, selevel, serole, setype, seuser, state, unsafe_writes, validate"}
I found a solution that does what I want:
add the line line after the line containing pam_sss.so if the line does not already exists, meaning that the regex pam_mkhomedir is not already present.
- name: Ensure home directories are created upon login
lineinfile:
path: /etc/pam.d/common-session
regexp: 'pam_mkhomedir'
insertafter: 'pam_sss'
line: 'session required pam_mkhomedir.so skel=/etc/skel/ umask=0022'
tags:
- ldap
- pam
- config

Copy & fetch files in Ansible/Cygwin

Question - - how do you navigate the cygwin path structure for file transfers, copies and fetches?
I've installed ansible on a windows 10 machine using cygwin. Everthing works except for the ansible.builtin.copy task. Here is the setup
Relevant Directory Structure
C:.
├───.github
│ └───workflows
├───files
└───payload
├───communication
├───monitoring
The playbook sits in the documents directory of the user, so . is C:/Users/user/Documents/
Ansible Task
- name: Download YAML payloads
ansible.builtin.copy:
src: payload
dest: /some/directory/
The ansible cygwin command line actually runs from /cygdrive/c/Users... path. I can navigate to the payload directory from either windows cli or the cygwin cli using their native paths. [Must be a symlink?] In any event - when I run the above task, the src directory is not found.
What I've tried - both absolute and relative path variables in the src line, for both the cywgin and the windows paths. I've also tried using the inventory environment variables ({{ playbook_dir }}). fileglob: didn't work either.
What I haven't tried - {{ role_path }}. I'd like to keep the source YAMLs all together in the top directory. But not sure if this would work by putting the files directory under a role.
added details
Path to playbook from windows
C:\Users\billr\Documents\GitHub\home-k3s
Path to playbook from cygwin
/cygdrive/c/Users/billr/Documents/GitHub/home-k3
files & directories
home-k3s
files // these are the files/dirs I'm looking to copy
payload
communication
first.yaml
second.yaml
monitoring
first.yaml
second.yaml
hosts.ini //contains playbook hosts.
test.yml //this is the playbook I'm running
playbook cat
---
- hosts: master
gather_facts: yes
become: yes
tasks:
- name: Download YAML payloads
ansible.builtin.copy:
src: payload
dest: /home/bill/
Run #1
src: payload <-- this is the method per docs (for linux).
result: FAILED! => {"changed": false, "msg": "Source payload not found"}
Run #2
src: "{{ playbook_dir }}/files/payload"
result: FAILED! => {"changed": false, "msg": "Source /cygdrive/c/Users/billr/Documents/GitHub/home-k3s/files/payload not found"}
Run #3
src: "/cygdrive/c/Users/billr/Documents/GitHub/home-k3s/files/payload"
result: FAILED! => {"changed": false, "msg": "Source /cygdrive/c/Users/billr/Documents/GitHub/home-k3s/files/payload not found"}
Run #4
src: "c:/Users/billr/Documents/GitHub/home-k3s/files/payload"
FAILED! => {"changed": false, "msg": "Source c:/Users/billr/Documents/GitHub/home-k3s/files/payload not found"}
Note that I can see the files from the cygwin terminal with ls and I can see the files from the windows cli with dir.
Final Notes
Cygwin Github Issue Link

Ansible get_url module: Unable to find a checksum for file

I'm trying to fetch ActiveMQ Artemis using the following:
- name: Download the ActiveMQ Artemis artifact
get_url:
url: "https://www.apache.org/dyn/closer.cgi?filename=activemq/activemq-artemis/{{ artemis_version }}/apache-artemis-{{ artemis_version }}-bin.tar.gz&action=download"
dest: "/tmp/apache-artemis-{{ artemis_version }}-bin.tar.gz"
#with fixed checksumm it works but breaks the idea of the version to be a variable.
#checksum: "sha512:4990a6b742b08bff6a4c7b310d2610565b08a2a02e1a7aec065460d16f8a6fe3d4fe91a8040839f93d7c2eab09fd6a79848fb130f9820559ee3e81dcf8d51ead"
#Getting "Unable to find a checksum for file 'closer.cgi' in 'https://downloads.apache.org/activemq/activemq-artemis/2.16.0/apache-artemis-2.16.0-bin.tar.gz.sha512'"
checksum: "sha512:https://downloads.apache.org/activemq/activemq-artemis/{{ artemis_version }}/apache-artemis-{{ artemis_version }}-bin.tar.gz.sha512"
#Also getting: fatal: [dev-broker-01]: FAILED! => {"changed": false, "dest": "/tmp/apache-artemis-2.16.0-bin.tar.gz", "elapsed": 0, "msg": "Request failed: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:618)>", "url": "https://www.apache.org/dyn/closer.cgi?filename=activemq/activemq-artemis/2.16.0/apache-artemis-2.16.0-bin.tar.gz&action=download"}
validate_certs: no
and getting: "Unable to find a checksum for file 'closer.cgi' in 'https://downloads.apache.org/activemq/activemq-artemis/2.16.0/apache-artemis-2.16.0-bin.tar.gz.sha512'"
It's not picking up the filename from dest: "/tmp/apache-artemis-{{ artemis_version }}-bin.tar.gz"
Also having some issue validating the certificate.
Any ideas how can I solve both problems?
The error seems to suggest that it is looking for checksum of file closer.cgi rather than the actual tar.gz file. And the filename in the checksum URL is: apache-artemis-2.16.0-bin.tar.gz.
The other way to specify the checksum, is to just supply the checksum string (without filename). Although for that we need to come up with a couple of tasks prior to get it.
Something like below:
- uri:
url: "https://downloads.apache.org/activemq/activemq-artemis/{{ artemis_version }}/apache-artemis-{{ artemis_version }}-bin.tar.gz.sha512"
return_content: true
register: url_sha512
- set_fact:
artemis_checksum: "{{ url_sha512.content.split(' ')[0] }}" # there are 2 spaces
- get_url:
url: "https://www.apache.org/dyn/closer.cgi?filename=activemq/activemq-artemis/{{ artemis_version }}/apache-artemis-{{ artemis_version }}-bin.tar.gz&action=download"
dest: "/tmp/apache-artemis-{{ artemis_version }}-bin.tar.gz"
checksum: "sha512:{{ artemis_checksum }}"
# I was able to download without having below parameter
# validate_certs: no
Update:
This kind of approach can be useful when site directory cannot be browsed, and file must be obtained from a mirrored URL.

"Unsupported parameters for (file) module: creates"

I created a ansible playbook which creates a directory and then copy files to that directory. Then to provide idempotency i used args and creates, but it is givig me below error:
FAILED! => {"changed": false, "checksum": "691713a12a3c088b216c14832a0c2682c88d205d", "failed": true, "msg": "Unsupported parameters for (file) module: creates. Supported parameters include: attributes,backup,content,delimiter,diff_peek,directory_mode,follow,force,group,mode,original_basename,owner,path,recurse,regexp,remote_src,selevel,serole,setype,seuser,src,state,unsafe_writes,validate"}
I checked the documentation for File and it does not tell anything about args. Can someone please suggest what need to be done to provide idempotency for directory creation and copying files ?? Below is my code:
hosts: all
become: yes
become_method: sudo
vars_files:
roles/bootstrap/vars/main.yml
tasks:
name : Creating Directory /opt/app/{{ appversion }}
file: path=/opt/app/{{ appversion }} state=directory
args:
creates: "/opt/app/{{ appversion }}"
name: Copying the Upgradce Build
copy:
src: "/tmp/app-{{ appversion }}.tar.gz"
dest: /opt/app/{{ appversion }}
args:
creates: "/opt/app/{{ appversion }}/app-{{ appversion }}.tar.gz"
Idempotency is already built into the file and copy modules. You do not need to add anything to the play.

Ansible unarchive: doesn't reach a remote host

I use Ansible as a provisioner for Vagrant. I have a task:
- name: download and unarchive redis binaries
unarchive:
src: "http://download.redis.io/redis-stable.tar.gz"
dest: "/tmp"
remote_src: True
but for some reasons I see an error in console when I run a vagrant provision:
"failed": true, "msg": "file or module does not exist: /Users/my-username/Projects/project-name/http:/download.redis.io/redis-stable.tar.gz"`
> ansible --version
ansible 2.1.2.0
Any ideas?
NB: look carefully for the error http:/download. Why is there only one backslash?
The syntax from your question works with Ansible 2.2.0.0 and later.
For Ansible 2.0 and 2.1 use:
- name: download and unarchive redis binaries
unarchive:
src: "http://download.redis.io/redis-stable.tar.gz"
dest: "/tmp"
copy: false
The double slash from your question was stripped, because the argument src was treated as a path to a local file (again, because old versions of Ansible required copy: false in addition to the URL).

Resources