如何在shell模块中的ansible中传递多个可执行文件?

时间:2018-08-20 14:51:48

标签: ansible ansible-2.x

我试图在执行命令时在提示中传递提示y。 当我在服务器上手动执行操作时,它会提示您。

问题在于命令要运行,我需要传递可执行文件/ bin / bash 命令:source /etc/profile.d/tableau_server.sh && tsm pending-changes apply 要运行期望命令,我需要传递/ usr / bin / expect。

我的问题,我该如何在ansible中传递2个可执行文件,以便对于命令它使用/ bin / bash并对于期望提示它应该使用/ usr / bin / expect,而错误是因为我使用的是源代码,我可以使用替代方法吗?

更新:我不知道为什么,但是我无法通过--ignore-prompt,它给出了错误

ubuntu@ip-xx-xxx-xx-xx:~$ tsm pending-changes apply --ignore-prompt
Unrecognized option: --ignore-prompt

请帮助我解决问题!

ubuntu@ip-xx-xxx-xx-xx:~$ tsm pending-changes apply
This operation will perform a server restart. Are you sure you wish to continue?
(y/n): 

我的ansible脚本:

  shell: |
     source /etc/profile.d/tableau_server.sh && tsm pending-changes apply
     expect "This operation will perform a server restart. Are you sure you wish to continue?\n(y/n):"
     send "y\n"
     exit 0
  args:
     executable: /usr/bin/expect
      args:
         executable: /bin/bash/expect
      when: inventory_hostname == "xx.xxx.xx.xx"

错误:

 changed: [xx.xxx.xxx.xx] => {
    "changed": true,
    "cmd": "source /etc/profile.d/tableau_server.sh && tsm pending-changes apply\n expect \"This operation will perform a server restart. Are you sure you wish to continue?\\n(y/n):\"\n send \"y\\n\"\n exit 0",
    "delta": "0:00:00.034824",
    "end": "2018-08-20 17:29:41.457700",
    "invocation": {
        "module_args": {
            "_raw_params": "source /etc/profile.d/tableau_server.sh && tsm pending-changes apply\n expect \"This operation will perform a server restart. Are you sure you wish to continue?\\n(y/n):\"\n send \"y\\n\"\n exit 0",
            "_uses_shell": true,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": "/usr/bin/expect",
            "removes": null,
            "stdin": null,
            "warn": true
        }
    },
    "rc": 0,
    "start": "2018-08-20 17:29:41.422876",
    "stderr": "wrong # args: should be \"source ?-encoding name? fileName\"\n    while executing\n\"source /etc/profile.d/tableau_server.sh && tsm pending-changes apply\"",
    "stderr_lines": [
        "wrong # args: should be \"source ?-encoding name? fileName\"",
        "    while executing",
        "\"source /etc/profile.d/tableau_server.sh && tsm pending-changes apply\""
    ],
    "stdout": "",
    "stdout_lines": []

2 个答案:

答案 0 :(得分:1)

我要说的是,您在bash命令和'&&'内部命令中所做的事情太多了,这些都不是幂等的。

我是否可以建议您以此返回绘图板。我建议使用'creates'参数创建命令,以便它可以告诉它是否需要运行。

https://docs.ansible.com/ansible/2.6/modules/command_module.html

或者先检查一下,然后再查看是否需要使用寄存器运行该命令。

在这种情况下,您遇到以下问题:

tsm pending-changes apply

应按照https://onlinehelp.tableau.com/current/server-linux/en-us/cli_pending-changes.htm

支持

tsm pending-changes apply --ignore-prompt

这将不会提示您输入yes,并且不需要Expect模块。

答案 1 :(得分:0)

我通过传递-r选项解决了问题。

- name: Initialize and Start Tableau Server
  shell: source /etc/profile.d/tableau_server.sh && tsm pending-changes apply -r -u ubuntu -p '{{ tableau_server_admin_password }}'

  args:
    executable: /bin/bash
  when: inventory_hostname == "xx.xxx.xx.xx"
相关问题