ansible batch management server, ansible-playbook

ansible in the actual management process can not directly change the configuration file operations, we need to create a folder, will be written into the key configuration, but also to create a list of host names to create the configuration file in accordance with:

[root@ansible ~]# mkdir /opt/xxoo

[root@ansible ~]# cd /opt/xxoo

[root@ansible ~]# vim ansible.cfg

[defaults]

inventory = / opt / xxoo / hosts // define a list of hosts
host_key_checking = False // let each of the host operating when prompted, should it have every indication that we need to turn it off

[root@ansible xxoo]#vim es.yml 

---
- hosts: es1,es2,es3
  remote_user: root
  tasks:
    - copy:
        src: local.repo
        dest: /etc/yum.repos.d/local.repo
        owner: root
        group: root
        mode: 0644

    - name: install the latest version of Apache
      yum:
        name: elasticsearch
        state: installed

    - name: install java-1.8.0-openjdk.x86_64
      yum:
        name: java-1.8.0-openjdk.x86_64
        state: installed
      tags: openjdk
      notify:
           restart es

    - template:
        src: elasticsearch.yml
        dest: /etc/elasticsearch/elasticsearch.yml
        owner: root
        group: root
        mode: 0644
      tags: es_change
      notify:
        - restart es

  handlers:
    - name: restart es
      service:
        name: elasticsearch
        state: restarted
        enabled: yes

By ansible manage multiple servers:

[root@ansible xxoo]#ansible-playbook es.yml

We define the es.yml the file, tags and handlers, so we can only perform ansible, a specific module command in order to avoid duplication of command, improve code reuse:

[root@ansible xxoo]#ansible-playbook es.yml -t openjdk

Guess you like

Origin blog.csdn.net/sxjwcs/article/details/92610821