Basic ansible command-line fails with module failure error - ansible

My ansible command fails with error as below:
$ ansible all -i /tmp/myhosts -m shell -a 'uname -a' -u user1 -k
SSH password:
host1.mycomp.com | FAILED! => {
"changed": false,
"module_stderr": "",
"module_stdout": " File \"/home/user1/.ansible/tmp/ansible-tmp-1556597037.27-168849792658772/AnsiballZ_command.py\", line 39\r\n with open(module, 'wb') as f:\r\n ^\r\nSyntaxError: invalid syntax\r\n",
"msg": "MODULE FAILURE\nSee stdout/stderr for the exact error",
"rc": 1
}
I do not know how to See stdout/stderr from single command line. Any suggestion how to view std out/err would be great.
$ more /tmp/myhosts
[mybox]
host1.mycomp.com
[dev]
host1.mycomp.com
[allserver:children]
mybox
dev
[allserver:vars]
variable=somestring
/tmp/hosts has good permissions.
I'm also able to ssh to the target server using the userid
$ ssh user1#host1.mycomp.com
Password:
Last login: Tue Apr 30 00:03:57 2019 from 10.123.235.126
$ hostname
host1
ansible --version
2.7.1
Can you please help me overcome the error ?

Related

how does become-user differ bettwen adhoc and playbooks

Using ansible i am logging in as LDAP user and then sudo/becoming the 'db2inst1' user to run some commands. The adhoc mode runs fine but if I try things using a playbooks I get an error that means I need to add the user to the sudoers files.
This works fine. (i had to edit some things out :) )
ansible all -i ftest.lst -e ansible_user=user#a.com -e "ansible_ssh_pass={{pass}}" -e "ansible_become_pass={{test_user}}" -e "#pass.yml" --vpas vault -a ". /home/db2inst1/sqllib/db2profile;db2pd -hadr -alldbs;whoami" -m raw -b --become-user=db2inst1
But connecting to the same box and using a playbook i get issues.
ansible-playbook -i ftest.lst -e target=test -e ansible_user=user#a.com -e "ansible_ssh_pass={{pass}}" -e "ansible_become_pass={{test_user}}" -e "#pass.yml" --vpas vault test.yml
The test.yml has
become: true
& become_user: "{{ instance }}"
with the instance = db2inst1 being passed it.
This spits out...
fatal: [hostname123.a]: FAILED! => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python"}, "changed": false, "module_stderr": "Shared connection to hostname123.a closed.\r\n", "module_stdout": "\r\n", "msg": "MODULE FAILURE\nSee stdout/stderr for the exact error", "rc": 1}z

Ansible4 builtin shell rewrites trust list command to trust list-modules

Ansible4 builtin shell rewrites trust list command to trust list-modules:
- block:
- name: Check if Certs installed - Linux RHEL8 - step 1
ansible.builtin.shell:
cmd: "trust list"
register: isCertInstalled
some additional formatting is piped to whittle this down to a number (omitted for privacy purposes), then:
TASK [Check if Certs installed - Linux RHEL8 - step 1] ********************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"changed": true, "cmd": ["trust", "list"], "delta": "0:00:00.007482", "end": "2022-06-21 18:20:43.759496", "msg": "non-zero return code", "rc": 2, "start": "2022-06-21 18:20:43.752014", "stderr": "p11-kit: 'list-modules' is not a valid command. See 'trust --help'", "stderr_lines": ["p11-kit: 'list-modules' is not a valid command. See 'trust --help'"], "stdout": "", "stdout_lines": []}
Any idea how to force it to literally just run "trust list" instead of "trust list-modules"?
RHEL 8.5, Ansible 4.1, Python 3.9
Use the full path for the trust command and enclose trust list in single quotes. Ansible is only seeing LIST, tying that in as list module.
ie. "'trust list' | grep Internal-Cert | wc -l | tr -d '\n'"

Running gather_facts: true fails with "uid not found: 1001"

I have a ansible-playbook that has gather_facts set to true. But it fails to get the uid. Here is the error I am getting:
TASK [Gathering Facts] **************************************************************************************************************************************************************
The full traceback is:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 144, in run
res = self._execute()
File "/usr/lib/python2.7/site-packages/ansible/executor/task_executor.py", line 516, in _execute
self._play_context = self._play_context.set_task_and_variable_override(task=self._task, variables=variables, templar=templar)
File "/usr/lib/python2.7/site-packages/ansible/playbook/play_context.py", line 335, in set_task_and_variable_override
new_info.remote_user = pwd.getpwuid(os.getuid()).pw_name
KeyError: 'getpwuid(): uid not found: 1001'
fatal: [localhost]: FAILED! => {
"msg": "Unexpected failure during module execution.",
"stdout": ""
}
Now, the 1001 uid is present in the setup:
$ echo $UID
1001
I am running this inside a container, could that be an issue? Any pointers to help debug this are appreciated. TIA.
I am running this inside a container, could that be an issue?
While that doesn't automatically make it a problem, it is perhaps relevant since you can more easily execute a process as an arbitrary UID inside a docker container. You don't typically see that problem on a virtual machine because in order to run anything on the virtual host, you have to actually be authenticated first, which almost always involves looking up all kinds of user information in /etc/passwd. However, there is usually no "login" process for a container, since it is just Linux namespace trickery
You can try it yourself by running docker run --rm -u 12345 ubuntu:18.04 id -a and observe uid=12345 gid=0(root) groups=0(root) but there is no entry in /etc/passwd for UID 12345 (notice the missing (something) after the uid= result)
2 solutions:
- hosts: all
gather_facts: no
or
ansible_test:
image: docker.io/major/ansible:fedora29
script:
- echo "tempuser:x:$(id -u):$(id -g):,,,:${HOME}:/bin/bash" >> /etc/passwd
- echo "tempuser:x:$(id -G | cut -d' ' -f 2)" >> /etc/group
- id
- ansible-playbook -i hosts playbook.yml
https://major.io/2019/03/22/running-ansible-in-openshift-with-arbitrary-uids/

Ansible script module - Control socket permission denied

I'm new to Ansible and trying to run a local script on a remote node using the script module. My task is defined as follows:
- name: Initial setup
script: ../../../initial_setup.sh
become: yes
When I run the playbook I get the error below but I'm not clear on what the actual problem is. Does this indicate a problem connecting to the node or a problem transferring the script?
fatal: [default]: FAILED! => {
"changed": true,
"failed": true,
"invocation": {
"module_args": {
"_raw_params": "../../../initial_setup.sh"
},
"module_name": "script"
},
"rc": 127,
"stderr": "Control socket connect(/tmp): Permission denied\r\nControlSocket /tmp already exists, disabling multiplexing\r\nConnection to 127.0.0.1 closed.\r\n",
"stdout": "/bin/sh: 1: /home/ubuntu/.ansible/tmp/ansible-tmp-1482161914.64-107588947758469/initial_setup.sh: not found\r\n",
"stdout_lines": [
"/bin/sh: 1: /home/ubuntu/.ansible/tmp/ansible-tmp-1482161914.64-107588947758469/initial_setup.sh: not found"
]
}
tl;dr
Ensure -o ControlMaster=auto is defined in ssh_args in Ansible in ansible.cfg:
[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=60s
The following error is related to SSH connection multiplexing:
Control socket connect(/tmp): Permission denied
ControlSocket /tmp already exists, disabling multiplexing
Connection to 127.0.0.1 closed
It tried to create a socket directly at /tmp, not inside /tmp... Some other parameter defined somewhere for SSH could play role here.
Setting the value of ControlMaster to auto causes SSH to create a new master connection should the existing one not exist (or have problems, as here?).

Ansible execute commands

I have configured a fully distributed hadoop cluster using ansible, but I want to configure ansible to execute the next command too.
I use the next to execute zookeeper, but I have the next error.
- name: ejecutar zkServer.sh
become_user: vagrant
shell: sh /opt/zookeeper-3.4.7/bin/zkServer.sh
TASK: [ejecutar | ejecutar zkServer.sh] ***************************************
failed: [172.16.8.211] => {"changed": true, "cmd": "sh /opt/zookeeper-3.4.7/bin/zkServer.sh", "delta": "0:00:00.006246", "end": "2016-01-18 14:28:44.181146", "rc": 2, "start": "2016-01-18 14:28:44.174900", "warnings": []}
stderr: /opt/zookeeper-3.4.7/bin/zkServer.sh: 81: /opt/zookeeper-3.4.7/bin/zkEnv.sh: Syntax error: "(" unexpected (expecting "fi")
FATAL: all hosts have already failed -- aborting
Thanks.
It is working like with the next command:
- name: ejecutar zkServer.sh
become_user: vagrant
command: "/opt/zookeeper-3.4.7/bin/zkServer.sh restart"

Resources