Ansible linux部署错误{"失败":true,"解析":false}

时间:2015-12-24 08:23:08

标签: linux bash deployment ssh ansible

我有3台Linux机器,一台用于部署,两台用于部署应用程序。我使用Ansible,我有一个问题。 在我的部署过程中的某个时刻,我想远程执行这两台机器上的bash脚本。

我有一个看起来像这样的剧本app_stop.yml:

### rel/app_stop.yml
#
# Erlang app stop playbook

-
  hosts: fe
  gather_facts: false
  sudo: app

  roles:
    - app_stop

我的app_stop角色在某个时候有一个任务,当它从机器调用一些东西来ping应用程序时,他们会用pong回复。为了简化操作,我创建了一个将回显pong的bash脚本,并尝试运行它。它给了我同样的错误。

这是app_stop角色:

- name: Is the Erlang app installed?
  stat: path="{{ app_install_dir }}/{{ app_name_short }}/bin/{{ app_name_short }}"
  register: bin



- name: Ping the Erlang app
  command: "sudo /home/deploy/script.bash"
  when: bin.stat.exists == true
  register: ping
  ignore_errors: yes



- name: Stop the Erlang app
  command: "sudo -u {{ app_user }} {{ app_install_dir }}/{{ app_name_short }}/bin/{{ app_name_short }} stop"
  when: ping is defined and ping.stdout.find("pong") != -1
  ignore_errors: yes

- name: Determine the ERTS bin folder path
  shell: "ls {{ app_install_dir }}/{{ app_name_short }}/erts-*/bin/epmd"
  when: bin.stat.exists == true
  register: epmd
  ignore_errors: yes

- name: Stop the Erlang port mapping daemon (epmd)
  command: "sudo -u {{ app_user }} {{ epmd.stdout_lines[0] }} -kill"
  ignore_errors: yes
  when: epmd.stdout_lines is defined

第二项任务很重要。我ignore_errors: yes的原因是,如果应用程序关闭,我将停止守护程序,如果不是,我将首先停止应用程序,然后停止守护程序。

Ping Erlang应用程序失败,即使我没有做任何Erlang进程,我只调用打印pong的shell脚本。 (如果我手动连接ssh,它会起作用。)

我用Ansible得到的错误是下一个错误:

failed: [stefan1] => {"failed": true, "parsed": false}
/bin/sh: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory
[sudo] password for deploy: 
{"changed": true, "end": "2015-12-24 03:04:55.207125", "stdout": "", "cmd": ["sudo", "/home/deploy/script.bash"], "start": "2015-12-24 02:59:55.095913", "delta": "0:05:00.111212", "stderr": "", "rc": 1, "warnings": []}
OpenSSH_6.6.1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /home/stefan-work-tiefighter-dev/.ssh/config
debug1: /home/stefan-work-tiefighter-dev/.ssh/config line 8: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 56: Applying options for *
debug1: auto-mux: Trying existing master
debug2: fd 3 setting O_NONBLOCK
debug2: mux_client_hello_exchange: master version 4
debug3: mux_client_forwards: request forwardings: 0 local, 0 remote
debug3: mux_client_request_session: entering
debug3: mux_client_request_alive: entering
debug3: mux_client_request_alive: done pid = 100975
debug3: mux_client_request_session: session request sent
debug1: mux_client_request_session: master session id: 2
debug3: mux_client_read_packet: read header failed: Broken pipe
debug2: Received exit status from master 0
Shared connection to 192.168.0.201 closed.

1 个答案:

答案 0 :(得分:0)

正如小鸡指出的那样,你可以使用Ansible" sudo"和" sudo_user"在Command任务上的属性,它会更清晰。

你的第二个任务来自" app_stop"角色看起来像这样:

- name: Ping the Erlang app
  command: "/home/deploy/script.bash"
  when: bin.stat.exists
  register: ping
  ignore_errors: yes
  sudo: yes

关于您收到的错误消息,来自操作系统的抱怨没有正确设置区域设置。 我之前遇到过同样的问题,我通常使用一个Ansible任务来确保基于模板文件正确配置Locale。

从您的输出中,我猜测您正在使用CentOS,所以这是我使用的代码。

为区域设置定义变量很有意思,只是为了提高可重用性。

这将是模板( i18n.j2 ):

LANG="{{ locale }}"
SYSFONT="latarcyrheb-sun16"
LC_CTYPE="{{ locale }}"

这将是配置CentOS使用它的任务:

- name: Ensure language and locale are correct
  template: src="i18n.j2" dest="/etc/sysconfig/i18n"
  sudo: yes