1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        自動化運維工具Ansible實戰(zhàn)

        共 6892字,需瀏覽 14分鐘

         ·

        2021-06-23 01:29

        Ansible描述

        官網(wǎng):https://docs.ansible.com

        ansible是新出現(xiàn)的自動化運維工具,基于Python開發(fā),集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優(yōu)點,實現(xiàn)了批量系統(tǒng)配置、批量程序部署、批量運行命令等功能。

        ansible是基于模塊工作的,本身沒有批量部署的能力。真正具有批量部署的是ansible所運行的模塊,ansible只是提供一種框架。

        Ansible特點
        • 模塊化:調用特定的模塊,完成特定任務
        • 有Paramiko,PyYAML,Jinja2(模板語言)三個關鍵模塊
        • 支持自定義模塊
        • 基于Python語言實現(xiàn)
        • 部署簡單,基于python和SSH(默認已安裝),agentless
        • 安全,基于OpenSSH
        • 支持playbook編排任務
        • 冪等性:一個任務執(zhí)行1遍和執(zhí)行n遍效果一樣,不因重復執(zhí)行帶來意外情況
        • 無需代理不依賴PKI(無需ssl)
        • 可使用任何編程語言寫模塊
        • YAML格式,編排任務,支持豐富的數(shù)據(jù)結構
        • 較強大的多層解決方案
        準備實驗環(huán)境
        服務器名稱服務器ip
        master192.168.1.20
        node01192.168.1.21
        node02192.168.1.22
        1.0各節(jié)點配置本地hosts
        • 方便后期配置服務
        cat    << EOF >> /etc/hosts
        192.168.1.20       master
        192.168.1.21       node01
        192.168.1.22       node02
        EOF
        2.0 master節(jié)點安裝ansible并配置
        yum -y install  ansible

        ansible主配置常見說明

        cat               /etc/ansible/ansible.cfg
        inventory                 = /etc/ansible/host   #ansible主機管理清單
        forks                      = 5  #并發(fā)數(shù)量
        sudo_user                 = root  #提權
        remote_port               = 22   #操作主機的端口
        host_key_checking         = False  #第一次交互目標主機,需要輸入yes/no,改成False不用輸入
        timeout                   = 10    #連接主機的超時時間
        log_path = /var/log/ansible.log    #設置日志路徑
        private_key_file = /root/.ssh/id_rsa  #指定認證密鑰的私鑰

        ansible hosts主機清單配置文件說明

        根據(jù)不同服務類型可以對主機清單進行分類,例如web/db等

        驗證ansible對主機的遠程操作

        2.1 對分組的主機操作
        ansible web   -a  "df -h"  #查看web分組磁盤使用情況
        2.2對分組內主機操作,精確匹配
        2.3 對所有主機操作all

        官方文檔https://docs.ansible.com/ansible/latest/index.html

        2.4 定義變量,并驗證
        mkdir /etc/ansible/group_vars/
        cat  /etc/ansible/group_vars/web.yml #文件命名和組名一致
        http_port: 8090
        server_name: www.baidu.com

        打印變量

        ansible web   -a "echo {{server_name}}"
        ansible 常見的選項
        -vvv 打印詳細輸出
        ansible web -vvv -a 'cat  /tmp/123'
        -f 指定并發(fā)數(shù)
        ansible web -vvv -a 'cat  /tmp/123'  -f 10
        -i 指定別的位置的主機清單hosts
        ansible web -a  'cat  /tmp/123'  -i /root/hosts
        -m 指定模塊
        ansible  web   -m  shell -a  'echo hello-zhangfan >>  /tmp/123'
        3.0 配置免密登錄認證
        #生成密鑰
        ssh-keygen
        將密鑰發(fā)送到目標主機
        ssh-copy-id  [email protected]
        ssh-copy-id  [email protected]
        3.1 修改主機清單文件,并測試
        cat  /etc/ansible/hosts
        ansible web   -a  "ls  /tmp"

        驗證成功

        4.0 ansible常用的模塊
        - 執(zhí)行shell命令  (command和shell)
        - 文件傳輸  (copy和file)
        - 管理軟件包 (yum )
        - 用戶和組(user)
        - 從源代碼管理系統(tǒng)部署(git)
        - 管理服務(service)
        - 目標主機信息(setup)
        4.1 使用shell模塊,sudo提權測試
        #node01,node02執(zhí)行
        useradd   zhangfan 
        echo 123456  |passwd --stdin zhangfan
        #node02配置sudo權限
        vim  /etc/sudoers
        • node01不配置sudo權限

        master測試

        ansible  web   -m  shell  -a 'ls /root'   -u  zhangfan -k --become  --become-user root -K
        ansible  web 
        -m  shell \           #指定模塊
        -a 'ls /root' \         #執(zhí)行的命令
        -u  zhangfan  -k \ 指定遠程連接的用戶并輸入密碼
        --become  --become-user root \ 提權的用戶
        -K #sudo提權用戶輸入的密碼
        ```bash

        ![](https://img-blog.csdnimg.cn/20210530232429448.png)

        發(fā)現(xiàn)提權的node02節(jié)點正常node01權限不足

        ##### 4.2 使用copy模塊

        ```bash
        ansible  web   -m copy -a  "src=/root/nginx-1.12.tar  dest=/tmp"   -u root
        4.2 使用file模塊
        ansible  web  -m  file  -a "dest=/opt/hello  mode=600 state=directory" -u root

        文件狀態(tài)

        absent          #卸載/刪除
        directory       #目錄
        file,           #文件
        hard            #硬連接
        link            #軟連接
        touch           #空文件

        說明創(chuàng)建目錄成功

        刪除目錄

        ansible  web  -m  file  -a "dest=/opt/hello   state=absent" 

        創(chuàng)建一個文件

        ansible  web  -m  file  -a "dest=/opt/hello mode=755  state=touch"
        4.3 Yum模塊
        狀態(tài)
        absent  #卸載
        present  #安裝
        ansible web   -m yum  -a  'name=memcached  state=present'
        ansible web   -m yum  -a  'name=memcached  state=absent'
        4.4 創(chuàng)建用戶模塊
         ansible  web   -m user  -a   "name=lisi password=123.com"
         ansible  web   -m user  -a   "name=php password=123456  shell=/sbin/nologin"  #指定shell創(chuàng)建

        刪除用戶

        ansible  web   -m user  -a   "name=lisi  state=absent"
        4.5 git 模塊

        需要提前創(chuàng)建一個空目錄

        ansible  web -m  git -a "repo=https://github.com/ansible/ansible.git  dest=/mnt/ansible"
        4.6 service模塊

        啟動nginx服務

         ansible web  -m  service  -a "name=nginx state=started"

        加入開機自啟

        ansible web  -m  service  -a "name=nginx enabled=true"
        4.7 setup模塊獲取相應模塊信息

        打印所有信息

        ansible  web   -m  setup

        獲取系統(tǒng)版本

        ansible  web   -m  setup  -a  "filter=ansible_os_family*"

        獲取內存信息

        ansible  web   -m  setup  -a  "filter=ansible_*_mb"

        文章來自原創(chuàng),轉載請授權

        (版權歸原作者所有,侵刪)


        點擊下方“閱讀原文”查看更多

        瀏覽 85
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            日本香蕉网 | 码人妻免费视频 | 美女操屄视频 | 日本一级淫片视频三爬 | 一级片免费的 | 亚洲成人18 | 91承认视频在线观看 | 四虎影院黄片 | 日日操人人 | 纲手被吸乳歪歪漫画 |