1、# 安装软件
创建一个名为/home/devops/ansible/packages.yml的剧本
- 在dev, prod 和 test 主机组中安装 php 和 mariadb 软件包
- 在dev 主机组中安装 Development Tools 包组
- 升级dev主机组中主机的所有软件包
[devops@workstation ~]$ vim /home/devops/ansible/packages.yml
---
- name: play 1
hosts: dev,prod,test
tasks:
- name: install php and mariadb
yum:
name: "{{ packages }}"
vars:
packages:
- php
- mariadb
- name: play 2
hosts: dev
tasks:
- name: install the 'Development tools' package group
yum:
name: "@Development tools"
state: present
- name: upgrade all packages
yum:
name: '*'
state: latest
[devops@workstation ansible]$ ansible-playbook packages.yml
2、多剧本练习
1> 新建一个playbook,/home/devops/ansible/internet.yml
第一个剧本名为Enable internet services,受管主机为serverb.lab.example.com
2> 安装软件firewalld、httpd、mariadb-server、php和php-mysqlnd的最新版。
3> 确保服务firewalld服务处于enabled和running状态,并允许访问httpd服务。
4> 确保httpd和Maria服务处于enabled和running状态。
5> web主页index.php内容为
<?php
echo "Hello Ansible!";
?>
---
- name: Enable internet services
hosts: serverb
tasks:
- name: install softwore
yum:
name:
- firewalld
- httpd
- mariadb-server
- php
- php-mysqlnd
state: latest
- name: start firewall
service:
name: firewalld
state: started
enabled: yes
刚开始enable没有写d出现错误
- name: firewall rule
firewalld:
service: http
permanent: yes
permanent: yes
immediate: yes
- name: start httpd
service:
name: httpd
state: started
enabled: yes
- name: index.php
copy:
src: files/index.php
dest: /var/www/html/index.php
出现下面错误的原因是没有写这段话,所以要创建文件
[devops@workstation ansible]$ mkdir files
[devops@workstation ansible]$ vim files/index.php
5> web主页index.php内容为
<?php
echo "Hello Ansible!";
?>
6> 第二个剧本名为 Test internet web server
7> 利用uri模块从控制节点测试serverb上运行web服务,检查返回状态代码200.
8> 验证internet.yml playbook的语法。
9> 使用ansible-playbook 运行剧本。
- name: Test internet web server
hosts: localhost
tasks:
- name: test web server
uri:
url: http://serverb/index.php
return_content: yes
status_code: 200
将两个剧本写在一起
验证
[devops@workstation ansible]$ ansible-playbook internet.yml --syntax-check
添加完文件Hello Ansible!后 输入 curl serverb出现Hello Ansible表示成功
注意:
出现fatal: [serverb]: FAILED! => {"changed": false, "msg": "missing required arguments: state"}
需要在模块中添加state
- name: firewall rule
firewalld:
service: http
permanent: yes
permanent: yes
immediate: yes
state: enabled