Unable to create a directory using Ansible - 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

Related

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

How do i print statements using debug in Ansible

The file main.yaml inside a role is below where I want to print some message like "Directory created/existed". When I use something like below I get the error -
ERROR: multiple actions specified in task: 'file' and 'Ensuring that directory exists to download the artifacts'
---
- name: Ensuring that directory exists to download the artifacts
debug:
msg: "Directory Created/Existed"
file:
path: "{{ local_server_release_location }}"
state: directory
It's not possible to use both debug and file modules in one task.
Instead, given the directory shall be present at localhost, create the directory and test its existence. For example
- name: Create directory
file:
path: "{{ local_server_release_location }}"
state: directory
delegate_to: localhost
- name: Ensuring that directory exists to download the artifacts
debug:
msg: "Directory exists"
when: local_server_release_location is directory

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

Setting up symlink error - 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:
...

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