无法使用ansible playbook运行DB2脚本

问题描述 投票:0回答:1

创建了脚本文件

script.sh
,其中包含命令

db2 connect to <DB Name>

上述 shell 脚本保存在远程服务器中,我尝试通过

ansible-playbook
运行此脚本,该脚本可在我的笔记本电脑上本地使用,如下所述:

- 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 }}"

有库存文件

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>

运行命令时

ansible-playbook -i inventory playbook.yml

出现以下错误:

致命:[node2]:失败! => {“更改”:true,“cmd”:“db2连接”,“delta”:“0:00:00.004647”,“end”:“2023-09-29 03:32:53.888841”,“msg” : "非零返回码", "rc": 127, "start": "2023-09-29 03:32:53.884194", "stderr": "/bin/sh: 1: db2: 未找到", “stderr_lines”:[“/bin/sh:1:db2:未找到”],“stdout”:“”,“stdout_lines”:[]}

建议会有帮助

ansible db2 ansible-2.x ansible-inventory
1个回答
0
投票

最后我用下面的方法解决了:

- 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 }}"
© www.soinside.com 2019 - 2024. All rights reserved.