パターンを見つけて、シェルスクリプトでその値を置き換えます。

パターンを見つけて、シェルスクリプトでその値を置き換えます。

シェルスクリプトを使用して設定ファイルを次のように変更しようとしました。以下のElastic Search設定ファイルは次のとおりです。

次の行をコメントアウトし、その値を置き換える必要があります。

#network.host: 192.168.0.1

ネットワークセクションに次の行を追加します。

transport.host: localhost
transport.tcp.port: 9300

この目標をどのように達成できますか?

# ======================== Elasticsearch Configuration =========================
#
# NOTE: Elasticsearch comes with reasonable defaults for most settings.
#       Before you set out to tweak and tune the configuration, make sure you
#       understand what are you trying to accomplish and the consequences.
#
# The primary way of configuring a node is via this file. This template lists
# the most important settings you may want to configure for a production cluster.
#
# Please consult the documentation for further information on configuration options:
# https://www.elastic.co/guide/en/elasticsearch/reference/index.html
#
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
#cluster.name: my-application
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
#node.name: node-1
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
path.data: /var/lib/elasticsearch
#
# Path to log files:
#
path.logs: /var/log/elasticsearch
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
#bootstrap.memory_lock: true
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.0.1
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
#discovery.zen.ping.unicast.hosts: ["host1", "host2"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
#discovery.zen.minimum_master_nodes: 
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#

ベストアンサー1

sedそれでできます。

最初の部分は非常に簡単です。注釈付きのnetwork.hostを見つけて、それを異なる値を持つ注釈のないネットワークホストに置き換える方法は次のとおりです。

sed -i -e 's/^#network\.host: .*/network.host: 1.2.3.4/' "${ES_HOME}/config/elasticsearch.yml"

この-iオプションは内部変更を実行するため、現在のelasticsearch.ymlファイルを置き換えます。 (たとえば、代替方法をelasticsearch.yml.bak使用してバックアップを保存できます。)-i.bak

引数は-esedスクリプトであり、この場合は検索/置換コマンドを含む正規表現です。#network.hostIPを含むコメントアウトされていない行で始まるコメントアウトされた行を一致させ、それを置き換えます。

環境変数からIPまたはホスト名を取得するには、文字列「...」を2つの部分に分割し、ここに外部変数を挿入します。

sed -i -e 's/^#network\.host: .*/network.host: '"${ip_address}"'/' "${ES_HOME}/config/elasticsearch.yml"

ただし、これは脆弱である可能性があることに注意してください。内容に${ip_address}単一/文字が含まれている場合、sed コマンドは中断されます。

2番目の部分では、Transport.host行を挿入する場合は、sedのi\コマンドを使用して一致する行の前に行を挿入できます。たとえば、ネットワークセクションの最後の説明(「...ネットワークモジュールのマニュアルを参照」)を一致させて挿入できます。複数の行を挿入する場合は、複数の{コマンドを実行できるように新しいブロックを起動する必要があります。

これにより、トリックを実行できます(このコマンドは複数行にわたって適用されます!)。

sed -i -e '
/consult the network module documentation/{
i\
# Set custom transport settings:
i\
#
i\
transport.host: localhost
i\
transport.tcp.port: 9300
i\
#
}' "${ES_HOME}/config/elasticsearch.yml"

これで、それらを一緒に集め、以前に挿入が行われた場合は挿入をスキップするようにチェックを追加することもできます。挿入されたコメント(「カスタムトランスポート設定の設定」)を見つけて、コマンドを使用してスクリプトの最後に移動すると、bこれを行うことができます。この場合は、次の編集内容をスキップしてください。

最終スクリプトは次のとおりです。

# Set your own IP into ${ip_address} however you have to.
ip_address=1.2.3.4
sed -i -e '
s/^#network\.host: .*/network.host: '"${ip_address}"'/
/^# Set custom transport settings/,$b
/consult the network module documentation/{
i\
# Set custom transport settings:
i\
#
i\
transport.host: localhost
i\
transport.tcp.port: 9300
i\
#
}' "${ES_HOME}/config/elasticsearch.yml"

おすすめ記事