我刚刚开始学习Ansible。我在安装elasticsearch的角色中定义了以下任务。
---
- name: install elasticsearch
homebrew: name=elasticsearch state=present
- command: brew --prefix elasticsearch
register: elasticsearch_home
- name: install elasticsearch-head
command: "{{ elasticsearch_home.stdout }}/bin/plugin --install mobz/elasticsearch-head"
- name: install elasticsearch-analysis-icu
command: "{{ elasticsearch_home.stdout }}/bin/plugin --install elasticsearch/elasticsearch-analysis-icu/2.2.0"
- name: install elasticsearch-inquisitor
command: "{{ elasticsearch_home.stdout }}/bin/plugin --install polyfractal/elasticsearch-inquisitor"
在运行我的剧本时,我会出现以下错误。
PLAY [all] ********************************************************************
GATHERING FACTS ***************************************************************
ok: [localhost]
TASK: [java | install latest java] ********************************************
ok: [localhost]
TASK: [elasticsearch | install elasticsearch] *********************************
ok: [localhost]
TASK: [elasticsearch | command brew --prefix elasticsearch] *******************
changed: [localhost]
TASK: [elasticsearch | install elasticsearch-head] ****************************
failed: [localhost] => {"changed": true, "cmd": "/usr/local/opt/elasticsearch/bin/plugin --install mobz/elasticsearch-head", "delta": "0:00:00.264923", "end": "2015-03-21 21:18:36.863296", "rc": 1, "start": "2015-03-21 21:18:36.598373", "warnings": []}
stderr: Exception in thread "main" org.elasticsearch.common.settings.SettingsException: Failed to load settings from [file:/Users/<username>/elasticsearch.yml]
at org.elasticsearch.common.settings.ImmutableSettings$Builder.loadFromStream(ImmutableSettings.java:947)
at org.elasticsearch.common.settings.ImmutableSettings$Builder.loadFromUrl(ImmutableSettings.java:931)
at org.elasticsearch.node.internal.InternalSettingsPreparer.prepareSettings(InternalSettingsPreparer.java:77)
at org.elasticsearch.plugins.PluginManager.main(PluginManager.java:389)
Caused by: org.elasticsearch.ElasticsearchParseException: malformed, expected settings to start with 'object', instead was [START_ARRAY]
at org.elasticsearch.common.settings.loader.XContentSettingsLoader.load(XContentSettingsLoader.java:65)
at org.elasticsearch.common.settings.loader.XContentSettingsLoader.load(XContentSettingsLoader.java:45)
at org.elasticsearch.common.settings.loader.YamlSettingsLoader.load(YamlSettingsLoader.java:46)
at org.elasticsearch.common.settings.ImmutableSettings$Builder.loadFromStream(ImmutableSettings.java:944)
... 3 more
FATAL: all hosts have already failed -- aborting
PLAY RECAP ********************************************************************
to retry, use: --limit @/Users/<username>/elasticsearch.retry
localhost : ok=4 changed=1 unreachable=0 failed=1
起初,我认为这可能是插件安装程序中的一个错误,错误地处理了插件已经安装的情况。我卸载了插件,然后再次运行播放程序,但我收到了完全相同的错误。我也试着用shell
代替command
,但结果没有差异。
stderr: Exception in thread "main" org.elasticsearch.common.settings.SettingsException: Failed to load settings from [file:/Users/<username>/elasticsearch.yml]
这句话让我认为弹性搜索没有正确接收某些配置。但我不明白为什么它会期望从yml文件中获得信息。即使有一些功能允许将设置管道化到elasticsearch插件安装程序中,我也希望使用Ansible的shell
模块会在一个单独的shell中执行命令,因此elaticsearch对yml文件或Ansible一无所知。有什么想法吗?
根据http://www.elastic.co/guide/en/elasticsearch/reference/master/setup-configuration.html
Elasticsearch设置
elasticsearch配置文件可以在
ES_HOME/config
文件夹下找到。该文件夹包含两个文件,elasticsearch.yml
用于配置Elasticsearch不同模块,logging.yml
用于配置Ellasticsearch日志。
所以最有可能发生的事情是elasticsearch感到困惑,认为你的剧本文件是elasticsearch自己的配置文件。
解决这个问题的最简单方法是将您的剧本文件重命名为不同的文件名。
然而,更合适的方法是将您的任务修改为使用command
模块的chdir
参数。
- name: install elasticsearch-head
command: "bin/plugin --install mobz/elasticsearch-head chdir={{ elasticsearch_home.stdout }}"
这样,该命令就可以在ES的bin/目录中运行。
更改剧本文件名的另一种选择是对command
操作使用Ansible的chdir
功能。
正在更改。。。
- name: install elasticsearch-head
command: "{{ elasticsearch_home.stdout }}/bin/plugin --install mobz/elasticsearch-head"
到…
- name: install elasticsearch-head
command: "bin/plugin --install mobz/elasticsearch-head chdir={{ elasticsearch_home.stdout }}"
解决了问题。我更喜欢这个解决方案,因为这意味着我在文件名的选择上不受限制。但是,之所以选择所选答案(和注释),是因为它描述了根本原因,并且只有当您关心文件名时,才需要此解决方案。