Ansible自動部署工具
運維部署三層次:
1 純手動操作
2 一鍵部署指令碼
3 使用自動部署工具
ansible是自動化運維工具,基於Python開發
功能:批次系統配置、批次程式部署、批次執行命令
工作原理:
1 主機清單:定義ansible要管理的物件
2 playbooks:劇本 python的yml指令碼
3 功能外掛(模組):實現劇本中具體的任務
4 連線模組:ssh
5 ansible整合
-----------------------------------------------
應用場景:
安裝系統的工具:
kickstart
初始化:
selinux 、iptables 、 IP 聯網 、 主機名、 時間 、常用工具
部署應用:
apache 、nginx 、 tomcat 、 mysql
批次執行命令
…
--------------------------------------
準備環境:
ansible 172.20.10.6 node1 172.20.10.7 node2 172.20.10.8 [root@ansible ~]# tail -3 /etc/hosts 172.20.10.6 ansible.ysla.com ansible 172.20.10.7 node1.ysla.com node1 172.20.10.8 node2.ysla.com node2 [root@ansible ~]# scp /etc/hosts 172.20.10.7:/etc/ [root@ansible ~]# scp /etc/hosts 172.20.10.8:/etc/
--------------------------------------------------
開始部署ansible:
[root@ansible ~]# rpm -ivh epel-release-7-6.noarch.rpm //安裝epel,yum也可以 [root@ansible ~]# yum install -y ansible [root@ansible ~]# rpm -ql ansible | head -20 /etc/ansible /etc/ansible/ansible.cfg ##配置檔案 /etc/ansible/hosts ##主機清單檔案 /etc/ansible/roles ##角色配置檔案 /usr/bin/ansible-playbook ##執行劇本的命令
配置: [root@ansible ~]# cd /etc/ansible/ [root@ansible /etc/ansible]# ls ansible.cfg hosts roles [root@ansible /etc/ansible]# vim hosts 172.20.10.7
測試連線: [root@ansible ~]# ssh 172.20.10.7 The authenticity of host '172.20.10.7 (172.20.10.7)' can't be established. ECDSA key fingerprint is SHA256:4JUE9YLEued+tSEr4sRDz1+7hze39R8aiRHxwQaaO4A. ECDSA key fingerprint is MD5:f7:3b:08:d7:a2:1f:19:2d:c4:98:6a:9c:d0:a8:72:8a. Are you sure you want to continue connecting (yes/no)? yes [root@ansible ~]# cat .ssh/known_hosts 172.20.10.7 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBDHNd2TGYifRr0u1Yr94st5CdVG+NqoHVcG8Fyt/Hh2osliJmUu1N/UDTS8MvD8T+nSMYcGQ3GUvAV0YZ+uUf8Q= 需要記錄節點的公鑰指紋 [root@ansible /etc/ansible]# ansible 172.20.10.7 -m ping -k SSH password: 172.20.10.7 | SUCCESS => { [root@ansible /etc/ansible]# vim hosts [node] 172.20.10.7 172.20.10.8 無密碼連線: [root@ansible ~]# vim /etc/ansible/hosts [node] 172.20.10.7 ansible_ssh_user=root ansible_ssh_pass=123 172.20.10.8 ansible_ssh_user=root ansible_ssh_pass=123 [root@ansible ~]# ansible node -m ping [root@ansible ~]# ll /etc/ansible/hosts -rw-r--r-- 1 root root 1134 Jul 19 10:08 /etc/ansible/hosts ssh無密碼連線: [root@ansible ~]# ssh-keygen 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:回車 [root@ansible ~]# cd .ssh/ [root@ansible ~/.ssh]# ls id_rsa id_rsa.pub 私鑰 公鑰 公鑰釋出給被連線端 [root@ansible ~]# ssh-copy-id -i .ssh/id_rsa.pub 172.20.10.7 [root@node1 ~]# cd .ssh/ [root@node1 ~/.ssh]# ls authorized_keys [root@node1 ~/.ssh]# cat authorized_keys
分發公鑰的操作指令碼化:
[root@ansible ~]# vim iplist 172.20.10.7 172.20.10.8 [root@ansible ~]# vim scp_sshpubkey.sh #!/bin/bash # 分發ssh的公鑰 pass="123" key="/root/.ssh/id_rsa.pub" file="/root/iplist" while read ip do sshpass -p ${pass} /usr/bin/ssh-copy-id -o StrictHostKeyChecking=no -i ${key} ${ip} &> /dev/null && echo "${ip}公鑰傳輸成功." done < $file
sshpass帶入ssh被連線端的密碼 [root@ansible ~]# sshpass -p 123 ssh 172.20.10.7 Last login: Thu Jul 19 10:15:12 2021 from ansible.ysla.com [root@node1 ~]# [root@ansible ~]# > .ssh/known_hosts [root@ansible ~]# sshpass -p 123 ssh -o StrictHostKeyChecking=no 172.20.10.7
介紹ansible的常用模組:
1. ping
探測對端是否存活
2. command
在節點執行命令,不支援管道|
[root@ansible ~]# ansible node -m command -a "mkdir /tmp/dir1" 返回資訊: 綠 成功 紅 失敗 粉 警告 黃 執行成功
3. copy
將ansible的檔案傳輸給節點。
選項: src 原始檔 dest 目標檔案 backup 如果目標檔案已存在,覆蓋之前是否要備份 owner 指定所有者 mode 指定許可權 [root@ansible /etc/ansible]# mkdir files [root@ansible /etc/ansible]# ls ansible.cfg files hosts roles [root@ansible /etc/ansible]# cp /etc/hosts files/ [root@ansible /etc/ansible]# ansible node -m copy -a "src=/etc/ansible/files/hosts dest=/tmp/hosts [root@ansible /etc/ansible]# ansible node -m copy -a "src=/etc/ansible/files/hosts dest=/etc/hosts backup=yes" [root@ansible /etc/ansible]# ansible node -m command -a "useradd user1" [root@ansible /etc/ansible]# ansible node -m copy -a "src=/etc/ansible/files/hosts dest=/tmp/hosts1 owner=user1 mode=700" [root@ansible /etc/ansible]# ansible node -m command -a "ls -l /tmp/hosts1" 172.20.10.8 | CHANGED | rc=0 >> -rwx------ 1 user1 root 256 Jul 19 11:53 /tmp/hosts1 172.20.10.7 | CHANGED | rc=0 >> -rwx------ 1 user1 root 256 Jul 19 11:53 /tmp/hosts1
4. file
管理節點上的檔案
選項: path 指定檔案路徑 state 操作 touch 建立檔案 directory 建立目錄 absent 刪除 [root@ansible ~]# ansible node -m file -a "path=/tmp/f1 state=touch" ## == touch [root@ansible ~]# ansible node -m file -a "path=/tmp/d1 state=directory" ## == mkdir [root@ansible ~]# ansible node -m file -a "path=/tmp/f1 state=absent" ## == rm -f [root@ansible ~]# ansible node -m file -a "path=/tmp/d1 state=absent" ## == rm -fr [root@ansible ~]# ansible node -m file -a "path=/tmp/f1 mode=700 owner=user1 group=user1 state=touch" ## == touch ## == chown user1:user1 ## == chmod 700 [root@ansible ~]# ansible node -m file -a "path=/tmp/d1/d2/d3 state=directory" ## == mkdir -p [root@ansible ~]# ansible node -m file -a "path=/tmp/d1/d2/d3 mode=757 recurse=yes" ## == chmod -R 757 # vim /etc/bashrc export PS1="[\u@\[\e[32;40m\]\h \[\e[31;40m\]\w\[\e[0m\]]\\$ "
5. get_url
下載檔案的
[root@ansible ~]# ansible node -m get_url -a "url=ftp://172.20.10.99/release/epel-release-7-6.noarch.rpm dest=/tmp" [root@ansible ~]# ansible node -m get_url -a "url=ftp://172.20.10.99/scripts/nginx-1.13-clean.sh dest=/tmp mode=755"
6. user
管理使用者
[root@ansible ~]# ansible node -m user -a "name=zhangsan uid=2000" ## == useradd -u 2000 zhangsan [root@ansible ~]# ansible 172.20.10.7 -m user -a "name=zhangsan state=absent" ## == userdel zhangsan [root@ansible ~]# ansible 172.20.10.8 -m user -a "name=zhangsan state=absent remove=yes" ## == userdel -r zhangsan
7. group
管理組
[root@ansible ~]# ansible node -m group -a "name=group1 gid=100000" ## == groupadd -g 100000 group1 [root@ansible ~]# ansible node -m group -a "name=group1 state=absent" ## == groupdel group1
8. yum
安裝rpm包
解除安裝: [root@ansible ~]# ansible node -m yum -a "name=lftp state=absent" 安裝: [root@ansible ~]# ansible node -m yum -a "name=lftp"
9. systemd
管理節點上的服務 開啟或關閉
centos7
centos6是service模組
[root@ansible ~]# ansible node -m yum -a "name=httpd" [root@ansible ~]# ansible node -m systemd -a "name=httpd state=started" [root@ansible ~]# ansible node -m systemd -a "name=httpd state=stopped" [root@ansible ~]# ansible node -m systemd -a "name=httpd state=restarted" [root@ansible ~]# ansible node -m systemd -a "name=httpd enabled=yes"
10. shell
在節點執行shell指令碼
[root@ansible ~]# ansible node -m shell -a "/usr/bin/bash /tmp/nginx-1.13-clean.sh"
11. cron
在遠端節點配置計劃任務
每隔5分鐘,執行一次關閉firewalld的命令: */5 * * * * systemctl stop firewalld 分 minute 時 hour 日 day 月 month 周 weekday 命令 job [root@ansible ~]# ansible node -m cron -a 'name="stop firewalld" minute=*/5 job="systemctl stop firewalld"' [root@node1 /tmp]# crontab -l #Ansible: stop firewalld */5 * * * * systemctl stop firewalld
=======================================
寫劇本:
[root@ansible /etc/ansible]# mkdir playbooks [root@ansible /etc/ansible]# cd playbooks/ [root@ansible /etc/ansible/playbooks]# vim apache.yml - name: install and config apache hosts: node user: root tasks: - name: install apache yum: name=httpd - name: config apache copy: src=files/httpd.conf dest=/etc/httpd/conf/httpd.conf backup=yes notify: restart httpd - name: create index.html copy: src=files/index.html dest=/var/www/html/index.html handlers: - name: restart httpd systemd: name=httpd state=restarted enabled=yes [root@ansible /etc/ansible/playbooks]# mkdir files [root@ansible /etc/ansible/playbooks]# yum install -y httpd [root@ansible /etc/ansible/playbooks]# cp /etc/httpd/conf/httpd.conf files/ [root@ansible /etc/ansible/playbooks]# echo "test-ansible" > files/index.html [root@ansible /etc/ansible/playbooks]# vim files/httpd.conf 加一些註釋符號 [root@ansible /etc/ansible/playbooks]# ansible-playbook apache.yml 虛擬主機: - name: config apache hosts: node user: root tasks: - name: config apache copy: src=files/httpd.conf dest=/etc/httpd/conf/httpd.conf backup=yes notify: restart httpd - name: create a'directory file: path=/var/www/html/a state=directory - name: create b'directory file: path=/var/www/html/b state=directory - name: create a'index.html copy: src=files/aindex.html dest=/var/www/html/a/index.html - name: create b'index.html copy: src=files/bindex.html dest=/var/www/html/b/index.html handlers: - name: restart httpd systemd: name=httpd state=restarted enabled=yes
寫一個ftp部署劇本:
[root@ansible /etc/ansible/playbooks]# vim ftp.yml - name: install and config vsftpd hosts: node user: root tasks: - name: install vsftpd yum: name=vsftpd - name: create upload dir file: path=/var/ftp/upload owner=ftp state=directory - name: config vsftpd copy: src=files/vsftpd.conf dest=/etc/vsftpd/vsftpd.conf backup=yes notify: restart vsftpd handlers: - name: restart vsftpd systemd: name=vsftpd state=restarted enabled=yes [root@ansible /etc/ansible/playbooks]# yum install -y vsftpd [root@ansible /etc/ansible/playbooks]# cp /etc/vsftpd/vsftpd.conf files/ [root@ansible /etc/ansible/playbooks]# vim files/vsftpd.conf
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69990629/viewspace-2787041/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Ansible自動化部署工具-元件及語法介紹元件
- Ansible自動化運維工具運維
- 叢集自動化工具--Ansible
- Ansible自動化部署工具-role模式安裝filebeat實際案例分析模式
- 使用 Jenkins + Ansible 實現自動化部署 NginxJenkinsNginx
- 自動化運維工具Ansible介紹運維
- 自動化運維工具——ansible詳解(一)運維
- 自動化運維工具——ansible詳解(二)運維
- 自動化運維工具ansible的實踐運維
- [Linux]Ansible自動化運維② - 工具與模組Linux運維
- Ansible 多機自動化工具 初學筆記筆記
- Ansible與Ansible部署
- 自動化kolla-ansible部署openstack+GPU透傳方法GPU
- Python自動化部署工具-FabricPython
- Ansible 運維自動化 ( 配置管理工具 )運維
- 在Linux中,如何在Linux中使用Ansible進行自動化部署?Linux
- ansible部署配置
- 如何在 CentOS 8/RHEL 8 上安裝 Ansible(自動化工具)CentOS
- Ansible自動化配置詳解
- ansible批次部署apacheApache
- ansible自動化運維入門運維
- 用 python 寫一個自動化部署工具Python
- 運維必會:基於 Ansible 的 Docker 自動化部署,小白也能看懂?運維Docker
- 自動化kolla-ansible部署centos7.9+openstack-train-超融合單機架構CentOSAI架構
- ansible自動化運維資料庫運維資料庫
- [自動化]淺聊ansible的冪等
- Kolla-ansible部署openStack
- python學習-fabric(高效遠端自動化部署工具)Python
- 提升自動化技巧的 10 篇 Ansible 文章
- 用Ansible 自動化搭建本地Kubernetes叢集
- Python+Django+Ansible Playbook自動化運維PythonDjango運維
- kolla-ansible-----快速部署openstack
- Ansible的安裝及部署
- [Linux]Ansible自動化運維① - 入門知識Linux運維
- 自動化運維利器Ansible要點彙總運維
- Jenkins自動部署配置Jenkins
- Maven 自動化部署Maven
- github actions自動部署Github