Ansible是一個開源部署工具php
開發語言:Pythonhtml
特色:SSH協議通訊,全平臺,無須要編譯,模塊化部署管理node
做用:推送Playbook進行遠程節點快速部署python
Python語言編寫,無Client,模塊化配置管理nginx
Playbook腳本編寫規範,易於上手,適合中小規模快速部署git
Ansible的優點和應用場景github
輕量級無客戶端shell
開源免費,學習成本低,快速上手vim
使用Playbook做爲核心配置架構,統一的腳本格式批量化部署centos
完善的模塊化擴展,支持目前主流的開發場景
強大的穩定性和兼容性
1.Yum包管理安裝
yum -y install ansible
2.Git源代碼安裝【推薦】
git clone https://github.com/ansible/ansible.git
1.預先安裝Python3.6版本
2.安裝virtualenv
pip install virtualenv
3.建立Ansible帳戶並安裝python3.6版本virtualenv實例
useradd deploy && su - deploy
virtualenv -p /usr/local/bin/python3.6 .py3-a2.5-env
4. Git源代碼安裝ansible2.5
cd /home/deploy/.py3-a2.5-env
git clone https://github.com/ansible/ansible.git
cd ansible && git checkout stable-2.5
5. 加載python3.6 virtualenv 環境
source /home/deploy/.py3-a2.5-env/bin/activate
6.安裝ansible依賴包
pip install paramiko PyYAML jinja2
7.在python3.6虛擬環境下加載ansible2.5
source /hone/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q
8.驗證ansible2.5
ansible --version
[root@ansible ~]# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz [root@ansible ~]# tar xf Python-3.6.5.tar.xz [root@ansible ~]# cd Python-3.6.5/ [root@ansible Python-3.6.5]# yum install -y gcc [root@ansible Python-3.6.5]# ./configure --prefix=/usr/local/ --with-ensurepip=install --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib" [root@ansible Python-3.6.5]# make && make install [root@ansible local]# which pip3.6 /usr/local/bin/pip3.6 [root@ansible local]# ln -s /usr/local/bin/pip3.6 /usr/local/bin/pip [root@ansible local]# pip install virtualenv
[root@ansible local]# useradd deploy [root@ansible local]# su - deploy [deploy@ansible ~]$ virtualenv -p /usr/local/bin/python3.6 .py3-a2.5-env
[root@ansible local]# cd /home/deploy/.py3-a2.5-env/ 若是沒有Git就yum安裝一個 [root@ansible .py3-a2.5-env]# yum install -y git
[root@ansible local]# su - deploy
[root@localhost Python-3.6.5]$ git clone https://github.com/ansible/ansible.git
[deploy@ansible ~]$ source /home/deploy/.py3-a2.5-env/bin/activate (.py3-a2.5-env) [deploy@ansible ~]$
(.py3-a2.5-env) [deploy@ansible ~]$ pip install paramiko pyYAML jinja2 (.py3-a2.5-env) [deploy@ansible ~]$ mv ansible/ .py3-a2.5-env/ (.py3-a2.5-env) [deploy@ansible ~]$ cd .py3-a2.5-env/ansible/ (.py3-a2.5-env) [deploy@ansible ansible]$ git checkout stable-2.5 分支 stable-2.5 設置爲跟蹤來自 origin 的遠程分支 stable-2.5。 切換到一個新分支 'stable-2.5' (.py3-a2.5-env) [deploy@ansible ansible]$ source /home/deploy/.py3-a2.5-env/ansible/hacking/env-setup -q (.py3-a2.5-env) [deploy@ansible ansible]$ ansible --version
[root@localhost ~]# su - deploy 上一次登陸:三 4月 10 15:24:28 CST 2019pts/0 上 [deploy@ansible ~]$ source .py3-a2.5-env/bin/activate (.py3-a2.5-env) [deploy@ansible ~]$ source .py3-a2.5-env/ansible/hacking/env-setup -q (.py3-a2.5-env) [deploy@ansible ~]$ ansible --version ansible 2.5.15 (stable-2.5 5b3fd67381) last updated 2019/04/10 15:31:37 (GMT +800)
(.py3-a2.5-env) [deploy@ansible ~]$ ansible-playbook --version
說明在python3.6.5虛擬環境下加載了ansible2.5版本
(.py3-a2.5-env) [deploy@ansible ~]$ mkdir test_playbooks (.py3-a2.5-env) [deploy@ansible ~]$ cd test_playbooks (.py3-a2.5-env) [deploy@ansible test_playbooks]$ mkdir inventory (.py3-a2.5-env) [deploy@ansible test_playbooks]$ mkdir roles (.py3-a2.5-env) [deploy@ansible test_playbooks]$ cd inventory/ (.py3-a2.5-env) [deploy@ansible inventory]$ cat testenv [testservers] 192.168.122.135 [testservers:vars] server_name=192.168.122.135 user=root output=/root/test.txt (.py3-a2.5-env) [deploy@ansible inventory]$ cd .. (.py3-a2.5-env) [deploy@ansible test_playbooks]$ cd roles/ (.py3-a2.5-env) [deploy@ansible roles]$ mkdir testbox (.py3-a2.5-env) [deploy@ansible roles]$ cd testbox/ (.py3-a2.5-env) [deploy@ansible testbox]$ mkdir tasks (.py3-a2.5-env) [deploy@ansible testbox]$ cd tasks/ (.py3-a2.5-env) [deploy@ansible tasks]$ vim main.yml (.py3-a2.5-env) [deploy@ansible tasks]$ cat main.yml - name: Print server name and user to remote testbox shell: "echo 'Cueerntly {{user}} is logining {{server_name}}' > {{output}}" (.py3-a2.5-env) [deploy@ansible tasks]$ pwd /home/deploy/test_playbooks/roles/testbox/tasks (.py3-a2.5-env) [deploy@ansible tasks]$ cd ../../.. (.py3-a2.5-env) [deploy@ansible test_playbooks]$ pwd /home/deploy/test_playbooks #建立入口文件 (.py3-a2.5-env) [deploy@ansible test_playbooks]$ ls inventory roles (.py3-a2.5-env) [deploy@ansible test_playbooks]$ vim deploy.yml (.py3-a2.5-env) [deploy@ansible test_playbooks]$ cat deploy.yml - hosts: "testservers" gather_facts: true remote_user: root roles: - testbox 查看一下 (.py3-a2.5-env) [deploy@ansible test_playbooks]$ tree . . ├── deploy.yml ├── inventory │ └── testenv └── roles └── testbox └── tasks └── main.yml 4 directories, 3 files
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ ssh-keygen -t rsa (.py3-a2.5-env) [deploy@ansible test_playbooks]$ ssh-copy-id -i /home/deploy/.ssh/id_rsa.pub root@192.168.122.135 #測試是否不須要密碼登錄 (.py3-a2.5-env) [deploy@ansible test_playbooks]$ ssh root@192.168.122.135
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ ansible-playbook -i inventory/testenv ./deploy.yml PLAY [testservers] ****************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************** ok: [192.168.122.135] TASK [testbox : Print server name and user to remote testbox] *********************************************************************** changed: [192.168.122.135] PLAY RECAP ************************************************************************************************************************** 192.168.122.135 : ok=2 changed=1 unreachable=0 failed=0
驗證是否成功
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ ssh root@192.168.122.135 Last login: Wed Apr 10 16:34:50 2019 from 192.168.122.1 [root@node01 ~]# ls anaconda-ks.cfg test.txt [root@node01 ~]# cat test.txt Cueerntly root is logining 192.168.122.135
在目標主機建立文件或目錄,並賦予其系統權限
實現Ansible服務器到目標主機的文件傳送
remote_src=no不要目標主機上搜索文件,若是是yes則容許在目標主機上搜索文件
獲取遠程文件狀態信息
打印語句到Ansible執行輸出
若是存在打印foo.sh exists 不存在則不打印
用來執行Linux目錄主機命令行
shell模塊支持重定向管道符
實現Ansible服務端到目標主機的jinja2模塊傳送
調用目標主機系統包管理工具(yum , apt)進行安裝
管理目標主機系統服務
[root@localhost ~]# su - deploy [deploy@ansible ~]$ source .py3-a2.5-env/bin/activate (.py3-a2.5-env) [deploy@ansible ~]$ (.py3-a2.5-env) [deploy@ansible ~]$ ps -ef | grep ansible | awk '{print $2}' 77171 (.py3-a2.5-env) [deploy@ansible ~]$ source .py3-a2.5-env/ansible/hacking/env-setup -q (.py3-a2.5-env) [deploy@ansible ~]$ ansible-playbook --version 預配置 (.py3-a2.5-env) [deploy@ansible test_playbooks]$ ssh root@192.168.122.135 [root@node01 ~]# useradd foo [root@node01 ~]# useradd deploy [root@node01 ~]# mkdir /etc/nginx [root@node01 ~]# wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm [root@node01 ~]# rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
一、改寫劇本
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ cat roles/testbox/tasks/main.yml - name: Print server name and user to remote testbox shell: "echo 'Cueerntly {{user}} is logining {{server_name}}' > {{output}}" - name: create a file file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'
二、執行
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ ansible-playbook -i inventory/testenv ./deploy.yml
一、準備及寫劇本
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ mkdir roles/testbox/tasks/files
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ cat roles/testbox/tasks/files/foo.sh
echo "this is a test scripts"
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ cat roles/testbox/tasks/main.yml
- name: Print server name and user to remote testbox
shell: "echo 'Cueerntly {{user}} is logining {{server_name}}' > {{output}}"
- name: create a file
file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo'
- name: copya file
copy: 'remote_src=no src=roles/testbox/tasks/files/foo.sh dest=/root/foo.sh mode=0644 force=yes'
二、運行
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ ansible-playbook -i inventory/testenv ./deploy.yml
一、編寫劇本
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ cat roles/testbox/tasks/main.yml - name: Print server name and user to remote testbox shell: "echo 'Cueerntly {{user}} is logining {{server_name}}' > {{output}}" - name: create a file file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo' - name: copya file copy: 'remote_src=no src=roles/testbox/tasks/files/foo.sh dest=/root/foo.sh mode=0644 force=yes' - name: check if foo.sh exits stat: 'path=/root/foo.sh' register: script_stat - debug: msg="foo.sh exists" when: script_stat.stat.exists - name: run the scripts command: 'sh /root/foo.sh'
二、運行
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ ansible-playbook -i inventory/testenv ./deploy.yml
一、
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ cat inventory/testenv [testservers] 192.168.122.135 [testservers:vars] server_name=192.168.122.135 user=root output=/root/test.txt server_name=192.168.122.135 port=80 user=deploy worker_processes=1 max_open_file=65505 root=/www
二、
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ mkdir roles/testbox/Templates (.py3-a2.5-env) [deploy@ansible test_playbooks]$ cat roles/testbox/Templates/nginx.conf.j2 #user {{ user }}; worker_processes {{ worker_processes }}; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen {{ port }}; server_name {{ server_name }}; #charset koi8-r; #access_log logs/host.access.log main; location / { root {{ root }}; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
三、
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ cat roles/testbox/tasks/main.yml - name: Print server name and user to remote testbox shell: "echo 'Cueerntly {{user}} is logining {{server_name}}' > {{output}}" - name: create a file file: 'path=/root/foo.txt state=touch mode=0755 owner=foo group=foo' - name: copya file copy: 'remote_src=no src=roles/testbox/tasks/files/foo.sh dest=/root/foo.sh mode=0644 force=yes' - name: check if foo.sh exits stat: 'path=/root/foo.sh' register: script_stat - debug: msg="foo.sh exists" when: script_stat.stat.exists - name: run the scripts command: 'sh /root/foo.sh' - name: write the nginx config file template: src=roles/testbox/Templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf - name: ensure nginx is at the latest version yum: pkg=nginx state=latest - name: start nginx service service: name=nginx state=started
四、
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ ansible-playbook -i inventory/testenv ./deploy.yml
五、
[root@node01 yum.repos.d]# mkdir /www [root@node01 www]# touch a.txt [root@node01 www]# cat a.txt aaa
六、
(.py3-a2.5-env) [deploy@ansible test_playbooks]$ curl 192.168.122.135/a.txt aaa