centos7安装ansible
Centos7安裝ansible
一、安裝ansible軟件
1、安裝yum源
rpm -Uvh http://mirrors.ustc.edu.cn/epel/epel-release-latest-7.noarch.rpmyum install epel-release -yyum install ansible要是報錯:epel源與python版本沖突原因,有些包是需要依賴python2.6的版本,此主機的python版本是2.7.5。
2、那就先卸載 epel-release源
yum install epel-release -y3、到 /etc/yum.repos.d 目錄下,將epel.repo源備份,
mv epel.repo epel.repo.bak4、清理yum源緩存和新建緩存,
yum clean allyum makecache5、再執行安裝命令
yum install ansible -y6、查看安裝的版本
ansible --version7、配置主機組
Ansible工具默認主目錄為/etc/ansible/,其中hosts文件為被管理機IP或者主機名列
二、配置免秘鑰登錄
1、管理主機上生成秘鑰
ssh-keygen -t rsaGenerating 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.1323、測試是否配置成功
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-74、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: alltasks:- name: Installs nginx web serveryum: name=nginx state=installed update_cache=truenotify:- start nginxhandlers:- name: start nginxservice: name=nginx state=started執行結果:
總結
以上是生活随笔為你收集整理的centos7安装ansible的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【收藏】goland报错:报错packa
- 下一篇: Scala Akka的Actor模型