Setting up symlink error - Ansible - ansible

I am trying to set a symlink from source to destination, but keep hitting
fatal: [default]: FAILED! => {"changed": false, "failed": true, "msg": "Error while linking: [Errno 2] No such file or directory", "path": "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/ruby/config.h", "state": "absent"}
Observed that it shows state absent even though state is given as link.
This is the executed task:
- name: "Create ruby config.h symlink"
file:
src: "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/universal-darwin15/ruby/config.h"
dest: "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/ruby/config.h"
state: link
force: yes
when: xcode_version != "8.0"
File exists:
ls -l /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/universal-darwin15/ruby/config.h
-rw-r--r-- 2 root wheel 7805 Jan 31 2016 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/universal-darwin15/ruby/config.h
Note: Included force yes based on discussion here - Ansbible github issue
Any help is appreciated!

My answer may not address the OP question, but it certainly does cause this issue and so might explain another person running into this issue. I've often found such an error can be caused by a trailing / in the link name.
For example, a task to create a directory as below will succeed.
- name: ensure foobar directory exists
file:
state: directory
path: '/tmp/foobar/'
owner: 'foo'
group: 'bar'
mode: 0777
However, the subsequent link creation task, if written as below, will result in the error you're experiencing.
- name: Establish link to foobar
file:
src: '/tmp/foobar/'
dest: '/local/baz/'
state: link
Results in the below error.
fatal: [default]: FAILED! => {"changed": false, "failed": true, "msg":
"Error while linking: [Errno 2] No such file or directory", "path":
"...",
"state": "absent"}
Removing the / as per below, can resolve this.
- name: Establish link to foobar
file:
src: '/tmp/foobar/'
dest: '/local/baz'
state: link

I'm not sure if a missing directory is the cause of the problem here but it's a good practice to ensure that the destination directory exists before creating a link that points to that directory:
- name: Ensure that the directory exist
file:
path: "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/include/ruby-2.0.0/ruby"
state: directory
mode: 0755
owner: root
group: wheel
- name: "Create ruby config.h symlink"
file:
...

Related

Destination /home/developer/.conan/conan.conf does not exist

I am building ansible-core project, but due to following code snipped getting error
# This was necessary to prevent errors when upgading conan from an earlier to a newer version, as the accepted file format changed.
- name: Make conan storage path absolute
lineinfile:
path: '{{ HOME }}/.conan/conan.conf'
regexp: '^path ='
line: path = ./data
Error-
TASK [Make conan storage path absolute] ****************************************
fatal: [localhost]: FAILED! => {"changed": false, "msg": "Destination /home/developer/.conan/conan.conf does not exist !", "rc": 257}
I tried commenting that part and was able to build but not sure whether to remove that code snippet
I have resolved issue by changing path from
'{{ HOME }}' to '~'
# This was necessary to prevent errors when upgading conan from an earlier to a
newer version, as the accepted file format changed.
- name: Make conan storage path absolute
lineinfile:
path: '{{ HOME }}/.conan/conan.conf'
regexp: '^path ='
line: path = ./data

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: How to change $HOME directory

I am running ansible 2.9.6 in my control machine Ubuntu 18.04 Desktop
to control single server Ubuntu 16.04 server which doesn't have /home/username/ directory.
I don't intend to create one aswell.
I am just trying to create a new folder "/usr/local/src/fromcontrolmachine" in slave machine from control machine
So I ran below command
dinesh#dinesh-VirtualBox:/etc/ansible$ ansible all -u dira
--become -m file -a "dest=/usr/local/src/fromcontrolmachine mode=755 owner=dira group=dira state=directory" -K
BECOME password:
> 10.211.108.44 | FAILED! => {
> "changed": false,
> "module_stderr": "Shared connection to 10.211.108.44 closed.\r\n",
> "module_stdout": "Could not chdir to home directory /home/dira: No such file or directory\r\n\r\n",
> "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
> "rc": 1 }
I thought of changing the $Home directory by adding below line in
/etc/ansible/ansible.cfg. It just created an empty folder called ansible
remote_tmp = usr/local/src/ansible
How to tell ansible to change the default $Home directory by pointing to other location than default /home/dira ?
I wanted to clear this annoying error
"module_stdout": "Could not chdir to home directory /home/dira271641: No such file or directory
UPDATE:
Also tried creating playbook pb.yml & add home_dir: /usr/local/src/ansible as mentioned below.
---
- hosts: all
become: true
tasks:
- set_fact:
home_dir: /usr/local/src/ansible
become: true
- name: ansible create directory example
file:
path: /tmp/devops_directory
state: directory
When i run above using command ansible-playbook pb.yml -K
But it gives the same error as mentioned above.
UPDATE:
I tried environment: HOME:
---
- hosts: all
become: true
environment:
HOME: /usr/local/src/ansible
tasks:
- name: ansible create directory example
file:
path: /tmp/devops_directory
state: directory
Throws same error
Could not chdir to home directory /home/dira: No such file or directory\r\n\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}}, "msg": "The following modules failed to execute: setup\n"}
Adding below line.
become_user: dira
solved this problem. Note: dira is my username. So replace your username instead.
So full playbook script will look like
---
- hosts: all
become: true
become_user: dira
environment:
HOME: /usr/local/src/ansible
tasks:
- name: ansible create directory example
file:
path: /tmp/devops_directory
state: directory

Fetching file from remote server to local using Ansible script

Ansible automated script:
tasks:
- name: copying catalina.out to /tmp/jagthish location
fetch:
src:
- yes
- /usr/tomcat/tomcat8/logs/catalina.out
dest: /tmp/jagthish/
error message:
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: AttributeError: 'list' object has no attribute 'startswith'
fatal: [ip]: FAILED! => {"failed": true, "msg": "Unexpected failure during module execution.", "stdout": ""}
I tried to copy a file (catalina.out) from remote server to my local server. it shows above error.
You can't provide a list to the src argument of fetch module. It expects a path to a file in a string.
You seem to want this:
- name: copying catalina.out to /tmp/jagthish location
fetch:
src: /usr/tomcat/tomcat8/logs/catalina.out
dest: /tmp/jagthish/
flat: yes

Permission denied when deleting directory in Ansible

I have a task which simply creates directory in the home directory of user 'foo' as follows:
---
- name: Create bar directory
become: yes
become_user: foo
file:
path: "/home/foo/bar"
state: directory
owner: foo
group: foo
mode: 755
This works fine, but when I try to run a task to delete the directory created by above task, I got the following error:
TASK: [a task | Create bar directory]
failed: [192.168.50.4] => {"failed": true}
msg: rmtree failed: [Errno 13] Permission denied: '/home/foo/bar'
FATAL: all hosts have already failed -- aborting
Below is the delete task.
---
- name: Create bar directory
become: yes
become_user: foo
file:
path: "/home/foo/bar"
state: absent
I confirmed the created directory is owned by 'foo' so the directory should be able to be deleted by 'foo'. Why am I getting permission denied error?
Although the directory is owned by the correct user, I realized that the permission for the directory was not correctly set, i.e, drwxr-xr-x.
The problem was mode: 755, which does not seem to be a problem at all. But I needed to prepend 0 before 755 in order for it to work as expected.
Example in official document

Resources