Created script file script.sh
which consists of the command
db2 connect to <DB Name>
The above shell script is saved in the remote server and I am trying to run this script via ansible-playbook
which is available locally on my laptop mentioned below:
- hosts: node2
tasks:
- name: change current directory and execute command
register: result
become: true
become_method: su
become_user: db2inst1
changed_when: true
shell:
chdir: /data/db2script/
cmd : ./script.sh
- debug:
msg: "{{ result.stdout }}"
with inventory file inventory
[remote] # group name
node1 ansible_host=<IP Address> ansible_ssh_user=root ansible_ssh_pass=<password>
node2 ansible_host=<IP Address> ansible_ssh_user=db2inst1 ansible_ssh_pass=<password>
while running command ansible-playbook -i inventory playbook.yml
getting below error:
fatal: [node2]: FAILED! => {"changed": true, "cmd": "db2 connect ", "delta": "0:00:00.004647", "end": "2023-09-29 03:32:53.888841", "msg": "non-zero return code", "rc": 127, "start": "2023-09-29 03:32:53.884194", "stderr": "/bin/sh: 1: db2: not found", "stderr_lines": ["/bin/sh: 1: db2: not found"], "stdout": "", "stdout_lines": []}
suggestions will be helpful
Finally It worked out for me using below:
- name: Connect to DB
become: true
become_method: sudo
become_user: db2inst1
become_flags: 'su - db2inst1 /bin/bash -c'
register: result
shell:
cmd : ./script.sh
- debug:
msg: "{{ result.stdout }}"