天天看點

企業運維----自動化運維工具-Ansible(一)安裝部署

Ansible(一):Ansible的安裝部署

      • 1.Ansible簡介
      • 2.Ansible特點
      • 3.Ansible安裝部署
        • 環境
        • 安裝
      • 4.建構使用者級ansible操作環境
        • 編輯清單
        • ssh免密認證
        • 為所有受控主機建立devops
        • sudo授權:

1.Ansible簡介

  1. ansible是新出現的自動化運維工具,基于Python開發,集合了衆多運維工具(puppet、chef、func、fabric)的優點,實作了批量系統配置、批量程式部署、批量運作指令等功能。
  2. ansible是基于 paramiko 開發的,并且基于子產品化工作,本身沒有批量部署的能力。真正具有批量部署的是ansible所運作的子產品,ansible隻是提供一種架構。ansible不需要在遠端主機上安裝client/agents,因為它們是基于ssh來和遠

    程主機通訊的。

2.Ansible特點

  1. 部署簡單,隻需在主要端部署Ansible環境,被控端無需做任何操作;
  2. 預設使用SSH協定對裝置進行管理;
  3. 有大量正常運維操作子產品,可實作日常絕大部分操作;
  4. 配置簡單、功能強大、擴充性強;
  5. 支援API及自定義子產品,可通過Pyhon輕松擴充;
  6. 通過Playbooks來定制強大的配置、狀态管理;
  7. 輕量級,無需在用戶端安裝agent,更新時,隻需在操作機上進行一次更新即可;
  8. 提供一個功能強大、操作性強的Web管理界面和REST API接口——AWX平台。

3.Ansible安裝部署

環境

準備rhel8.2的主機和虛拟機,也可以開4台虛拟機。配置私有網段

主機名:ip role
真機:172.25.41.250 ansible
westosa:172.25.41.1 node
westosb:172.25.41.2 node
westosc:172.25.41.3 node

安裝

配置好本地網絡倉庫

安裝epel源後dnf安裝ansible

dnf install epel-release -y
dnf install ansible –y
           

檢視版本資訊

[[email protected]_student41 ansible]$ ansible --version 
ansible 2.9.23
  config file = /home/devops/ansible/ansible.cfg
  configured module search path = ['/home/devops/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Dec  5 2019, 15:45:45) [GCC 8.3.1 20191121 (Red Hat 8.3.1-5)]

           

4.建構使用者級ansible操作環境

在生産環境中一般不使用root。建立使用者devops并登陸

[[email protected]_student41 ~]# useradd  devops
[[email protected]_student41 ~]# su - devops
Last login: Thu Aug 26 09:44:38 CST 2021 on pts/0
           

在devops下建立一個ansible目錄,複制ansible配置檔案到目錄并且修改配置。

為了實驗友善删除一些實驗用不到的内容。

[[email protected]_student41 ~]$ mkdir ansible
[[email protected]_student41 ~]$ cd ansible/
[[email protected]_student41 ansible]$ cp /etc/ansible/ansible.cfg .
[[email protected]_student41 ansible]$ ls
ansible.cfg
[[email protected]_student41 ansible]$ vim ansible.cfg 
[[email protected]_student41 ansible]$ cat ansible.cfg 
# config file for ansible -- https://ansible.com/
# ===============================================

# nearly all parameters can be overridden in ansible-playbook
# or with command line flags. ansible will read ANSIBLE_CONFIG,
# ansible.cfg in the current working directory, .ansible.cfg in
# the home directory or /etc/ansible/ansible.cfg, whichever it
# finds first

[defaults]

# some basic default values...

inventory      = ~/ansible/inventory
#roles_path    = /etc/ansible/roles
host_key_checking = False
remote_user = devops
module_name = shell

[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
           

編輯清單

添加受控主機ip(或者主機名)

[[email protected]_student41 ansible]$ vim inventory 
[[email protected]_student41 ansible]$ cat inventory 
[westos]
172.25.41.1
[linux]
172.25.41.1
172.25.41.3
[prod]
172.25.41.2
           

分組測試

[[email protected]_student41 ansible]$ ansible all --list
  hosts (3):
    172.25.41.1
    172.25.41.3
    172.25.41.2
[[email protected]_student41 ansible]$ ansible linux  --list
  hosts (2):
    172.25.41.1
    172.25.41.3
           

ssh免密認證

[[email protected]_student41 ansible]$ ssh-keygen 
[[email protected]_student41 ansible]$ for i in 1 2 3 ; do ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected].$i; done
           

為所有受控主機建立devops

[[email protected]_student41 ~]# openssl passwd -6
Password: 
Verifying - Password: 
$6$EWhfM1Ds4rxJhHvu$vgFeXV20C.20sBaXsHwCxSMEsVRl1pIMBBiAKIS2Zc92OsGleGVP6c5zfXKBY.elzv.l84.ga8rNY305DQQ2T0


[[email protected]_student41 ansible]$  ansible all -m user -a 'name=devops state=present password="$6$EWhfM1Ds4rxJhHvu$vgFeXV20C.20sBaXsHwCxSMEsVRl1pIMBBiAKIS2Zc92OsGleGVP6c5zfXKBY.elzv.l84.ga8rNY305DQQ2T0"' -u root
           

sudo授權:

[[email protected]_student41 ansible]$ ansible all -m lineinfile -a 'path=/etc/sudoers line="devops ALL=(ALL) NOPASSWD: ALL "' -u root
[[email protected]_student41 ansible]$ ansible all -m shell -a 'tail -n 3 /etc/sudoers'

172.25.41.1 | CHANGED | rc=0 >>
## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d
devops ALL=(ALL) NOPASSWD: ALL 
172.25.41.2 | CHANGED | rc=0 >>
## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d
devops ALL=(ALL) NOPASSWD: ALL 
172.25.41.3 | CHANGED | rc=0 >>
## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d
devops ALL=(ALL) NOPASSWD: ALL