Fatal error at provisioning while installing python - python

I have this playbook:
---
- hosts: all
become: yes
become_user: root
gather_facts: no
pre_tasks:
- name: 'install python2'
raw: sudo apt-get -y install python-simplejson
roles:
- git
...
Sometimes, it works perfectly fine and sometimes it gives me this error:
==> default: Running provisioner: ansible...
default: Running ansible-playbook...
PYTHONUNBUFFERED=1 ANSIBLE_FORCE_COLOR=true ANSIBLE_HOST_KEY_CHECKING=false ANSIBLE_SSH_ARGS='-o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -i '/var/www/test/.vagrant/machines/default/virtualbox/private_key' -o ForwardAgent=yes -o ControlMaster=auto -o ControlPersist=60s' ansible-playbook --connection=ssh --timeout=30 --extra-vars="ansible_ssh_user='ubuntu'" --limit="all" --inventory-file=ansible/hosts/hosts_linux.txt -v ansible/site.yml
Using /etc/ansible/ansible.cfg as config file
PLAY ***************************************************************************
TASK [install python2] *********************************************************
fatal: [10.0.11.44]: FAILED! => {"changed": false, "failed": true, "rc": 255, "stderr": "", "stdout": "", "stdout_lines": []}
PLAY RECAP *********************************************************************
10.0.11.44 : ok=0 changed=0 unreachable=0 failed=1
Ansible failed to complete successfully. Any error output should be
visible above. Please fix these errors and try again.
I am wondering why do I get this error and what does it mean? There is no output on the error and the exactly same code works if I try this many times. It makes no sense. Why is ansible failing to install python?

According to your comments, the problem is with ssh. You can create your own ansible.cfg file in your current working directory and supply openssh options directly as described here: open-ssh options
I suggest you try:
[defaults]
host_key_checking = False
And also during your playbook run, you can manually specify which ssh key to use like:
ansible-playbook --private-key /path/to/ssh_key ...
Finally, make sure there is no IP conflict, because your server's ssh fingerprint should not change. Try manually ssh'ing a few times as well.

Related

Ansible molecule test framework fails to gather facts from docker image of centos7

I am trying to test a simple ansible script I have made using molecule. I am currently setting up molecule and have been following this tutorial: https://www.youtube.com/watch?v=93urFkaJQ44
When I run molecule test I get this error (this is verbose, -vvv output):
TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
fatal: [instance]: UNREACHABLE! => {"changed": false, "msg": "Failed to create temporary directory.In some cases, you may have been able to authenticate and did not have permissions on the target directory. Consider changing the remote tmp path in ansible.cfg to a path rooted in \"/tmp\", for more error information use -vvv. Failed command was: ( umask 77 && mkdir -p \"` echo ~/.ansible/tmp `\"&& mkdir \"` echo ~/.ansible/tmp/ansible-tmp-1631621484.12-47224-231505895998260 `\" && echo ansible-tmp-1631621484.12-47224-231505895998260=\"` echo ~/.ansible/tmp/ansible-tmp-1631621484.12-47224-231505895998260 `\" ), exited with result 1", "unreachable": true}
It says that this error is caused by the converge script, as it errors in the PLAY Converge section.
Converge.yml
---
- name: Converge
hosts: all
gather_facts: true
- import_playbook: ../../setUpVm.yml
/.ansible exists as it returns:
collections cp galaxy_token tmp
Any help appreciated.
Update Setting gather_facts: no makes the test run to completion. This "fix" however is not idea.
Your docker engine has restarted. Do a simple
molecule destroy && molecule create
will permit you to successfully run your tests.
Another clue may be a filesystem problem. It often occurs within multiple operation systems, like running a docker inside a WSL2 virtualized kernel on Windows. Try to change your molecule against checked source tree to a native linux path.

Error in executing ansible playbook on target server without python

I have installed ansible on my windows machine using Cygwin. Executing the playbook for various target servers of Linux, AIX, Oracle is working completely fine.But when I am executing the playbook for VIOS server, it is giving below error. The problem is only with the execution for VIOS server because python is not present there and it is not possible for me to make any change on the target VIOS server.
Tried passing in vars ansible_python_interpreter: /usr/bin/python or ansible_python_interpreter: /usr/bin/python3, but still error persists.
Please can anybody help me with the solution.
PLAY [playbook for vios servers] ************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************
fatal: [vios01_cont]: FAILED! => {"ansible_facts": {}, "changed": false, "failed_modules": {"setup": {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "failed": true, "module_stderr": "Connection to 10.10.64.25 closed.\r\n", "module_stdout": "/bin/sh: /usr/bin/python: not found.\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 127, "warnings": ["No python interpreters found for host vios01_cont (tried ['/usr/bin/python', 'python3.7', 'python3.6', 'python3.5', 'python2.7', 'python2.6', '/usr/libexec/platform-python', '/usr/bin/python3', 'python'])"]}}, "msg": "The following modules failed to execute: setup\n"}
PLAY RECAP ***************************************************************************************************************************************
vios01_cont : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
As already mentioned in the comments, the only module you can use in your case is raw. Furthermore you will not be able to gather facts.
- hosts: "{{ target_hosts }}"
gather_facts: no
tasks:
- name: "Do something"
raw: 'echo "test"'
register: result
failed_when: result.rc != 0
Additionally you will need to address the error handling by yourself, as well if something has changed via changed_when.
Source
Whats the difference between ansible 'raw', 'shell' and 'command'?

Ansible Tower - aws_s3 module - set python interpreter and install dependencies

I am trying to deploy the below task via Ansible AWX (Tower) and having some issues with the aws_s3 module.
---
- hosts: all
become: yes
tasks:
- name: Setting host facts for Python interpreter
set_fact:
ansible_python_interpreter: "/usr/bin/python3"
- name: 01 - Download file locally
aws_s3:
bucket: temp-buck-0001
object: /test/quiz.sh
dest: /tmp/quiz.sh
mode: get
- name: 02 - Change the file permissions of the shell scrip to allow for execute
file:
path: /tmp/quiz.sh
mode: "u=x"
- name: 03 - Change the working directory to tmp before executing the command.
shell: quiz.sh >> quizlog.txt
args:
chdir: /tmp
I'm getting the below error when trying to deploy the above ansible play. It seems to have a problem with Python dependencies in particular boto3 and botocore so I attempted to install these manually for testing purposes.
However, I'm receiving the below errors. It concerns dependencies for the aws_s3 module. I'm not sure if I have set up my Python interpreter correctly. Any help would be much appreciated.
Also, if anybody could suggest how to write a task to install Python3x and dependencies which is required for the aws_s3 module that would also be much appreciated.
TASK [01 - Download Metricbeats file locally]
********************************** fatal: [Dev-02]: FAILED! => {"changed": false, "module_stderr": "Shared connection to 20.10.12.114
closed.\r\n", "module_stdout": "/bin/sh: /usr/bin/python3: No such
file or directory\r\n", "msg": "The module failed to execute
correctly, you probably need to set the interpreter.\nSee
stdout/stderr for the exact error", "rc": 127}
and the below output
{ "module_stdout": "/bin/sh: /usr/bin/python3: No such file or
directory\r\n", "module_stderr": "Shared connection to 20.10.12.114
closed.\r\n", "msg": "The module failed to execute correctly, you
probably need to set the interpreter.\nSee stdout/stderr for the exact
error", "rc": 127, "_ansible_no_log": false, "changed": false }
For context, this is the version of Python I have installed:
[ec2-user#ip-20-10-12-114 ~]$ pip --version
pip 21.0.1 from /usr/lib/python3.6/site-packages/pip (python 3.6)
[ec2-user#ip-20-10-12-114 ~]$ which pip
/usr/bin/pip
[ec2-user#ip-20-10-12-114 ~]$ python --version
Python 2.7.18
[ec2-user#ip-20-10-12-114 ~]$ which python
/usr/bin/python
[ec2-user#ip-20-10-12-114 ~]$ python3 --version
Python 3.6.2
[ec2-user#ip-20-10-12-114 ~]$ which python3
/usr/bin/python3

Kolla-ansible openstack: Docker version failure

I have encountered a problem during a deploy of openstack pike.
There's an error running pre-check command.
Kolla-ansible version 6.0.0.0rc2, Ubuntu Server 16 LTS [error][1]
I've found out that the required docker version is >=2.4.6.
My current is 1.12.6.
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'result | failed or result.stdout | regex_replace('.(\\d+\\.\\d+\\.\\d+).', '\\1') | version_compare(docker_version_min, '<')' failed. The error was: template error while templating string: no filter named 'failed'. String: {% if result | failed or result.stdout | regex_replace('.(\\d+\\.\\d+\\.\\d+).', '\\1') | version_compare(docker_version_min, '<') %} True {% else %} False {% endif %}"}
So, I tried to upgrade it by running command
sudo pip install -U docker==2.4.2
But docker version still says it is 1.12.6 and an error occurs.
My question is: do I have to upgrade docker in other way, change something in kolla-ansible playbooks, or maybe something else?
PS I know pike, Ubuntu srv 16 and 6.0.0.0rc2 are a little bit outdated, but its just for my university project :)
failed is not a filter but a test.
Using tests with the filter syntax used to be allowed. It has been deprecated in ansible 2.5 (with warnings) and totally removed in ansible 2.9.
The correct syntax is:
result is failed
Your problem is that you installed the wrong docker.
Kolla needs docker engine. You should install docker-ce by following this:
https://docs.docker.com/install/linux/docker-ce/ubuntu/
And then try again.
Also remove docker from pypi:
pip uninstall docker
And the one from apt:
sudo apt remove docker
The problem was in ansible. Like #Zeitounator said, that script worked only in ansible 2.5 or less, so this one solved my problem:
sudo -H pip install ansible==2.5.6
But another fail is:
TASK [prechecks : Checking docker SDK version] *******************************************************************************************
fatal: [localhost]: FAILED! => {"changed": false, "cmd": ["/usr/bin/python", "-c", "import docker; print docker.version"], "delta": "0:00:00.014776", "end": "2020-01-21 14:35:06.561138", "failed_when_result": true, "msg": "non-zero return code", "rc": 1, "start": "2020-01-21 14:35:06.546362", "stderr": "Traceback (most recent call last):\n File \"\", line 1, in \nImportError: No module named docker", "stderr_lines": ["Traceback (most recent call last):", " File \"\", line 1, in ", "ImportError: No module named docker"], "stdout": "", "stdout_lines": []}
solution is:
sudo pip install docker

Python - Using Fabric with Sudo

I'm pretty new to python and fabric and I am trying to do a simple code where I can get the output on two hosts that uses sudo, although I keep getting an error.... Can anyone help me out with what I might be missing ?
My code:
from fabric.api import *
from getpass import getpass
from fabric.decorators import runs_once
env.hosts = ['host1','host2']
env.port = '22'
env.user = 'username'
env.password="password"
def sudo_dsmc(cmd):
sudo("-l")
When I run: fab sudo_dsmc:"-1" :
MacBookPRO:PYTHON username$ fab sudo_dsmc:"-l"
[host1] Executing task 'sudo_dsmc'
[host1] sudo: -l
[host1] out: sudo password:
[host1] out: Sorry, user username is not allowed to execute '/bin/bash -l -c - l' as root on host1.
[host1] out:
Fatal error: sudo() received nonzero return code 1 while executing!
Requested: -l
Executed: sudo -S -p 'sudo password:' /bin/bash -l -c "-l"
Aborting.
Disconnecting from host1... done.
Although I can run the apt-get update with my below function fine without any errors:
def sudo_command(cmd):
sudo("apt-get update")
# run like: fab sudo_command:"apt-get-update"
It looks like your sudoers file is preventing you from running that command as sudo. Check your /etc/sudoers file and read the sudo documentation.
Also "-l" isn't a valid command. sudo takes -l as an optional flag (which lists commands allowed by the user). But Fabric's sudo appears to be taking unknown strings and routing them through /bin/bash instead of using them directly as sudo command parameters.

Categories

Resources