相关网址
- Ansible github: https://github.com/ansible/an...
- Ansible 官方文档: https://docs.ansible.com/
配置 ssh 免密登录
- 将公钥传送到 ssh 被控服务器
主控端配置(配好后用
ssh node01
测试)vim ~/.ssh/config
# 将 ip 本身也作为别名之一 Host node01 172.31.10.157 HostName 172.31.10.157 Port 22 User ubuntu IdentityFile ~/.ssh/id_rsa Host node02 172.31.15.123 HostName 172.31.15.123 Port 22 User ubuntu IdentityFile ~/.ssh/id_rsa
基本命令
# ping 所有资产清单上的主机 ansible all -m ping # ping node 组的所有主机 ansible node -m ping
执行 shell 命令
ansible node -a 'ls /home/ubuntu'
执行脚本
ansible-playbook 1_push.yml
Ansible 配置 Elasticsearch
环境
操作系统: Ubuntu 20.04 LTS
Python: 3.8.5
Ansible: 2.10.4
资产清单(Inventory)
cat /etc/ansible/hosts
[node]
172.31.10.157 ansible_user=ubuntu es_node_name=node01
172.31.15.123 ansible_user=ubuntu es_node_name=node02
172.31.8.77 ansible_user=ubuntu es_node_name=node03
脚本(PalyBook)
- 推送(copy)
# 1_push.yml
- hosts: node
user: ubuntu
tasks:
- name: push file to node
copy:
src: /home/ubuntu/es_down
dest: /home/ubuntu
mode: 0660
- 安装(shell)
# 2_install.yml
- hosts: node
user: ubuntu
tasks:
- name: Configure jvm.options
become: true
become_user: root
shell: dpkg -i /home/ubuntu/es_down/elasticsearch-7.10.1-amd64.deb
- 配置 jvm(lineinfile)
# 3_config_jvm.yml
- hosts: node
user: ubuntu
tasks:
- name: Configure jvm.options
become: true
become_user: root
lineinfile:
dest: '/etc/elasticsearch/jvm.options'
regexp: "{{ item.old }}"
line: "{{ item.new }}"
with_items:
- {old: '^-Xms',new: '-Xms8g' }
- {old: '^-Xmx',new: '-Xmx8g' }
- 配置 Elasticsearch(lineinfile)
# 4_config_es.yml
- hosts: node
user: ubuntu
tasks:
- name: Configure elasticsearch.yml
become: true
become_user: root
lineinfile:
dest: '/etc/elasticsearch/elasticsearch.yml'
backrefs: no
state: present
regexp: "{{ item.old }}"
line: "{{ item.new }}"
with_items:
- {old: '^cluster.name', new: 'cluster.name: esapi' }
- {old: '^node.roles', new: 'node.roles: [data]' }
- {old: '^node.name', new: 'node.name: {{ es_node_name }}' }
- {old: '^myhost', new: 'myhost: {{ inventory_hostname }}' }
- {old: '^path.data', new: 'path.data: /data/elasticsearch/data' }
- {old: '^path.log', new: 'path.log: /data/elasticsearch/log' }
- {old: '^network.host', new: 'network.host: 0.0.0.0' }
- {old: '^http.port', new: 'http.port: 9200' }
- {old: '^discovery.seed_hosts', new: 'discovery.seed_hosts: ["172.31.0.1", "172.31.0.2", "172.31.0.3"]' }
- {old: '^cluster.initial_master_nodes', new: 'cluster.initial_master_nodes: ["node01", "node02", "node03"]' }
本文出自 qbit snap
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。