Ansible playbook實戰
1.基礎準備
#1.安裝ansible [root@m01 ~]# yum install -y ansible #2.配置ansible [root@m01 ~]# vim /etc/ansible/ansible.cfg host_key_checking = False #這個解開註釋 #3.配置主機清單 [root@m01 ~]# vim /etc/ansible/hosts [web_group] web01 ansible_ssh_pass='admin123' web02 ansible_ssh_pass='admin123' [db_group] db01 ansible_ssh_pass='admin123' db02 ansible_ssh_pass='admin123' #4.建立專案目錄 [root@m01 ~]# mkdir /project/{web,mariadb,rsyncd,nfs,sersync,base} -p [root@m01 ~]# tree /project/ /project/ ├── base ├── mariadb ├── nfs ├── rsyncd ├── sersync └── web 6 directories, 0 files
2.編寫playbook
[root@m01 ~]# cd /project/base [root@m01 base]# vim lnmp.yml - hosts: all tasks: - name: Stop Firewalld systemd: name: firewalld state: stopped enabled: no - name: Stop Selinux selinux: state: disabled - name: Create www Group group: name: www gid: 666 state: present - name: Create www User user: name: www uid: 666 group: www create_home: false # 不需要家目錄 shell: /sbin/nologin state: present
2)第二部分:安裝nginx伺服器
#原始碼包安裝 1.上傳nginx包 copy 2.解壓 unarchive 3.生成 shell 4.編譯 shell 5.安裝 shell -m shell -a 'cd /nginx/ && ./configure -- && make && make install' 6.編輯system管理nginx檔案 copy 7.配置conf.d的目錄 8.推配置檔案 #官方源安裝 1.配置官方源 copy 2.安裝nginx yum 3.配置nginx #rpm包安裝
#1.上傳rpm包 [root@m01 ~]# rz nginx-1.16.1-1.el7.ngx.x86_64.rpm #2.配置nginx配置檔案 [root@m01 ~]# vim nginx.conf user www; ... #3.配置wordpress配置檔案 [root@m01 ~]# vim wordpress.conf server { listen 80; server_name blog.linux.com; root /code/wordpress; index index.php; location ~ \.php$ { root /code/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
[root@m01 base]# vim lnmp.yml # 結尾繼續接上 - hosts: web_group tasks: - name: Copy Nginx Rpm copy: src: /root/nginx-1.16.1-1.el7.ngx.x86_64.rpm dest: /tmp/ - name: Install Nginx Server yum: name: /tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm state: present - name: Config Nginx Server copy: src: /root/nginx.conf dest: /etc/nginx/ - name: Config Wordpress Conf copy: src: /root/wordpress.conf dest: /etc/nginx/conf.d/ - name: Start Nginx Server systemd: name: nginx state: started
3)第三部分:安裝php服務
#1.上傳php包 [root@m01 ~]# rz php.tar.gz #2.配置php使用者 [root@m01 ~]# vim www.conf user = www group = www
[root@m01 base]# vim lnmp.yml # 結尾繼續接上 - name: Tar PHP Package unarchive: src: /root/php.tar.gz dest: /tmp/ - name: Install PHP Server shell: "yum localinstall -y /tmp/*.rpm" - name: Config PHP Server copy: src: /root/www.conf dest: /etc/php-fpm.d/ - name: Start PHP Server systemd: name: php-fpm state: started
4)第四部分:配置wordpress站點
#1.上傳wordpress包 [root@m01 ~]# rz wordpress-5.0.3-zh_CN.tar.gz
[root@m01 base]# vim lnmp.yml # 結尾繼續接上 - name: Create Code Dir file: path: /code state: directory - name: Install Wordpress Code unarchive: src: /root/wordpress-5.0.3-zh_CN.tar.gz dest: /code/ - name: Chown Code Dir file: path: /code state: directory owner: www group: www recurse: yes
- hosts: db01 tasks: - name: Install Mariadb Server yum: name: mariadb-server state: present - name: Install MySQL-python Server #ansible要控制mysql需要安裝這個 yum: name: MySQL-python state: present - name: Start Mariadb Server systemd: name: mariadb state: started enabled: yes - name: Create Database Wordpress mysql_db: name: wordpress state: present - name: Create Database Wordpress User mysql_user: name: "wp" host: "172.16.1.%" password: '123456' priv: 'wordpress.*:ALL' state: present
3.遷移(擴充套件)
1)編寫資料遷移
[root@m01 base]# cat sql.yml - hosts: db01 tasks: - name: Dump Database Wordpress mysql_db: #把wordpress庫匯出到sql中 state: dump name: wordpress target: /root/wordpress.sql - name: Get wordpress.sql fetch: #把db01的sql拉到本地路徑 src: /root/wordpress.sql dest: /root/ #會在下面建立 主機名/檔案路徑 /root/db01/root/wordpress.sql - hosts: db02 tasks: - name: Post wordpress.sql copy: src: /root/db01/root/wordpress.sql dest: /tmp/ - name: Install Mariadb Server #下面同上建立資料庫 yum: name: mariadb-server state: present - name: Install MySQL-python Server #ansible要控制mysql需要安裝這個 yum: name: MySQL-python state: present - name: Start Mariadb Server systemd: name: mariadb state: started enabled: yes - name: Create Database Wordpress mysql_db: name: wordpress state: present - name: Create Database Wordpress User mysql_user: name: "wp" host: "172.16.1.%" password: '123456' priv: 'wordpress.*:ALL' state: present - name: Import wordpress.sql #匯入sql資料 mysql_db: state: import name: wordpress target: /tmp/wordpress.sql
- hosts: web01 tasks: - name: Tgz Wordpress Code - archive: path: /code/wordpress dest: /root/wordpress.tgz - name: Get Wordpress Code fetch: src: /root/wordpress.tgz dest: /root/ - hosts: web02 tasks: - name: Tar Wordpress Code unarchive: src: /root/web01/root/wordpress.tgz dest: /
一、變數概述
變數提供了便捷的方式來管理Ansible playbook的每一個專案中的動態值,比如nginx-1.6.3這個軟體包的版本,在其他地方或許會反覆使用,
那麼如果講此值設定為變數,然後再在其他的playbook中呼叫,會方便許多。如此一來還方便維護,減少維護的成本。
1.透過命令列進行變數定義 2.在play檔案中進行變數定義 3.透過Inventory主機資訊檔案中進行變數定義 4.透過vars_files定義變數 5.透過host_vars和group_vars定義變數 如果在定義變數時,變數衝突了 在上述的三個地方分別設定了: 1.命令列中:age=11 2.play檔案中:age=12 3.Inventory中:age=13 那麼,最終的age結果一定是11 變數的讀取優先順序為:命令列 > playbook檔案 > Inventory檔案
1.在playbook定義變數
[root@m01 base]# vim install.yml - hosts: web_group vars: package: - httpd - mariadb-server - MySQL-python - php - nginx tasks: - name: Install Server yum: name: "{{ package }}"
[root@m01 base]# vim install.yml - hosts: web_group vars: web: - httpd - nginx db: - mariadb-server - MySQL-python tasks: - name: Install Server yum: name: "{{ web }}"
- name: Install LNMP yum: name: "{{ packages }}" vars: packages: - nginx - php - mariadb-server
2.透過vars_file定義變數
#1.配置變數檔案 [root@m01 base]# vim vars.yml web_server: nginx db_server: mariadb-server #2.呼叫變數檔案再使用變數 [root@m01 base]# vim host.yml - hosts: db_group vars_files: /project/base/vars.yml tasks: - name: Install Mariadb Server yum: name: "{{ db_server }}" #3.定義多個變數檔案 [root@m01 base]# vim host.yml - hosts: db_group vars_files: - /project/base/vars.yml - /project/base/vars2.yml tasks: - name: Install Mariadb Server yum: name: "{{ nginx }}"