Playbook是 Ansible 一个非常简单的配置管理多主机的状态描述文件
Playbook按照指定的操作步骤有序执行,支持同步和异步方式
Playbook是通过YAML格式来书写的:
- 缩进:和 Python 一样,YAML 使用缩进表示数据关系,两个空格,不能使用Tab
- 冒号:冒号后面不是空格就是换行
- 短横线:短横线后面一定是空格,短横线表示选项
Playbook剧本基础
受控端环境尽量干净一点
## 创建项目目录 [root@Ansible ~]# mkdir project && cd project ## 第一种Playbook书写方式(官方推荐) [root@Ansible project]# vim httpd.yml - hosts: web ##对那个主机执行 tasks: ##执行的任务 - name: Install Httpd Server ##描述(暂时没有意义) yum: name: httpd state: installed ## 第二种Playbook书写方式 [root@Ansible project]# vim httpd1.yml - hosts: web tasks: - name: Install Httpd Server yum: name=httpd state=installed ## 检测文件有没有格式错误 [root@Ansible project]# ansible-playbook --syntax-check httpd.yml playbook: httpd.yml ## 执行一下(可以用 -C 模拟执行) [root@Ansible project]# ansible-playbook httpd.yml PLAY [web] ******************************************************************************** TASK [Gathering Facts] ******************************************************************** ok: [192.168.1.2] TASK [Install Httpd Server] *************************************************************** changed: [192.168.1.2] PLAY RECAP ******************************************************************************** 192.168.1.2 : ok=2 changed=1 unreachable=0 failed=0
在上面的配置文件基础上继续
[root@Ansible project]# vim httpd.yml - hosts: web tasks: - name: Install Httpd Server yum: name: httpd state: installed - name: Edit Index.html File copy: content: The is Ansible Edit dest: /var/www/html/index.html - name: Started Httpd Server systemd: name: httpd state: started enabled: yes - name: Stopped Firewalld Server systemd: name: firewalld state: stopped ## 检测文件有没有格式错误 [root@Ansible project]# ansible-playbook --syntax-check httpd.yml playbook: httpd.yml ## 执行一下 [root@Ansible project]# ansible-playbook httpd.yml PLAY [web] ******************************************************************************** TASK [Gathering Facts] ******************************************************************** ok: [192.168.1.2] TASK [Install Httpd Server] *************************************************************** ok: [192.168.1.2] TASK [Edit Index.html File] *************************************************************** changed: [192.168.1.2] TASK [Started Httpd Server] *************************************************************** changed: [192.168.1.2] TASK [Stopped Firewalld Server] *********************************************************** ok: [192.168.1.2] PLAY RECAP ******************************************************************************** 192.168.1.2 : ok=5 changed=2 unreachable=0 failed=0
直接搭建一个 LAMP ,下载个WordPress解压放到相应的目录
环境不干净,配置可能需要更深入的,后面会讲到,这里就清理一下环境了
## 下载Wordpress [root@Ansible project]# wget https://cn.wordpress.org/wordpress-5.0.3-zh_CN.tar.gz ## 书写lamp文件 [root@Ansible project]# vim lamp.yml - hosts: hosts tasks: - name: Install Server yum: name: - httpd - mariadb-server - php - php-mysql - php-pdo state: present - name: Unzip File unarchive: src: ./wordpress-5.0.3-zh_CN.tar.gz dest: /var/www/html/ mode: 0755 - name: Started Httpd Server systemd: name: httpd state: started - name: Started Mariadb Server systemd: name: mariadb state: started - name: Stopped Firewall Server systemd: name: firewalld state: stopped ## 检测一下文件格式 [root@Ansible project]# ansible-playbook --syntax-check lamp.yml playbook: lamp.yml ## 执行一下 [root@Ansible project]# ansible-playbook lamp.yml PLAY [hosts] ****************************************************************************** TASK [Gathering Facts] ******************************************************************** ok: [192.168.1.2] ok: [192.168.1.3] TASK [Install Server] ********************************************************************* changed: [192.168.1.2] changed: [192.168.1.3] TASK [Unzip File] ************************************************************************* changed: [192.168.1.2] changed: [192.168.1.3] TASK [Started Httpd Server] *************************************************************** changed: [192.168.1.2] changed: [192.168.1.3] TASK [Started Mariadb Server] ************************************************************* changed: [192.168.1.2] changed: [192.168.1.3] TASK [Stopped Firewall Server] ************************************************************ ok: [192.168.1.3] ok: [192.168.1.2] PLAY RECAP ******************************************************************************** 192.168.1.2 : ok=6 changed=4 unreachable=0 failed=0 192.168.1.3 : ok=6 changed=4 unreachable=0 failed=0
这就安装好了浏览器访问 192.168.1.2/wordpress
或者 192.168.1.3/wordpress