&& 运算符在 Ansible 中创建新连接

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

我是ansible的新手,我想为db2运行一个shell命令,该命令连接到db2并为我运行.sql文件,但是它在&&运算符之后创建一个新连接,并为.sql命令它然后说没有找到连接。如何使用 && 并仍然成功运行剧本?

这是我正在尝试运行的任务:

- name: Update file
  shell: "db2 connect to db2_db user db2_user using db2_password && db2 -tvf file.sql"
  args:
    chdir: "{{ work_dir }}"
  become: yes
  become_user: "{{ user }}"
  become_flags: -i

我收到的错误是:

The command was processed as an SQL statement because it was not a \nvalid Command Line Processor command.  During SQL processing it returned:\n  A database connection does not exist.
ansible db2
1个回答
0
投票

我认为您的默认 shell 可能无法正确处理

&&
。尝试使用:

  args:
    executable: /bin/bash

在 shell 模块中它可能会有所帮助。但如果我是你,我会使用多行 shell 来肯定解决问题:

---
- name: Test
  hosts: localhost
  tasks:
  - name: Test touch
    shell: |
      touch a
      touch b
© www.soinside.com 2019 - 2024. All rights reserved.