"[Errno 13] Permission denied error" while running Py script in Ansible - ansible

When, I'm running a Python script via an Ansible playbook, I get the following error:
fatal: [localhost]: FAILED! => {"changed": false, "cmd": "/Dest/To/Repo/HW.py", "failed": true, "msg": "[Errno 13] Permission denied", "rc": 13}
I did add the sudo: yes line:
Here is my yaml file:
- name: a play that runs entirely on the ansible host
hosts: 127.0.0.1
sudo: yes
connection: local
tasks:
- name: check out a git repository
git: repo={{ repo_url }} dest=/Dest/To/Repo/ accept_hostkey=yes
vars:
repo_url: https://github.com/lorin/mezzanine-example.git
- name: Running the Python Script
command: /Dest/To/Repo/HW.py
The HW.py script is just print("Hello World")
Is there anything which I need to do, regarding the permissions error?

You need to use umask to add execution rights in you git module call :
- name: a play that runs entirely on the ansible host
hosts: 127.0.0.1
sudo: yes
connection: local
tasks:
- name: check out a git repository
git: repo={{ repo_url }} dest=/Dest/To/Repo/ accept_hostkey=yes
umask: 0022
vars:
repo_url: https://github.com/lorin/mezzanine-example.git

Related

Trying to git clone the repository using ssh in ansible-playbook got error occurred

While trying to git clone the repository using ssh in ansible-playbook, the below-mentioned error occurred. Could you please suggest a solution to this issue?
Error details:
FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "cmd": ["/bin/git", "fetch", "origin"], "msg": "Failed to download remote objects and refs: fatal: cannot exec '/tmp/tmpitfHF0': Permission denied\nfatal: unable to fork\n"}
An error occurred in Linux machine
Code:
hosts: App-1
become: yes
become_method: sudo
become_user: admin
vars:
git_uri: ssh://git#gitURLlink
tasks:
- name: App deploy
git: repo={{ git_uri }} dest=/etc/Repo update=yes force=yes track_submodules=yes

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

Ansible delegate_to "Incorrect sudo password"

With ansible i want to configure rsyslog service for a group of hosts than add the name those hosts to a central host (different from the group of hosts) so my playbook:
- hosts: gourp_of_hosts
tasks:
- name: set rsyslog configuration
lineinfile:
path: /etc/rsyslog.conf
line: '{{item}}'
with_items:
- some items....
become: yes
- name: add host to rsyslog central
blockinfile:
path: /etc/rsyslog.conf
block: |
{{ansible_hostname}}....
delegate_to: x.x.x.x (my central host)
become: yes
My inventory file contains both the group of hosts and my central host:
[gourp_of_hosts]
host1 ansible_user=.... ansible_user_pass=.. ansible_sudo_pass=..
host2 ansible_user=.... ansible_user_pass=.. ansible_sudo_pass=..
[central]
x.x.x.x ansible_user=... ansible_user_pass=.. ansible_sudo_pass=..
Now i'm facing the following error:
fatal: [host1]: FAILED! => {"msg": "Incorrect sudo password"}
I tried removing central host from my inventory file and used ssh-copy-id user#x.x.x.x in result i'm having the following error:
fatal: [host]: UNREACHABLE! => {"changed": false, "msg": "Failed
to connect to the host via ssh: Permission denied
(publickey,gssapi-with-mic,password).\r\n", "unreachable": true}

Why is Ansible not Running Pip as sudo user?

I have a playbook that ensures all requirements are installed locally. I am using ansible 2.0.0
ansible-playbook site.yml -i staging
site.yml:
---
- hosts: localhost
become: yes
become_user: "{{ sudo_user }}"
connection: local
vars_files:
- vars/main.yml
roles:
- do
sudo_user is surfer190 in vars.
do/tasks/main.yml:
- name: make sure everything is installed
apt: name={{item}} state=installed
with_items:
- python-apt
- python-pycurl
- python-pip
- python-setuptools
- name: Install dopy
pip: name={{ item }}
with_items:
- virtualenv
- dopy
- passlib
I get the following error:
failed: [localhost] => (item=passlib) => {"cmd": "/usr/local/bin/pip install passlib", "failed": true, "invocation": {"module_args": {"name": "passlib"}, "module_name": "pip"}, "item": "passlib", "msg": "stdout: Collecting passlib\n Using cached passlib-1.6.5-py2.py3-none-any.whl\nInstalling collected packages: passlib\n\n:stderr: Exception:\nTraceback (most recent call last):\n File \"/usr/local/lib/python2.7/dist-packages/pip/basecommand.py\", line 211, in main\n status = self.run(options, args)\n File \"/usr/local/lib/python2.7/dist-packages/pip/commands/install.py\", line 311, in run\n root=options.root_path,\n File \"/usr/local/lib/python2.7/dist-packages/pip/req/req_set.py\", line 646, in install\n **kwargs\n File \"/usr/local/lib/python2.7/dist-packages/pip/req/req_install.py\", line 803, in install\n self.move_wheel_files(self.source_dir, root=root)\n File \"/usr/local/lib/python2.7/dist-packages/pip/req/req_install.py\", line 998, in move_wheel_files\n isolated=self.isolated,\n File \"/usr/local/lib/python2.7/dist-packages/pip/wheel.py\", line 339, in move_wheel_files\n clobber(source, lib_dir, True)\n File \"/usr/local/lib/python2.7/dist-packages/pip/wheel.py\", line 310, in clobber\n ensure_dir(destdir)\n File \"/usr/local/lib/python2.7/dist-packages/pip/utils/__init__.py\", line 71, in ensure_dir\n os.makedirs(path)\n File \"/usr/lib/python2.7/os.py\", line 157, in makedirs\n mkdir(name, mode)\nOSError: [Errno 13] Permission denied: '/usr/local/lib/python2.7/dist-packages/passlib'\n"}
Which is basically a permission denied. So it is not running the command with sudo. As I try sudo pip install passlib and it works.
Even if I run with -k and enter the sudo password it does not work. Take note that the surfer190 user requires a password to sudo.
What am I doing wrong?
Add 'sudo: yes' to Install dopy task
- name: make sure everything is installed
apt: name={{item}} state=installed
with_items:
- python-apt
- python-pycurl
- python-pip
- python-setuptools
- name: Install dopy
pip: name={{ item }}
with_items:
- virtualenv
- dopy
- passlib
sudo: yes
If you notice that the execution of the playbook hangs while executing the task then it is likely that ansible waits for the sudo passoword but there is no way you can enter the password. There is a way to get around that. Update the below specified section in your sudoers file located at '/etc/sudoers'
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
yourusername ALL=NOPASSWD: ALL
Be careful while editing /etc/sudoers, having the incorrect entries might prevent you from logging on to the server ever again.
So it looks like the minimum config you need is:
---
- hosts: localhost
become: yes
become_user: "{{ sudo_user }}"
connection: local
vars_files:
- vars/main.yml
roles:
- ...
So the become_user needs to be a user that can sudo with no pass I think.
If there is no become_user it fails with:
failed: [localhost] => (item=passlib) => {"failed": true, "item": "passlib", "parsed": false}
[sudo via ansible, key=sgidbrejgqoibeozxncyhixnwsukxjbe] password:

Ansible error message

I'm trying to use ansible to build a docker image locally but I'm
running into problems.
- hosts: all
tasks:
- name: Build Docker image
local_action:
module: docker_image
path: .
name: SlothSloth
state: present
And my /etc/ansible/hosts contains
localhost ansible_connection=local
But when I try to run it I get:
TASK: [Build Docker image] ****************************************************
failed: [localhost -> 127.0.0.1] => {"failed": true, "parsed": false}
failed=True msg='failed to import python module: No module named docker.client'
FATAL: all hosts have already failed -- aborting
If you are using virtualenv, you are probably running ansible with /usr/bin/python by default. To bypass this behavior, you have to define the variable "ansible_python_interpreter".
Try to use :
- hosts: all
vars:
- ansible_python_interpreter: python
tasks:
- name: Build Docker image
local_action:
module: docker_image
path: .
name: SlothSloth
state: present

Resources