一、playbook 劇本介紹
playbooks 本身由以下各部分組成
(1)Tasks:任務,即通過 task 調用 ansible 的模板將多個操作組織在一個 playbook 中運行
(2)Variables:變量
(3)Templates:模板
(4)Handlers:處理器,當changed狀態(tài)條件滿足時,(notify)觸發(fā)執(zhí)行的操作
(5)Roles:角色
二、示例
vim /etc/ansible/playbook/deamo1.yml
---
- name: the first play for install apache
#gather_facts: false
hosts: dbservers
remote_user: root
tasks:
- name: disable firewwalld
service: name=firewalld state=stopped enabled=no
- name: disable selinux
command: '/usr/sbin/setenforce 0'
ignore_errors: True
- name: disable selinux forever
replace: path=/etc/selinux/config regexp="enforcing" replace="disabled"
- name: mount cdrom
mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
- name: copy local yum configuration file
copy: src=/etc/yum.repos.d/repo.bak/local.repo dest=/etc/yum.repos.d/local.repo
- name: install apache
yum: name=httpd state=latest
- name: prepare httpd configuration file
copy: src=/etc/ansible/playbook/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify: "reload httpd"
- name: start apache
service: name=httpd state=started enabled=yes
handlers:
- name: reload httpd
service: name=httpd state=reloaded
1、運行playbook
ansible-playbook deamo1.yml
//補充參數:
-k(–ask-pass):用來交互輸入ssh密碼
-K(-ask-become-pass):用來交互輸入sudo密碼
-u:指定用戶
2、定義、引用變量
三、使用playbook部署lnmp集群
- name: the first play for install nginx
hosts: dbservers
remote_user: root
tasks:
- name: disable firewwalld
service: name=firewalld state=stopped enabled=no
- name: disable selinux
command: '/usr/sbin/setenforce 0'
ignore_errors: True
- name: disable selinux forever
replace: path=/etc/selinux/config regexp="enforcing" replace="disabled"
- name: mount cdrom
mount: src=/dev/sr0 path=/mnt fstype=iso9660 state=mounted
- name: copy local yum configuration file
copy: src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/nginx.repo
- name: install nginx
yum: name=nginx
- name: prepare nginx configuration file
copy: src=/etc/ansible/playbook/default.conf dest=/etc/nginx/conf.d/default.conf
- name: start nginx
service: name=nginx state=started enabled=yes
- name: wordpress
copy: src=/usr/share/nginx/html/wordpress dest=/usr/share/nginx/html/
- name: mysql
hosts: dbservers
remote_user: root
tasks:
- name: remove mariadb
shell: yum remove mariadb* -y
ignore_errors: True
- name: yum
command: wget https://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm
- name: install mysql57
command: rpm -ivh mysql57-community-release-el7-11.noarch.rpm
- name: change mysql-community-server
shell: sed -i 's/gpgcheck=1/gpgcheck=0/' /etc/yum.repos.d/mysql-community.repo
- name: install mysql-server
yum: name=mysql-server
- name: start mysql
service: name=mysqld.service state=started enabled=yes
- name: mysql congruation file
copy: src=/etc/ansible/playbook/mysql.sh dest=/var/lib/mysql
- name: echo password
shell: grep "password" /var/log/mysqld.log | awk 'NR==1{print $NF}' #在日志文件中找出root用戶的初始密碼
register: mysql_password #將初始密碼導入到mysql_password的變量中
- name: echo
debug:
msg: "{{ mysql_password }}" #輸出變量mysql_password的值
- name: grant location
shell: mysql --connect-expired-password -uroot -p"{{ mysql_password['stdout'] }}" -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin@123';"
- name: grant
shell: mysql --connect-expired-password -uroot -pAdmin@123 -e "grant all privileges on *.* to 'root'@'%' identified by 'Admin@123456' with grant option;"
- name: create database
shell: mysql --connect-expired-password -uroot -pAdmin@123 -e "create database wordpress;"
- name: grant
shell: mysql --connect-expired-password -uroot -pAdmin@123 -e "grant all on wordpress.* to 'admin'@'%' identified by 'Admin@123456';"
- name: grant
shell: mysql --connect-expired-password -uroot -pAdmin@123 -e "grant all on wordpress.* to 'admin'@'localhost' identified by 'Admin@123456';"
- name: flush
shell: mysql --connect-expired-password -uroot -pAdmin@123 -e 'flush privileges;'
- name: yum remove
command: yum -y remove mysql57-community-release-el7-10.noarch
- name: php
hosts: dbservers
remote_user: root
tasks:
- name: yum rpm
command: yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
ignore_errors: true
- name: yum-utils
yum: name=yum-utils
- name: yum-config
command: yum-config-manager --enable remi-php74
- name: list php
command: yum list php
- name: yilaibao
command: yum -y install php php-cli php-fpm php-mysqlnd php-zip php-devel php-gd php-mcrypt php-mbstring php-curl php-xml php-pear php-bcmath php-json php-redis
- name: start php
service: name=php-fpm state=started enabled=yes
將yum安裝的nginx里面的配置文件進行修改,后傳輸到對應的遠程主機
解壓wordpress壓縮文件,放入到對應的html網頁目錄底下
進行傳輸到遠程主機里的網頁頁面目錄上
使用瀏覽器進行訪問測試文章來源:http://www.zghlxwxcb.cn/news/detail-624987.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-624987.html
到了這里,關于【Ansible 的腳本 --- playbook 劇本】的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!