跳转至

优雅安装 Apache Ambari 3

背景

Apache Ambari 是开源的,用于部署、管理和监控 Hadoop 大数据集群的工具。有了它,就可以优雅地安装 Hadoop 集群。

今天我们先优雅地安装 Ambari,下次可以利用 Ambari 优雅地安装 Hadoop。

关键是优雅,不要在控制台上敲一堆命令,尤其是操作多台机器时,一旦敲错、漏命令,真是让人抓狂。我在 23 年的时候,写过几篇安装 Hadoop 集群的博客,当时是一行行命令敲的,太不优雅了。

好消息是,前阵子 Ambari 更新到了 3.0 大版本,支持 Rocky Linux 8 和 9 操作系统,提供官方安装包,且使用开源的 Apache Bigtop 作为大数据组件的安装源,不用受制于闭源的 Cloudera 提供的 HDP 源。

为了快速准备多台虚拟机,我们会用到 Vagrant 这款软件。当实验全部完成后,会快速销毁这些虚拟机。Vagrant 并不能真正的运行虚拟机,它要依赖于 VirtualBox、VMware 这些虚拟机 Hypervisor 来创建、运行和销毁。


安装目标

  • 创建 3 台虚拟机作为实验环境,主机名分别是 vm1、vm2、vm3。
  • vm1 用于安装 ambari-server1,vm1、vm2、vm3 用于安装 ambari-agent2

前置条件

  • 安装 Vagrant,用于快速创建、销毁虚拟机。
  • 安装 VirtualBox,被 Vagrant 调用,用于实际创建和销毁虚拟机。

搭建实验环境

  1. 创建 Vagrantfile

    在 C:\Users\许王伟\dev\vagrant\ambari 目录下新建一个文件,名叫 Vagrantfile。

    Tip

    按照你的习惯建工作目录就好,C:\Users\许王伟\dev\vagrant\ambari 是我的习惯。

    Vagrantfile
    # -*- mode: ruby -*-
    # vi: set ft=ruby :
    VAGRANT_DISABLE_VBOXSYMLINKCREATE=1
    
    Vagrant.configure("2") do |config|
      config.vm.box = "rockylinux/9"
      config.vm.box_version = "6.0.0"
    
      hosts_entries = <<-HOSTS
    192.168.56.11 vm1
    192.168.56.12 vm2
    192.168.56.13 vm3
    HOSTS
    
      (1..3).each do |i|
        config.vm.define "vm#{i}" do |node|
          node.vm.hostname = "vm#{i}"
          node.vm.network "private_network", ip: "192.168.56.1#{i}"
          node.vm.synced_folder ".", "/vagrant", disabled: true
    
          node.vm.provider "virtualbox" do |vb|
            vb.name = "vm#{i}"
            vb.cpus = 2
            vb.memory = 8192
          end
    
          node.vm.provision "shell", inline: <<-SHELL
            echo "Updating /etc/hosts"
            echo "#{hosts_entries}" >> /etc/hosts
          SHELL
        end
      end
    end
    
  2. 启动虚拟机

    进入控制台,确保当前处于 C:\Users\许王伟\dev\vagrant\ambari 目录下。在控制台中输入

    Tip

    我用的 PowerShell,也可以用 CMD,什么都行,只要到工作目录下执行命令就行。

    PS C:\Users\许王伟\dev\vagrant\ambari> vagrant up
    
    输出
    Bringing machine 'vm1' up with 'virtualbox' provider...
    Bringing machine 'vm2' up with 'virtualbox' provider...
    Bringing machine 'vm3' up with 'virtualbox' provider...
    ==> vm1: Importing base box 'rockylinux/9'...
    ==> vm1: Matching MAC address for NAT networking...
    ==> vm1: Checking if box 'rockylinux/9' version '6.0.0' is up to date...
    ==> vm1: Setting the name of the VM: vm1
    ==> vm1: Clearing any previously set network interfaces...
    ==> vm1: Preparing network interfaces based on configuration...
        vm1: Adapter 1: nat     
        vm1: Adapter 2: hostonly
    ==> vm1: Forwarding ports...
        vm1: 22 (guest) => 2222 (host) (adapter 1)
    ==> vm1: Running 'pre-boot' VM customizations...
    ==> vm1: Booting VM...
    ==> vm1: Waiting for machine to boot. This may take a few minutes...
        vm1: SSH address: 127.0.0.1:2222
        vm1: SSH username: vagrant       
        vm1: SSH auth method: private key
        vm1: 
        vm1: Vagrant insecure key detected. Vagrant will automatically replace
        vm1: this with a newly generated keypair for better security.
        vm1: 
        vm1: Inserting generated public key within guest...
        vm1: Removing insecure key from the guest if it's present...
        vm1: Key inserted! Disconnecting and reconnecting using new SSH key...
    ==> vm1: Machine booted and ready!
    ==> vm1: Checking for guest additions in VM...
        vm1: No guest additions were detected on the base box for this VM! Guest
        vm1: additions are required for forwarded ports, shared folders, host only
        vm1: networking, and more. If SSH fails on this machine, please install
        vm1: the guest additions and repackage the box to continue.
        vm1:
        vm1: This is not an error message; everything may continue to work properly,
        vm1: in which case you may ignore this message.
    ==> vm1: Setting hostname...
    ==> vm1: Configuring and enabling network interfaces...
    ==> vm1: Running provisioner: shell...
        vm1: Running: inline script
        vm1: Updating /etc/hosts
    ==> vm2: Importing base box 'rockylinux/9'...
    ==> vm2: Matching MAC address for NAT networking...
    ==> vm2: Checking if box 'rockylinux/9' version '6.0.0' is up to date...
    ==> vm2: Setting the name of the VM: vm2
    ==> vm2: Fixed port collision for 22 => 2222. Now on port 2200.
    ==> vm2: Clearing any previously set network interfaces...
    ==> vm2: Preparing network interfaces based on configuration...
        vm2: Adapter 1: nat
        vm2: Adapter 2: hostonly
    ==> vm2: Forwarding ports...
        vm2: 22 (guest) => 2200 (host) (adapter 1)
    ==> vm2: Running 'pre-boot' VM customizations...
    ==> vm2: Booting VM...
    ==> vm2: Waiting for machine to boot. This may take a few minutes...
        vm2: SSH address: 127.0.0.1:2200
        vm2: SSH username: vagrant
        vm2: SSH auth method: private key
        vm2: 
        vm2: Vagrant insecure key detected. Vagrant will automatically replace
        vm2: this with a newly generated keypair for better security.
        vm2: 
        vm2: Inserting generated public key within guest...
        vm2: Removing insecure key from the guest if it's present...
        vm2: Key inserted! Disconnecting and reconnecting using new SSH key...
    ==> vm2: Machine booted and ready!
    ==> vm2: Checking for guest additions in VM...
        vm2: No guest additions were detected on the base box for this VM! Guest
        vm2: additions are required for forwarded ports, shared folders, host only
        vm2: networking, and more. If SSH fails on this machine, please install
        vm2: the guest additions and repackage the box to continue.
        vm2:
        vm2: This is not an error message; everything may continue to work properly,
        vm2: in which case you may ignore this message.
    ==> vm2: Setting hostname...
    ==> vm2: Configuring and enabling network interfaces...
    ==> vm2: Running provisioner: shell...
        vm2: Running: inline script
        vm2: Updating /etc/hosts
    ==> vm3: Importing base box 'rockylinux/9'...
    ==> vm3: Matching MAC address for NAT networking...
    ==> vm3: Checking if box 'rockylinux/9' version '6.0.0' is up to date...
    ==> vm3: Setting the name of the VM: vm3
    ==> vm3: Fixed port collision for 22 => 2222. Now on port 2201.
    ==> vm3: Clearing any previously set network interfaces...
    ==> vm3: Preparing network interfaces based on configuration...
        vm3: Adapter 1: nat
        vm3: Adapter 2: hostonly
    ==> vm3: Forwarding ports...
        vm3: 22 (guest) => 2201 (host) (adapter 1)
    ==> vm3: Running 'pre-boot' VM customizations...
    ==> vm3: Booting VM...
    ==> vm3: Waiting for machine to boot. This may take a few minutes...
        vm3: SSH address: 127.0.0.1:2201
        vm3: SSH username: vagrant
        vm3: SSH auth method: private key
        vm3: 
        vm3: Vagrant insecure key detected. Vagrant will automatically replace
        vm3: this with a newly generated keypair for better security.
        vm3: 
        vm3: Inserting generated public key within guest...
        vm3: Removing insecure key from the guest if it's present...
        vm3: Key inserted! Disconnecting and reconnecting using new SSH key...
    ==> vm3: Machine booted and ready!
    ==> vm3: Checking for guest additions in VM...
        vm3: No guest additions were detected on the base box for this VM! Guest
        vm3: additions are required for forwarded ports, shared folders, host only
        vm3: networking, and more. If SSH fails on this machine, please install
        vm3: the guest additions and repackage the box to continue.
        vm3:
        vm3: This is not an error message; everything may continue to work properly,
        vm3: in which case you may ignore this message.
    ==> vm3: Setting hostname...
    ==> vm3: Configuring and enabling network interfaces...
    ==> vm3: Running provisioner: shell...
        vm3: Running: inline script
        vm3: Updating /etc/hosts
    
  3. 登录虚拟机

    PS C:\Users\许王伟\dev\vagrant\ambari> vagrant ssh vm1
    
  4. 免密登录配置

    这一步要在 vm1 上生成公私钥,并且将公钥内容拷贝到所有机器上。

    [vagrant@vm1 ~]$ ssh-keygen -t ed25519 -N "" -f ~/.ssh/id_ed25519
    
    输出
    Generating public/private ed25519 key pair.
    Your identification has been saved in /home/vagrant/.ssh/id_ed25519
    Your public key has been saved in /home/vagrant/.ssh/id_ed25519.pub
    The key fingerprint is:
    SHA256:PZwJeeUHAcsfzPkz9Yb+johIFU8S9bfObqV42bzqlR4 vagrant@vm1
    The key's randomart image is:
    +--[ED25519 256]--+
    |          oo=.   |
    |         o B +   |
    |        o * B o o|
    |         = O + +o|
    |        S B o =.o|
    |         . . .o+o|
    |        .    ..E.|
    |       . . ...B++|
    |        . . o++=+|
    +----[SHA256]-----+
    

    重要

    使用 Vagrant 创建的虚拟机,默认会创建 vagrant 用户,密码也是 vagrant。

    [vagrant@vm1 ~]$ ssh-copy-id vm1
    [vagrant@vm1 ~]$ ssh-copy-id vm2
    [vagrant@vm1 ~]$ ssh-copy-id vm3
    
    输出
    [vagrant@vm1 ~]$ ssh-copy-id vm1
    /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/vagrant/.ssh/id_ed25519.pub"
    The authenticity of host 'vm1 (127.0.1.1)' can't be established.
    ED25519 key fingerprint is SHA256:i//1kETsN3lwCU95O8Z1psRoShJVs6wQB/Wj8t2f2Rg.
    This key is not known by any other names
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    vagrant@vm1's password: 
    
    Number of key(s) added: 1
    
    Now try logging into the machine, with:   "ssh 'vm1'"
    and check to make sure that only the key(s) you wanted were added.
    
    [vagrant@vm1 ~]$ ssh-copy-id vm2
    /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/vagrant/.ssh/id_ed25519.pub"
    The authenticity of host 'vm2 (192.168.56.12)' can't be established.
    ED25519 key fingerprint is SHA256:6peCRMLEvhKhyQInArvlLMvvIAnJ7eoIqyxQ+EtZwU8.
    This key is not known by any other names
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    vagrant@vm2's password: 
    
    Number of key(s) added: 1
    
    Now try logging into the machine, with:   "ssh 'vm2'"
    and check to make sure that only the key(s) you wanted were added.
    
    [vagrant@vm1 ~]$ ssh-copy-id vm3
    /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/vagrant/.ssh/id_ed25519.pub"
    The authenticity of host 'vm3 (192.168.56.13)' can't be established.
    ED25519 key fingerprint is SHA256:HQgMsIMO57GeLNWi7Au5Lxpu5gL1b111BvPd5nsvVLA.
    This key is not known by any other names
    Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
    /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
    /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
    vagrant@vm3's password: 
    
    Number of key(s) added: 1
    
    Now try logging into the machine, with:   "ssh 'vm3'"
    and check to make sure that only the key(s) you wanted were added.
    
  5. 安装 Ansible

    [vagrant@vm1 ~]$ sudo dnf install ansible-core -y
    [vagrant@vm1 ~]$ ansible-galaxy collection install community.mysql
    
    输出
    [vagrant@vm1 ~]$ sudo dnf install ansible-core -y
    Last metadata expiration check: 1:05:51 ago on Sat 19 Jul 2025 11:28:47 AM UTC.
    Dependencies resolved.
    ========================================================================================================================================================================================================================================== Package                                                        Architecture                                     Version                                                        Repository                                           Size 
    ==========================================================================================================================================================================================================================================Installing:
    ansible-core                                                   x86_64                                           1:2.14.18-1.el9                                                appstream                                           2.2 M 
    Installing dependencies:
    git-core                                                       x86_64                                           2.47.1-2.el9_6                                                 appstream                                           4.7 M 
    python3-cffi                                                   x86_64                                           1.14.5-5.el9                                                   baseos                                              241 k 
    python3-cryptography                                           x86_64                                           36.0.1-4.el9                                                   baseos                                              1.2 M 
    python3-packaging                                              noarch                                           20.9-5.el9                                                     appstream                                            69 k 
    python3-ply                                                    noarch                                           3.11-14.el9.0.1                                                baseos                                              103 k 
    python3-pycparser                                              noarch                                           2.20-6.el9                                                     baseos                                              124 k 
    python3-pyparsing                                              noarch                                           2.4.7-9.el9                                                    baseos                                              150 k 
    python3-resolvelib                                             noarch                                           0.5.4-5.el9                                                    appstream                                            29 k 
    python3-setuptools                                             noarch                                           53.0.0-13.el9_6.1                                              baseos                                              837 k 
    sshpass                                                        x86_64                                           1.09-4.el9                                                     appstream                                            27 k 
    
    Transaction Summary
    ==========================================================================================================================================================================================================================================Install  11 Packages
    
    Total download size: 9.6 M
    Installed size: 44 M
    Downloading Packages:
    (1/11): python3-ply-3.11-14.el9.0.1.noarch.rpm                                                                                                                                                            624 kB/s | 103 kB     00:00     
    (2/11): python3-pycparser-2.20-6.el9.noarch.rpm                                                                                                                                                           723 kB/s | 124 kB     00:00     
    (3/11): python3-pyparsing-2.4.7-9.el9.noarch.rpm                                                                                                                                                          2.6 MB/s | 150 kB     00:00     
    (4/11): python3-cffi-1.14.5-5.el9.x86_64.rpm                                                                                                                                                              2.9 MB/s | 241 kB     00:00     
    (5/11): python3-cryptography-36.0.1-4.el9.x86_64.rpm                                                                                                                                                      4.0 MB/s | 1.2 MB     00:00     
    (6/11): python3-setuptools-53.0.0-13.el9_6.1.noarch.rpm                                                                                                                                                   9.8 MB/s | 837 kB     00:00     
    (7/11): ansible-core-2.14.18-1.el9.x86_64.rpm                                                                                                                                                              21 MB/s | 2.2 MB     00:00     
    (8/11): sshpass-1.09-4.el9.x86_64.rpm                                                                                                                                                                     349 kB/s |  27 kB     00:00     
    (9/11): python3-resolvelib-0.5.4-5.el9.noarch.rpm                                                                                                                                                         497 kB/s |  29 kB     00:00     
    (10/11): python3-packaging-20.9-5.el9.noarch.rpm                                                                                                                                                          1.0 MB/s |  69 kB     00:00     
    (11/11): git-core-2.47.1-2.el9_6.x86_64.rpm                                                                                                                                                                31 MB/s | 4.7 MB     00:00     
    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Total                                                                                                                                                                                                     4.8 MB/s | 9.6 MB     00:01     
    Running transaction check
    Transaction check succeeded.
    Running transaction test
    Transaction test succeeded.
    Running transaction
    Preparing        :                                                                                                                                                                                                                  1/1 
    Installing       : git-core-2.47.1-2.el9_6.x86_64                                                                                                                                                                                  1/11 
    Installing       : python3-resolvelib-0.5.4-5.el9.noarch                                                                                                                                                                           2/11 
    Installing       : sshpass-1.09-4.el9.x86_64                                                                                                                                                                                       3/11 
    Installing       : python3-setuptools-53.0.0-13.el9_6.1.noarch                                                                                                                                                                     4/11 
    Installing       : python3-pyparsing-2.4.7-9.el9.noarch                                                                                                                                                                            5/11 
    Installing       : python3-packaging-20.9-5.el9.noarch                                                                                                                                                                             6/11 
    Installing       : python3-ply-3.11-14.el9.0.1.noarch                                                                                                                                                                              7/11 
    Installing       : python3-pycparser-2.20-6.el9.noarch                                                                                                                                                                             8/11 
    Installing       : python3-cffi-1.14.5-5.el9.x86_64                                                                                                                                                                                9/11 
    Installing       : python3-cryptography-36.0.1-4.el9.x86_64                                                                                                                                                                       10/11 
    Installing       : ansible-core-1:2.14.18-1.el9.x86_64                                                                                                                                                                            11/11 
    Running scriptlet: ansible-core-1:2.14.18-1.el9.x86_64                                                                                                                                                                            11/11 
    Verifying        : python3-pycparser-2.20-6.el9.noarch                                                                                                                                                                             1/11 
    Verifying        : python3-cryptography-36.0.1-4.el9.x86_64                                                                                                                                                                        2/11 
    Verifying        : python3-ply-3.11-14.el9.0.1.noarch                                                                                                                                                                              3/1  
    Verifying        : python3-pyparsing-2.4.7-9.el9.noarch                                                                                                                                                                            4/11 
    Verifying        : python3-cffi-1.14.5-5.el9.x86_64                                                                                                                                                                                5/11 
    Verifying        : python3-setuptools-53.0.0-13.el9_6.1.noarch                                                                                                                                                                     6/11 
    Verifying        : ansible-core-1:2.14.18-1.el9.x86_64                                                                                                                                                                             7/11 
    Verifying        : sshpass-1.09-4.el9.x86_64                                                                                                                                                                                       8/11 
    Verifying        : python3-resolvelib-0.5.4-5.el9.noarch                                                                                                                                                                           9/11 
    Verifying        : python3-packaging-20.9-5.el9.noarch                                                                                                                                                                            10/11 
    Verifying        : git-core-2.47.1-2.el9_6.x86_64                                                                                                                                                                                 11/11 
    
    Installed:
    ansible-core-1:2.14.18-1.el9.x86_64  git-core-2.47.1-2.el9_6.x86_64        python3-cffi-1.14.5-5.el9.x86_64       python3-cryptography-36.0.1-4.el9.x86_64     python3-packaging-20.9-5.el9.noarch  python3-ply-3.11-14.el9.0.1.noarch  
    python3-pycparser-2.20-6.el9.noarch  python3-pyparsing-2.4.7-9.el9.noarch  python3-resolvelib-0.5.4-5.el9.noarch  python3-setuptools-53.0.0-13.el9_6.1.noarch  sshpass-1.09-4.el9.x86_64
    
    Complete!
    [vagrant@vm1 ~]$ ansible-galaxy collection install community.mysql
    Starting galaxy collection install process
    Process install dependency map
    Starting collection install process
    Downloading https://galaxy.ansible.com/api/v3/plugin/ansible/content/published/collections/artifacts/community-mysql-3.14.0.tar.gz to /home/vagrant/.ansible/tmp/ansible-local-258549blhlwr1/tmppwt8t97k/community-mysql-3.14.0-fl52zgm4
    Installing 'community.mysql:3.14.0' to '/home/vagrant/.ansible/collections/ansible_collections/community/mysql'
    community.mysql:3.14.0 was installed successfully
    

安装 Apache Ambari

  1. 创建 Ansilbe inventory

    在家目录下新建一个文件 inventory.ini。其实,随便哪个目录下都行,按照个人习惯来就好。

    inventory.ini
    [ambari-server]
    vm1
    
    [ambari-agent]
    vm1
    vm2
    vm3
    
  2. 创建 Ansible playbook

    在家目录下新建一个文件 playbook.yml。把内容复制粘贴进去就行,不要纠结内容,除非你对 Ansible 感兴趣,那可以看看。

    ---
    - name: Setup Ambari Agent
    hosts: all
    become: true
    tasks:
        - name: Disable and stop firewalld
        ansible.builtin.service:
            name: firewalld
            state: stopped
            enabled: false
    
        - name: Install Ambari Agent
        ansible.builtin.dnf:
            name:
            - https://apache-ambari.com/dist/ambari/3.0.0/rocky9/ambari/ambari-agent-3.0.0.0-0.x86_64.rpm
            - python3-distro
            - java-17-openjdk-devel
            - java-1.8.0-openjdk-devel
            state: present
            disable_gpg_check: true
    
    - name: Setup Ambari Server
    hosts: ambari-server
    vars:
        mysql_root_password: "MyNewPass4!"
        mysql_ambari_password: "AmbariPass4!"
        mysql_hive_password: "HivePass4!"
        mysql_ranger_password: "RangerPass4!"
        mysql_rangerkms_password: "RangerKMSPass4!"
        mysql_data_dir: /var/lib/mysql
        mysql_pid_dir: /var/run/mysqld
        mysql_log_path: /var/log/mysqld.log
    become: true
    tasks:
        - name: Disable and stop firewalld
        ansible.builtin.service:
            name: firewalld
            state: stopped
            enabled: false
    
        - name: Install Ambari Server
        ansible.builtin.dnf:
            name:
            - https://apache-ambari.com/dist/ambari/3.0.0/rocky9/ambari/ambari-server-3.0.0.0-0.x86_64.rpm
            - python3-psycopg2
            - python3-PyMySQL
            - https://mirrors.ustc.edu.cn/mysql-repo/mysql80-community-release-el9.rpm
            state: present
            disable_gpg_check: true
    
        - name: Install MySQL community server
        ansible.builtin.dnf:
            name: mysql-community-server
            state: present
            disable_gpg_check: true
    
        - name: Make sure MySQL directories exist
        ansible.builtin.file:
            path: "{{ item }}"
            owner: mysql
            group: mysql
            recurse: true
            state: directory
        loop:
            - "{{ mysql_pid_dir }}"
            - "{{ mysql_data_dir }}"
    
        - name: Make sure MySQL log file exists
        ansible.builtin.file:
            path: "{{ mysql_log_path }}"
            owner: mysql
            group: mysql
            state: touch
    
        - name: Initialize MySQL datadir
        ansible.builtin.shell:
            cmd: "mysqld --initialize-insecure --user=mysql"
            creates: "/var/lib/mysql/mysql"
    
        - name: Start and enable MySQL service
        ansible.builtin.systemd:
            name: mysqld
            state: started
            enabled: true
    
        - name: Set MySQL root password
        community.mysql.mysql_user:
            name: root
            plugin: caching_sha2_password
            plugin_auth_string: "{{ mysql_root_password }}"
            password_expire: never
    
        - name: Create user 'ambari'@'localhost'
        community.mysql.mysql_user:
            login_user: root
            login_password: "{{ mysql_root_password }}"
            name: ambari
            plugin: caching_sha2_password
            plugin_auth_string: "{{ mysql_ambari_password }}"
            host: "localhost"
            priv: "*.*:ALL"
    
        - name: Create user 'ambari'@'%'
        community.mysql.mysql_user:
            login_user: root
            login_password: "{{ mysql_root_password }}"
            name: ambari
            plugin: caching_sha2_password
            plugin_auth_string: "{{ mysql_ambari_password }}"
            host: "%"
            priv: "*.*:ALL"
    
        - name: Create Ambari database
        community.mysql.mysql_db:
            login_user: root
            login_password: "{{ mysql_root_password }}"
            name:
            - ambari
            - hive
            - ranger
            - rangerkms
            state: present
    
        - name: Create user 'hive'@'%'
        community.mysql.mysql_user:
            login_user: root
            login_password: "{{ mysql_root_password }}"
            name: hive
            plugin: caching_sha2_password
            plugin_auth_string: "{{ mysql_hive_password }}"
            host: "%"
            priv: "hive.*:ALL"
    
        - name: Create user 'ranger'@'%'
        community.mysql.mysql_user:
            login_user: root
            login_password: "{{ mysql_root_password }}"
            name: ranger
            plugin: caching_sha2_password
            plugin_auth_string: "{{ mysql_ranger_password }}"
            host: "%"
            priv: "*.*:ALL,GRANT"
    
        - name: Create user 'rangerkms'@'%'
        community.mysql.mysql_user:
            login_user: root
            login_password: "{{ mysql_root_password }}"
            name: rangerkms
            plugin: caching_sha2_password
            plugin_auth_string: "{{ mysql_rangerkms_password }}"
            host: "%"
            priv: "rangerkms.*:ALL"
    
        - name: Init ambari database
        community.mysql.mysql_db:
            login_user: ambari
            login_password: "{{ mysql_ambari_password }}"
            state: import
            name: ambari
            target: /var/lib/ambari-server/resources/Ambari-DDL-MySQL-CREATE.sql
    
        - name: Install mysql-connector-j
        ansible.builtin.dnf:
            name: mysql-connector-j
            state: present
            disable_gpg_check: true
    
        - name: Setup JDBC driver
        ansible.builtin.shell: ambari-server setup --jdbc-db=mysql --jdbc-driver=/usr/share/java/mysql-connector-java.jar
    
        - name: Configure MySQL 8 compatibility
        ansible.builtin.lineinfile:
            path: /etc/ambari-server/conf/ambari.properties
            regexp: ^server\.jdbc\.url=
            line: server.jdbc.url=jdbc:mysql://localhost:3306/ambari?useSSL=true&verifyServerCertificate=false&enabledTLSProtocols=TLSv1.2
    
        - name: Configure Ambari server
        ansible.builtin.shell: > 
            ambari-server setup -s \
            -j /usr/lib/jvm/java-1.8.0-openjdk \
            --ambari-java-home /usr/lib/jvm/java-17-openjdk \
            --database=mysql \
            --databasehost=localhost \
            --databaseport=3306 \
            --databasename=ambari \
            --databaseusername=ambari \
            --databasepassword={{ mysql_ambari_password }}
    
        - name: Add systemd wrapper for ambari-server
        copy:
            dest: /etc/systemd/system/ambari-server.service
            content: |
            [Unit]
            Description=Ambari Server Compatibility Wrapper
            After=network.target
    
            [Service]
            Type=forking
            ExecStart=/etc/init.d/ambari-server start
            ExecStop=/etc/init.d/ambari-server stop
            ExecReload=/etc/init.d/ambari-server restart
            PIDFile=/var/run/ambari-server/ambari-server.pid
    
            [Install]
            WantedBy=multi-user.target
    
        - name: Enable and start Ambari Server
        ansible.builtin.service:
            name: ambari-server
            state: started
            enabled: true
            daemon_reload: true
    
    - name: Setup Ambari Agent
    hosts: all
    become: true
    tasks:
        - name: Edit ambari-agent configuration
        ansible.builtin.lineinfile:
            path: /etc/ambari-agent/conf/ambari-agent.ini
            regexp: '^hostname='
            line: hostname={{ groups['ambari-server'][0] }}
    
        - name: Add systemd wrapper for ambari-agent
        copy:
            dest: /etc/systemd/system/ambari-agent.service
            content: |
            [Unit]
            Description=Ambari Agent Compatibility Wrapper
            After=network.target
    
            [Service]
            Type=forking
            ExecStart=/etc/init.d/ambari-agent start
            ExecStop=/etc/init.d/ambari-agent stop
            ExecReload=/etc/init.d/ambari-agent restart
            PIDFile=/var/run/ambari-agent/ambari-agent.pid
    
            [Install]
            WantedBy=multi-user.target
    
        - name: Enable and start Ambari Agent
        ansible.builtin.service:
            name: ambari-agent
            state: started
            enabled: true
            daemon_reload: true
    
  3. 真一键安装

    [vagrant@vm1 ~]$ ansible-playbook -i inventory.ini playbook.yml
    
    输出
    [WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details
    
    PLAY [Setup Ambari Agent] ****************************************************************************************************************************************************************************************************************
    TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************
    ok: [vm1]
    ok: [vm2]
    ok: [vm3]
    
    TASK [Disable and stop firewalld] ********************************************************************************************************************************************************************************************************
    ok: [vm3]
    ok: [vm1]
    ok: [vm2]
    
    TASK [Install Ambari Agent] **************************************************************************************************************************************************************************************************************
    changed: [vm3]
    changed: [vm2]
    changed: [vm1]
    
    PLAY [Setup Ambari Server] ***************************************************************************************************************************************************************************************************************
    TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************
    ok: [vm1]
    
    TASK [Disable and stop firewalld] ********************************************************************************************************************************************************************************************************
    ok: [vm1]
    
    TASK [Install Ambari Server] *************************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Install MySQL community server] ****************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Make sure MySQL directories exist] *************************************************************************************************************************************************************************************************
    ok: [vm1] => (item=/var/run/mysqld)
    ok: [vm1] => (item=/var/lib/mysql)
    
    TASK [Make sure MySQL log file exists] ***************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Initialize MySQL datadir] **********************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Start and enable MySQL service] ****************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Set MySQL root password] ***********************************************************************************************************************************************************************************************************
    [WARNING]: Option column_case_sensitive is not provided. The default is now false, so the column's name will be uppercased. The default will be changed to true in community.mysql 4.0.0.
    changed: [vm1]
    
    TASK [Create user 'ambari'@'localhost'] **************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Create user 'ambari'@'%'] **********************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Create Ambari database] ************************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Create user 'hive'@'%'] ************************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Create user 'ranger'@'%'] **********************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Create user 'rangerkms'@'%'] *******************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Init ambari database] **************************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Install mysql-connector-j] *********************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Setup JDBC driver] *****************************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Configure MySQL 8 compatibility] ***************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Configure Ambari server] ***********************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Add systemd wrapper for ambari-server] *********************************************************************************************************************************************************************************************
    changed: [vm1]
    
    TASK [Enable and start Ambari Server] ****************************************************************************************************************************************************************************************************
    changed: [vm1]
    
    PLAY [Setup Ambari Agent] ****************************************************************************************************************************************************************************************************************
    TASK [Gathering Facts] *******************************************************************************************************************************************************************************************************************
    ok: [vm1]
    ok: [vm2]
    ok: [vm3]
    
    TASK [Edit ambari-agent configuration] ***************************************************************************************************************************************************************************************************
    changed: [vm3]
    changed: [vm2]
    changed: [vm1]
    
    TASK [Add systemd wrapper for ambari-agent] **********************************************************************************************************************************************************************************************
    changed: [vm3]
    changed: [vm2]
    changed: [vm1]
    
    TASK [Enable and start Ambari Agent] *****************************************************************************************************************************************************************************************************
    changed: [vm2]
    changed: [vm3]
    changed: [vm1]
    
    PLAY RECAP *******************************************************************************************************************************************************************************************************************************
    vm1                        : ok=29   changed=23   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    vm2                        : ok=7    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    vm3                        : ok=7    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    

验证

在宿主机上打开浏览器,输入 http://192.168.56.11:8080 后回车就可以看到 Web 管理页面。用户名和密码都是 admin。

Web

销毁实验环境

在虚拟机中输入 Ctrl + D 退出登录,然后在宿主机上的工作目录下执行下面的命令会删除所有实验的虚拟机。

PS C:\Users\许王伟\dev\vagrant\ambari> vagrant destroy -f

总结

我们通过 Vagrant 快速创建了 3 台虚拟机,这 3 台虚拟机由 Vagrantfile 文件来定义。

接着,我们通过 vagrant ssh vm1 命令远程登录 vm1 虚拟机,以 vagrant 用户的身份,我们用 ssh-keygen 命令生成了公私钥,并通过 ssh-copy-id 将公钥拷贝到了所有主机,实现了从 vm1 虚拟机上以 vagrant 用户身份免密登录所有虚拟机的需求。

然后,在 vm1 虚拟机上,我们创建了 inventory.ini 和 playbook.yml 文件,并用 ansible-play 命令一键安装了 ambari-server 和 ambari-agent。

最后,我们在宿主机上,使用浏览器打开了 Ambari 的 Web 管理页面。

最后的最后,还学会了使用 vagrant destroy -f 命令销毁所有虚拟机。


  1. ambari-server 是老大,只有一个,负责给小弟下命令和给用户提供 Web 界面。 

  2. ambari-agent 是小弟,负责真正干活,坚守在集群中的每一台机器上。