Ansible Tips(qbit)

qbit

相关网址

配置 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

    # 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)

# 1_push.yml
- hosts: node
  user: ubuntu
  tasks: 
    - name: push file to node
      copy: 
        src: /home/ubuntu/es_down
        dest: /home/ubuntu
        mode: 0660
# 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
# 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' }
# 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
阅读 290

qbit snap
开箱即用,拿走不谢。
200 声望
262 粉丝
0 条评论
你知道吗?

200 声望
262 粉丝
宣传栏