"Unsupported parameters for (file) module: creates" - ansible

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.

Related

Unable to create a directory using Ansible

I have tried something like this
- name: Create a directory
ansible.builtin.file:
path: /etc/fail2ban
state: directory
mode: '0755'
and I am a getting a error
fatal: [localhost]: FAILED! => {"changed": false, "msg": "There was an issue creating /etc/fail2ban as requested: [Errno 13] Permission denied: b'/etc/fail2ban'", "path": "/etc/fail2ban"}
I am trying to create a directory on a remote server.
Need Help !!!
Thanks in advance.
Have you already tested the execution with become: yes?
- name: Create a directory
ansible.builtin.file:
path: /etc/fail2ban
state: directory
mode: '0755'
become: yes
See the Ansible docs for more information on become and privilege escalation.
Otherwise the output of stat might help you to understand what is going on. You could add the following two tasks before your file task:
- name: Get file stat
stat:
path: /etc/fail2ban
register: stat_result
- name: Print file stat
debug:
var: stat_result

ansible to copy file and folders if md5 and size of file differs

I want to copy files and subfolders from a directory I wrote the following yml but its throwing error on my mac
---
- hosts: source_server
gather_facts: false
tasks:
- name: copy file from source server to local
fetch:
src: /home/user/Test/
dest: /Users/user/Log/rules/
run_once: true
- hosts: dest_servers
tasks:
- name: copy files
copy:
src: /Users/user/Log/rules/
dest: /home/user/Test/
remote_src: yes
directory_mode: yes
[Errno 21] Is a directory: '/home/user/Test/'\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
Any idea what I am doing wrong?

Ansible win_file can not create a directory on a partitioned drive?

Ansible 2.11.0
I created anI: disk partition on my Windows 2019 host under jenkins user, who is an admin. Logged in as jenkins, I can create a directory on the partition.
I have this Ansible task that simply tries to create the same directory structure, like this
- name: "Create data directory {{ pg_data_dir }}"
win_file:
path: "{{ pg_data_dir }}" # I:\pgdata\13
status: directory
I get ...
TASK [postgresql : Create data directory I:\pgdata\13] ********************************
task path: /path/ansible/exa-playbooks/roles/postgresql/tasks/install_postgresql.yml:21
redirecting (type: modules) ansible.builtin.win_file to ansible.windows.win_file
Using module file /usr/local/Cellar/ansible/3.3.0/libexec/lib/python3.9/site-packages/ansible_collections/ansible/windows/plugins/modules/win_file.ps1
Pipelining is enabled.
<10.227.xx.xx> ESTABLISH WINRM CONNECTION FOR USER: jenkins on PORT 5986 TO 10.227.xx.xx
EXEC (via pipeline wrapper)
fatal: [10.227.xx.xx]: FAILED! => {
"changed": false,
"msg": "path I:\\pgdata\\13 will not be created"
}
Any clues?
UPDATE
For now, I just changed my task to this, but the above issue persists.
- name: "Create data directory {{ pg_data_dir }}"
win_shell:
mkdir "{{ pg_data_dir }}" # I:\pgdata\13
Duh on me. It should be state, not status, like this
- name: "Create data directory {{ pg_data_dir }}"
win_file:
path: "{{ pg_data_dir }}" # I:\pgdata\13
state: directory

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

Using Ansible Playbook how to copy Java certs to hosts?

Using Ansible Playbook how to copy Java certs to hosts? Each host is having different JDK installed. I need to verify in all hosts which JDK is running and copy those certificate to all the hosts.
I have written the below playbook and the error that I'm getting. Please help me with figuring out what's wrong.
---
- hosts: test
vars:
pack1: /ngs/app/rdrt
pack2: /usr/java/jdk*
tasks:
- name: copy the files
copy:
src: "/Users/sivarami.rc/Downloads/Problem46218229/apple_corporate_root_ca.pem"
dest: "{{ pack1 }}"
- name: copy the files
copy:
src: "/Users/sivarami.rc/Downloads/Problem46218229/apple_corporate_root_ca2.pem"
dest: "{{ pack1 }}"
- name: copy the files
copy:
src: "/Users/sivarami.rc/Downloads/Problem46218229/ca-trust-check-1.0.0.jar"
dest: "{{ pack1 }}"
- name: Import SSL certificate to a given cacerts keystore
java_cert:
cert_path: "{{ pack1 }}/apple_corporate_root_ca.pem"
cert_alias: Apple_Corporate_Root_CA
cert_port: 443
keystore_path: "{{ pack2 }}/jre/lib/security/cacerts"
keystore_pass: change-it
executable: "{{ pack2 }}/bin/keytool"
state: present
- name: Import SSL certificate to a cacerts keystore
java_cert:
cert_path: "{{ pack1 }}/apple_corporate_root_ca2.pem"
cert_alias: Apple_Corporate_Root_CA2
cert_port: 443
keystore_path: "{{ pack2 }}/jre/lib/security/cacerts"
keystore_pass: changeit
executable: "{{ pack2 }}/bin/keytool"
state: present
- name: checking those files trusted or untrusted
shell: "{{ pack2 }}/bin/java -jar {{ pack1 }}/ca-trust-check-1.0.0.jar"
The error:
fatal: [c5147061#rn2-radart-lapp117.rno.apple.com]: FAILED! => {"changed": false, "cmd": "'/usr/java/jdk*/bin/keytool'", "msg": "[Errno 2] No such file or directory", "rc": 2}
fatal: [c5147061#rn2-radart-lapp121.rno.apple.com]: FAILED! => {"changed": false, "cmd": "'/usr/java/jdk*/bin/keytool'", "msg": "[Errno 2] No such file or directory", "rc": 2}
The following error is displayed:
"cmd": "'/usr/java/jdk*/bin/keytool'", "msg": "[Errno 2] No such file or directory"
As you can see, the keytool command can not be found in that location. You need to ensure that the path you're providing is actually there on the server.
Where you define the pack2 variable, you need to provide the full path instead of using a wildcard, e.g. like this:
vars:
pack2: /usr/java/jdk-1.8.0_67
Then ensure that this path exists on the remote machine, and your code should no longer show that error.
If the path is different on each node since you have a different version of Java on each node, here are some options:
Use host-specific variables for defining the path for each host, if you have that information.
Gather the information in a previous step, e.g. like here: Check Java version via Ansible playbook.
Check the JAVA_HOME environment variable to see if that is set.
I had the same error that the keytool utility was not found (on my PATH), but that was because I did not use the become_user which has the correct PATH value.
So my solution was to add the following line to my playbook:
become: yes
become_user: wls
(wls is the weblogic user but can be another system account depending on your needs)
I had the same error because keytool was link to a really old version of the JDK (version 6).
By using a more recent version (JDK version 11), I fixed this error.

Resources