run python script locally using ansible passing remote host has input - python

want to fetch remote hosts with out ssh to the remote host i want to run python script local. by passing remote host has input to my python script
by {{ inventory_hostname }}".
- hosts: "{{ variable_host | default('web')}}"
become: true
any_errors_fatal: yes
serial: 1
tasks:
- name: execute install script
command: python myscript.py -i "{{ inventory_hostname }}" -f script.cql -u username-p password
delegate_to: 127.0.0.1
i got the error
FAILED! => {"changed": false, "failed": true, "module_stderr": "sudo: a password is required\n", "module_stdout": "", "msg": "MODULE FAILURE", "rc": 1}

this looks like a case of ansible not knowing your sudo password.
see Specify sudo password for Ansible et al.

Related

The following modules failed to execute: ansible.legacy.setup?

I am trying to use the Dell OpenManage Ansible Modules to communicate with a PowerEdge's iDRAC. I cannot find a solution to my problem online, hopefully someone here will be able to assist. The only real answer I have found is that the host machine might not be using Python but as you can see from the error below, the host is in fact using a python interpreter. It is not the exact same interpreter as what is in the virtual environment I am running the playbook out of, I am not sure if that makes a difference or not.
Device:
PowerEdge R620 and iDRAC7
Playbook:
---
- hosts: PowerEdge
connection: local
gather_facts: False
tasks:
- name: Get hardware inventory
dellemc_get_system_inventory:
idrac_ip: "IP"
idrac_user: "USER"
idrac_password: "PASSWORD"
validate_certs: false
become: yes
Command:
ansible-playbook playbook.yml -i iDRAC_IP, -u USER --ask-pass -vvv -K
Error:
fatal: [iDRAC_IP]: FAILED! => {
"ansible_facts": {},
"changed": false,
"failed_modules": {
"ansible.legacy.setup": {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"failed": true,
"module_stderr": "Shared connection to iDRAC_IP closed.\r\n",
"module_stdout": "\rcmdstat\r\n\r\tstatus : 2\r\n\r\tstatus_tag : COMMAND PROCESSING FAILED\r\n\r\terror : 252\r\n\r\terror_tag : COMMAND SYNTAX ERROR\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 0,
"warnings": [
"Platform unknown on host iDRAC_IP is using the discovered Python interpreter at /usr/bin/python, but future installation of another Python interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.13/reference_appendices/interpreter_discovery.html for more information."
]
}
},
"msg": "The following modules failed to execute: ansible.legacy.setup\n"
}
the host machine might not be using Python but as you can see from the error below
This module uses a REST API which uses port 443.
Ansible doesn't like to use passwords, it is highly reccomended to use SSH Keys. Here is a tutorial to make the keys and place them on ESXI (stop at the ESXI part).
Create SSH Keys
To place the SSH key on an iDRAC (I used this method on a Dell PowerEdge FC430), follow the directions on Page 77 of this reference guide.
Placing SSH Keys on iDRAC
The playbook that worked for me
---
- hosts: host_file
gather_facts: False
collections: dellemc.openmanage
tasks:
- name: Get System Inventory
dellemc.openmanage.idrac_system_info:
idrac_ip: ip
idrac_password: pass
idrac_user: user
validate_certs: False
delegate_to: localhost
Host file contains:
host IP address
ansible_connection=ssh
remote username
Calling ansible playbook
ansible-playbook -vvvv <your_file>.yml
If that doesn't work, you should verify that python is upgraded to 3.8.6, and check if the iDRAC firmware upgraded to 2.82.82.82.

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'?

Fatal error at provisioning while installing 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.

Using authorized_key module in a playbook to set up SSH key for new users

Scenario:
Based on the [clients] section of the hosts file do the following:
Check if the SSH login of user "foo" fails and if yes
Add SSH keys for user "foo" using authorized_key module
Assuming that user "foo" already exists on remote machine and SSH public key has already been created on the local (ansible) host
I am aware of this solution using Ansible command line but I would like to be able to put this into a playbook. It's acceptable to make script interactive with user typing in password including sudo.
Right now I figured out how to do what I want using 3-rd party role GROG.authorized-key but it still requires me to run playbook with -K switch. Is there something in Ansible (beside command line switches) that would only prompt for the password if it is needed?
- hosts: clients
vars:
authorized_key_list:
- name: pdo
authorized_keys:
- key: "{{ lookup('file', '/home/pdo/.ssh/id_rsa.pub') }}"
state: present
roles:
- { role: GROG.authorized-key }
I think based on your comments this should work:
- hosts: clients
become: true
tasks:
- name: Add authorized_key to pdo user on the remote client machine(s)
authorized_key: user=foo key="{{ lookup('file', '/home/pdo/.ssh/id_rsa.pub') }}"
Call it with -K to get the become password question. This will make a sudo command on the remote machine. Thats what you need, isn't it?
Special kudos to GROG who helped me to understand what I was doing wrong.
Basically I was trying to do root job while running Ansible playbook as non-root user. I ended up creating the following bootstrap.yml and running it with this command:
ansible-playbook ./bootstrap.yml -u root -k
This will run my playbook as root with the root password prompt and was able to create the user and establish sudo and passwordless access
---
# file: bootstrap.yml
# Execute once as root user to create a public key and install it to your client machine(s) using the following command
# ansible-playbook ./auth-client.yml -u root -k
# This requires you to install GROG.management-user role from the Ansible Galaxy using this command:
# ansible-galaxy install GROG.management-user
# Add pdo user on remote machines
- hosts: all
tasks:
- name: Add remote users
user: name=pdo group=users
# Generate SSK keys at the localhost for pde user
- hosts: localhost
tasks:
- name: Provision local pdo user
user: name=pdo generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa
# Install public key into remote machine
- hosts: all
vars:
authorized_key_list:
- name: pdo
authorized_keys:
- key: "{{ lookup('file', '/home/pdo/.ssh/id_rsa.pub') }}"
state: present
roles:
- { role: GROG.authorized-key }
# Add sudo privileges for pdo user
- hosts: all
roles:
- { role: GROG.sudo, become: yes }

msg: ConnectionError(ProtocolError('Connection aborted.', error(2, 'No such file or directory')),)

I get an error on TASK: nginx container:
failed: [localhost] => {"changed": false, "failed": true}
msg: ConnectionError(ProtocolError('Connection aborted.', error(2, 'No such file or directory')),)
FATAL: all hosts have already failed -- aborting
When play next Ansible playbook:
---
- name: Play
hosts: localhost
vars: []
tasks:
- name: nginx container
docker:
name: my.nginx2
image: nginx
state: started
What I do wrong? Is this a bug?
P.S. More verbose output got with -vvvv is:
<localhost> REMOTE_MODULE docker state=started name=my.nginx2 image=nginx
<localhost> EXEC ['/bin/sh', '-c', 'mkdir -p $HOME/.ansible/tmp/ansible-tmp-1431434101.65-11072088770561 && chmod a+rx $HOME/.ansible/tmp/ansible-tmp-1431434101.65-11072088770561 && echo $HOME/.ansible/tmp/ansible-tmp-1431434101.65-11072088770561']
<localhost> PUT /tmp/tmp7ySlXq TO /home/victor/.ansible/tmp/ansible-tmp-1431434101.65-11072088770561/docker
<localhost> EXEC ['/bin/sh', '-c', u'LANG=C LC_CTYPE=C /usr/bin/python /home/victor/.ansible/tmp/ansible-tmp-1431434101.65-11072088770561/docker']
failed: [localhost] => {"changed": false, "failed": true}
msg: ConnectionError(ProtocolError('Connection aborted.', error(2, 'No such file or directory')),)
FATAL: all hosts have already failed -- aborting
You should install docker:
- name: install docker
shell: curl -sSL https://get.docker.com/ | sh
args:
creates: /usr/bin/docker
And you should check that it works:
- name: Wait for the Docker server to start
action: raw docker version
register: docker_version
until: docker_version.stdout.find("Client") != -1
retries: 30
delay: 10
And you need met all dependencies(http://docs.ansible.com/ansible/docker_module.html):
Requirements (on host that executes module)
python >= 2.6
docker-py >= 0.3.0
The docker server >= 0.10.0

Categories

Resources