Centos7安裝ansible自動運維


一、安裝ansible軟件

1、安裝yum源

rpm -Uvh http://mirrors.ustc.edu.cn/epel/epel-release-latest-7.noarch.rpm

yum install epel-release -y

yum install ansible

要是報錯:epel源與python版本沖突原因,有些包是需要依賴python2.6的版本,此主機的python版本是2.7.5。

2、那就先卸載 epel-release源

yum  install  epel-release  -y

3、到 /etc/yum.repos.d 目錄下,將epel.repo源備份,

mv  epel.repo  epel.repo.bak

4、清理yum源緩存和新建緩存,

 yum clean all

 yum makecache

5、再執行安裝命令

yum  install  ansible  -y

 6、查看安裝的版本

ansible --version

7、配置主機組

Ansible工具默認主目錄為/etc/ansible/,其中hosts文件為被管理機IP或者主機名列

二、配置免秘鑰登錄

1、管理主機上生成秘鑰

ssh-keygen -t rsa

Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
73:80:07:fa:9a:0d:e0:0e:d1:c2:44:d2:d2:61:67:21 root@ansible
The key's randomart image is:
+--[ RSA 2048]----+
|o=E.+..          |
|=oo+ . o         |
|ooo . . o        |
| + . . . .       |
|. . . . S .      |
| o   =   o       |
|  . o .          |
|                 |
|                 |
+-----------------+

2、將管理機上生成的秘鑰發送到被管理機

ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.207.137
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.207.132

3、測試是否配置成功

ansible -k all -m ping

 三、Ansible模塊詳細介紹

1、Ansible command模塊為Ansible默認模塊,主要用於執行Linux基礎命令,可以執行遠程服務器命令執行、任務執行等操作。

ansible -k -i /etc/ansible/hosts web -m command -a "date"
ansible -k all -m command -a "ping -c 1 www.baidu.com"
ansible -k 192.168.207.137 -m command -a "df -h" 指定單個IP執行任務

2、Ansible copy模塊主要用於文件或者目錄復制,支持文件、目錄、權限、用戶組功能。

ansible -k all -m copy -a 'src=/opt/test.txt dest=/tmp/ mode=755 owner=root'
ansible -k all -m copy -a 'content="Hello World" dest=/tmp/jfedu.txt mode=755 owner=root'
ansible -k all -m copy -a 'content="Hello World" dest=/tmp/xiaoxin.txt backup=yes mode=755 owner=root'

3、Ansible YUM模塊主要用於軟件的安裝、升級、卸載,支持紅帽rpm軟件包的管理。

ansible all -k -m yum -a "name=xinetd,screen state=installed" 
ansible all -k -m yum -a "name=sysstat,screen state=installed"       installed表示安裝服務
ansible all -k -m yum -a "name=sysstat,screen state=absent"       absent表示卸載服務
ansible 192.168.207.137 -k -m yum -a "name=sysstat,screen state installed disable_gpg_check=no" 表示不檢查key

  如有報以下錯:Cannot retrieve metalink for repository: epel/x86_64. Please verify its path and try again

  處理方法

  編輯epel.repo, 去除epel段中baseurl行的注釋符, 並注釋metalink行

vim /etc/yum.repos.d/epel.repo
 
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://download.fedoraproject.org/pub/epel/7/$basearch
#metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

4、Ansible file模塊主要用於對文件的創建、刪除、修改、權限、屬性的維護和管理。

 

ansible -k 192.168.207.* -m file -a "path=/tmp/test state=directory mode=755"               創建目錄
ansible -k 192.168.207.* -m file -a "path=/tmp/abc.txt state=touch mode=755" 創建文件

5、Ansible user模塊主要用於操作系統用戶、組、權限、密碼等操作

ansible -k 192.168.207.* -m user -a "name=jfedu home=/tmp/"                           home指定家目錄
ansible -k 192.168.207.* -m user -a "name=jfedu home=/tmp/ shell=/sbin/nologin"    指定家目錄,並指定shell
ansible -k 192.168.207.* -m user -a "name=jfedu state=absent force=yes" 刪除用戶

 6、Ansible cron模塊主要用於添加、刪除、更新操作系統crontab任務計划

ansible -k all -m cron -a "minute=0 hour=0 day=* month=* weekday=* name='Ntpdate server for sync time' job='/usr/sbin/ntpdate 139.224.227.121'"    定時同步時間
ansible -k all -m cron -a "minute=* hour=* day=* month=* weekday=* name='Ntpdate server for sync time' backup=yes job='/usr/sbin/ntpdate pool.ntp.org'" 開啟備份,備份目錄在/tmp下
ansible -k all -m cron -a "name='Ntpdate server for sync time' state=absent" 刪除備份計划

 7、Ansible synchronize模塊主要用於目錄、文件同步,主要基於rsync命令工具同步目錄和文件。

 

ansible -k all -m synchronize -a 'src=/tmp/ dest=/tmp/'                  同步/tmp目錄下的內容
ansible -k all -m synchronize -a 'src=/tmp/ dest=/tmp/ compress=yes delete=yes rsync_opts=--no-motd,--exclude=.txt'

 8、Ansible shell模塊主要用於遠程客戶端上執行各種shell命令或者運行腳本,遠程執行命令通過/bin/sh 環境來執行,支持比command更多的指令。

ansible -k all -m shell -a "/bin/sh /tmp/date.sh >>/tmp/var.log"               執行date.sh文件,並把執行結果追加到var.log文件里面去
ansible -k all -m shell -a "mkdir -p abc chdir=/tmp/ state=directory warn=no" 創建目錄
ansible -k all -m shell -a "ps -ef |grep http" 遠程查看http進程是否啟動
ansible -k all -m shell -a "crontab -l" 查看定時任務

9、Ansible service模塊主要用於遠程客戶端各種服務管理,包括啟動、停止、重啟、重新加載等。

 ansible -k all -m service -a "name=mysql state=restarted"               重啟mysql服務
ansible -k all -m service -a "name=network args=eth0  state=restarted" 重啟網卡服務
ansible -k all -m service -a "name=nfs enabled=yes runlevel=3.5" 遠程開啟nfs服務,設置3,5級別自動啟動

 10、Ansible PlayBook劇情模塊

 主要參數詳解

 

ansible-playbook nginx_install.yaml
nginx_install.yaml文件
---
- hosts: all
  tasks:
    - name: Installs nginx web server
      yum: name=nginx state=installed update_cache=true
      notify:
        - start nginx

  handlers:
    - name: start nginx
      service: name=nginx state=started

執行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM