在多个主机上运行角色角色?

时间:2018-12-21 10:13:34

标签: ansible ansible-inventory ansible-galaxy

我写了一个有趣的角色来安装elasticsearch多节点集群, 问题是我为每种类型的节点(主节点,数据节点)都有一个配置文件。我在指定每个剧本中的主机时遇到问题。

setup_elastic.yml

---
- hosts: all
  become: yes
  become_user: root
  roles:
  - elasticsearch

这是母版的配置文件:

---
    - name: config elasticsearch master
      blockinfile:
          path: /etc/elasticsearch/elasticsearch.yml
          block: |
                    cluster.name: lamedicale4
                    node.master: true
                    node.data: false
                    node.name: ${HOSTNAME}
                    bootstrap.mlockall: true
                    path.data: /data/elasticsearch
                    path.logs: /logs/elasticsearch
                    discovery.zen.minimum_master_nodes: 2
                    discovery.zen.ping.multicast.enabled: false
                    discovery.zen.ping.unicast.hosts : ["172.31.36.229","172.31.44.124"]

数据节点的配置文件:

---
    - name: config elasticsearch data nodes
      blockinfile:
          path: /etc/elasticsearch/elasticsearch.yml
          block: |
                    cluster.name: lamedicale4
                    node.master: false
                    node.data: true
                    node.name: ${HOSTNAME}
                    bootstrap.mlockall: true
                    path.data: /data/elasticsearch
                    path.logs: /logs/elasticsearch
                    discovery.zen.minimum_master_nodes: 2
                    discovery.zen.ping.multicast.enabled: false
                    discovery.zen.ping.unicast.hosts : ["172.31.36.229","172.31.44.124"]

这是主要任务:

---
# tasks file for /etc/ansible/roles/elastissearch

- import_tasks: install_java_jdk.yml
- import_tasks: install_elasticsearch.yml
- import_tasks: clear_file.yml
- import_tasks: directory_data_log_elastic.yml
- include_tasks: config_elasticsearch_master.yml
- include_tasks: config_elasticsearch_data.yml
- import_tasks: reload_restart_elasticsearch.yml

我该怎么办?

1 个答案:

答案 0 :(得分:0)

回答您的特定问题,将${HOSTNAME}替换为:

  • {{ventory_hostname}}
  • {{ansible_fqdn}}
  • {{ansible_hostname}}

看看this part of the docs,了解它们的来源。

更笼统地说,我建议对“ blockinfile”使用更灵活的方法是将整个配置文件转换为Ansible模板,并使用'template' module对其进行管理。过去,我是使用Elasticsearch配置文件完成此操作的,效果很好。