<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # ansible部署和實踐 ## 1 介紹和部署 ### 1.1 介紹 1) `ansible`的功能 ansible是一個基于Python開發的自動化運維工具,基于ssh協議實現遠程管理,可以實現多種批量管理操作. * 批量系統配置 * 批量軟件部署 * 批量文件拷貝 * 批量運行命令 2) 批量管理服務特征 * 管理端:不需要啟動任何服務,默認服務端不需要任何的配置 * 受控端:基于ssh免秘鑰,沒有客戶端軟件需要安裝 ### 1.2 `ansible`軟件安裝部署 1) 環境規劃 | 主機名 | IP地址 | 用途 | | ------ | --------- | ------ | | m01 | 10.0.0.61 | 管理段 | | backup | 10.0.0.41 | 受控端 | | nfs01 | 10.0.0.31 | 受控端 | | web01 | 10.0.0.7 | 受控端 | 2) 免秘鑰配置 生成秘鑰對 ```sh ssh-keygen -t dsa ``` 分發秘鑰 ```sh sh-copy-id -i /root/.ssh/id_dsa.pub 172.16.1.7 sh-copy-id -i /root/.ssh/id_dsa.pub 172.16.1.31 sh-copy-id -i /root/.ssh/id_dsa.pub 172.16.1.41 ``` 免交互批量分發秘鑰參考我的批量免秘鑰分發腳本 3) 口令登錄方式配置[備選] 如果企業限制不允許使用ssh免秘鑰方式,也可以使用在`ansible`的主機配置中,制定遠程主機用戶名和密碼的方式 ```sh [root@xxxx ~]# vim /etc/ansible/hosts [noah] 172.16.1.7 172.16.1.31 ansible_user=root ansible_password=123456 172.16.1.41 ``` > 要先按裝了`ansibl`軟件,才會有該配置文件 4) 安裝`ansible` 軟件安裝 ```sh yum install -y ansible ``` 若受控主機有啟用selinux,會影響ansible鏈接,在不能禁用的情況下,客戶機可以安裝以下程序 ```sh yum install -y libselinux-python ``` * 主機列表創建 ```sh cp /etc/ansible/hosts{,.bak} cat >/etc/ansible/hosts <<"EOF" [noah] 172.16.1.7 172.16.1.31 172.16.1.41 EOF ``` ### 1.3 用法說明 1) 語法 ```sh ansible 主機信息 -m 模塊名稱 -a "相關模塊參數"" 主機信息:遠程主機IP地址,主機組名稱,all代表所有主機 -m:指定使用哪個模塊 -a:模塊中的參數和功能 ``` 簡單示例:ping模塊 ```sh [root@xxxx ~]# ansible all -m ping 172.16.1.41 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.1.7 | SUCCESS => { "changed": false, "ping": "pong" } 172.16.1.31 | SUCCESS => { "changed": false, "ping": "pong" } ``` ping模塊沒有參數,不用跟-e 2) 返回顏色 `ansible`會根據返回結果的類型不同,顯示不同的顏色 * 綠色:查看遠程主機信息,不會對遠程主機系統做任何修改 * 紅色:執行操作出現異常錯誤 * 黃色:對遠程主機系統進行修改操作 * 粉色:警告或者忠告信息 ## 2 命令類常用模塊 ansible模塊列表 https://docs.ansible.com/ansible/latest/modules/modules_by_category.html ### 2.1 command[命令模塊] 1) 官方鏈接: http://docs.ansible.com/ansible/latest/modules/command_module.html 說明:該模塊與shell模塊類似,但不能識別特殊符號 2) 常用參數: ```sh free_form [必須]表示執行command模塊時,必須要有linux合法命令信息,如ls chdir 在執行某個命令前,先切換目錄 creates 判斷一個文件是否存在,如果已經存在了,后面的命令就不會執行 removes 判斷一個文件是否存在,如果不存在,后面的命令就不會執行 ``` 3) 舉例 * chdir參數 ```sh [root@xxxx ~]# ansible 172.16.1.31 -m command -a "chdir=/tmp/ pwd" 172.16.1.31 | SUCCESS | rc=0 >> /tmp ``` * creates參數 ```sh [root@xxxx ~]# ansible 172.16.1.41 -m command -a "creates=/etc/rsyncd.conf hostname" 172.16.1.41 | SUCCESS | rc=0 >> skipped, since /etc/rsyncd.conf exists [root@xxxx ~]# ansible 172.16.1.41 -m command -a "creates=/etc/rsyncd.123456 hostname" 172.16.1.41 | SUCCESS | rc=0 >> backup ``` * removes參數 ```sh [root@xxxx ~]# ansible 172.16.1.41 -m command -a "removes=/etc/rsyncd.conf hostname" 172.16.1.41 | SUCCESS | rc=0 >> backup [root@xxxx ~]# ansible 172.16.1.41 -m command -a "removes=/etc/rsyncd.1212213123 hostname" 172.16.1.41 | SUCCESS | rc=0 >> skipped, since /etc/rsyncd.1212213123 does not exist ``` ### 2.2 shell[萬能模塊] 1) 官方鏈接 https://docs.ansible.com/ansible/latest/modules/shell_module.html#shell-module 說明:shell模塊可以滿足command模塊所有功能,并且可以支持識別特殊字符信息 < > | ; 2) 常用參數 ```sh free_form [必須]表示執行command模塊時,必須要有linux合法命令信息,如ls chdir 在執行莫個命令前,先切換目錄 creates 判斷一個文件是否存在,如果已經存在了,后面的命令就不會執行 removes 判斷一個文件是否存在,如果不存在,后面的命令就不會執行 ``` 3) 舉例 ```sh [root@xxxx ~]# ansible 172.16.1.41 -m shell -a "hostname;pwd" 172.16.1.41 | SUCCESS | rc=0 >> backup /root ``` 可以使用該名模,執行所有linux的命令,所以叫萬能模塊 ### 2.3 script[腳本模塊] 1) 官方連接 https://docs.ansible.com/ansible/latest/modules/script_module.html#script-module 2) 常用參數 ```sh free_form [必須]表示執行command模塊時,必須要有linux合法命令信息,如ls chdir 在執行莫個命令前,先切換目錄 creates 判斷一個文件是否存在,如果已經存在了,后面的命令就不會執行 removes 判斷一個文件是否存在,如果不存在,后面的命令就不會執行 ``` 3) 舉例 * 先查看腳本 ```sh [root@xxxx ~]# cat /server/scripts/mk.sh #!/bin/sh mkdir -p /root/abc/def touch /root/abc/123.txt ls -lh /root/abc/* ``` * 在調用這個腳本遠程執行 ```sh [root@xxxx ~]# ansible 172.16.1.7 -m script -a "/server/scripts/mk.sh" 172.16.1.7 | SUCCESS => { "changed": true, "rc": 0, "stderr": "Shared connection to 172.16.1.7 closed.\r\n", "stdout": "-rw-r--r-- 1 root root 0 6月 18 17:05 /root/abc/123.txt\r\n\r\n/root/abc/def:\r\n總用量 0\r\n", "stdout_lines": [ "-rw-r--r-- 1 root root 0 6月 18 17:05 /root/abc/123.txt", "", "/root/abc/def:", "總用量 0" ] } ``` ### 3 文件類常用模塊 ### 3.1 copy[復制模塊] 1) 官方鏈接: https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module 2) 常用參數 ```sh backup 對數據信息進行備份 src 定義要推送數據信息 dest [必須]定義將數據推送到遠程主機什么目錄中 owner 設置復制后的文件屬主權限 group 設置復制后的文件屬組權限 mode 設置復制后的文件權限(600 755) ``` 3) 舉例 * backup參數 ```sh ansible 172.16.1.41 -m copy -a "src=/tmp/01.txt dest=/tmp/ backup=yes" ``` * src,dest參數 ```sh ansible 172.16.1.41 -m copy -a "src=/tmp/01.txt dest=/tmp/" ``` ### 3.2 file[文件操作模塊] 1) 官方鏈接 https://docs.ansible.com/ansible/latest/modules/file_module.html#file-module 2) 常見參數 ```sh src 定義要推送數據信息 dest [必須]定義將數據推送到遠程主機什么目錄中 owner 設置文件屬主權限 group 設置文件屬組權限 mode 設置文件權限(600 755) state 用于指定創建目錄或文件 ``` 3) 舉例 * 權限參數 ```sh ansible 172.16.1.7 -m file -a "dest=/tmp/01.txt owner=oldboy group=oldboy mode=600" ``` * state創建文件 ```sh ansible 172.16.1.41 -m file -a "dest=/tmp/02.txt state=touch" ``` * state創建目錄 ```sh ansible 172.16.1.41 -m file -a "dest=/tmp/01dir state=directory" ``` ### 4 包管理.系統服務管理.定時任務模塊 ### 4.1 yum[包管理模塊] 1) 官方鏈接 https://docs.ansible.com/ansible/latest/modules/yum_module.html#yum-module 2) 常用參數 ```sh name [必須]執行要安裝軟件的名稱,以及軟件的版本 state installed安裝 absent(卸載) list 指定軟件名稱,查看軟件是否可以安裝,以及是否安裝過 ``` 3) 舉例 ```sh ansible 172.16.1.41 -m yum -a "name=iftop state=installed" ansible 172.16.1.41 -m yum -a "name=iftop state=absent" ansible 172.16.1.41 -m yum -a "list=iftop" ``` ### 4.2 service[系統服務管理模塊] 1) 官方鏈接 https://docs.ansible.com/ansible/latest/modules/service_module.html#service-module 2) 常用參數 ```sh name [必須]指定要管理的服務名稱(管理的服務一定在chkconfig中可以看到) state stopped started restarted reloaded enabled yes表示服務開機自啟動 no表示服務開機不要自動啟動 ``` 3) 舉例 ```sh ansible 172.16.1.41 -m service -a "name=crond state=started enabled=yes" ``` ### 4.3 cron[定時任務模塊] 1) 官方鏈接 https://docs.ansible.com/ansible/latest/modules/cron_module.html#cron-module 2) 常用參數 ```sh minute 分,寫法同系統定時任務,如[0-59] [*] [*/n] hour 時,寫法同上 day 日,寫法同上 month 月,寫法同上 weekday 周,寫法同上 job 執行命令,如job='/bin/sh /server/scripts/test.sh &>/dev/null' ``` 3) 舉例 * 添加定時任務 ```sh ansible 172.16.1.41 -m cron -a "minute=0 hour=0 job='/bin/sh /server/scripts/test.sh &>/dev/null'" ansible 172.16.1.41 -m cron -a "name=oldboy02 minute=0 hour=0 job='/bin/sh /server/scripts/test.sh &>/dev/null'" ``` * 刪除定時任務 ```sh ansible 172.16.1.41 -m cron -a "name=oldboy02 minute=0 hour=0 job='/bin/sh /server/scripts/test.sh &>/dev/null' state=absent" ansible 172.16.1.41 -m cron -a "name=oldboy01 state=absent" ``` * 注釋定時任務 ```sh ansible 172.16.1.41 -m cron -a "name=oldboy01 minute=0 hour=0 job='/bin/sh /server/scripts/test.sh &>/dev/null' disabled=yes"a ansible 172.16.1.41 -m cron -a "name=oldboy01 job='/bin/sh /server/scripts/test.sh &>/dev/null' disabled=no" ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看