1、# 安装软件
创建一个名为/home/devops/ansible/packages.yml的剧本
- 在dev, prod 和 test 主机组中安装 php 和 mariadb 软件包
- 在dev 主机组中安装 Development Tools 包组
- 升级dev主机组中主机的所有软件包
(1)编写剧本
[devops@workstation ansible]$ vim /home/devops/ansible/packages.yml
---
- name: Istallphp and mariadb
hosts: dev,test,prod
tasks:
- name: Install php and mariadb
yum:
name: "{{ item }}" #使用变量的形式代替软件名
state: present
loop: #变量列表
- php
- mariadb
- name: Install Development Tools and update software
hosts: dev
tasks:
- name: Install Deveolpment Tools
yum:
name: "@Development Tools"
state: present
- name: update software
yum:
name: "*"
state: latest
(2)结果
[devops@workstation ansible]$ ansible-playbook packages.yml
PLAY [Istallphp and mariadb] ****************************************************
TASK [Gathering Facts] **********************************************************
ok: [serverc]
ok: [serverb]
ok: [servera]
ok: [serverd]
TASK [Install php and mariadb] **************************************************
ok: [serverd] => (item=php)
ok: [serverb] => (item=php)
ok: [serverc] => (item=php)
ok: [servera] => (item=php)
ok: [serverc] => (item=mariadb)
ok: [serverd] => (item=mariadb)
ok: [serverb] => (item=mariadb)
ok: [servera] => (item=mariadb)
PLAY [Install Development Tools and update software] ****************************
TASK [Gathering Facts] **********************************************************
ok: [servera]
TASK [Install Deveolpment Tools] ************************************************
ok: [servera]
TASK [update software] **********************************************************
ok: [servera]
PLAY RECAP **********************************************************************
servera : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverb : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverc : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
serverd : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
(3)测试
[devops@workstation ansible]$ ansible dev,prod,test -m shell -a 'rpm -aq | egrep "php|mariadb"'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'. If you need to use command because yum, dnf or zypper
is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
serverb | CHANGED | rc=0 >>
php-common-7.2.11-1.module+el8+2561+1aca3413.x86_64
mariadb-connector-c-config-3.0.7-1.el8.noarch
php-cli-7.2.11-1.module+el8+2561+1aca3413.x86_64
php-fpm-7.2.11-1.module+el8+2561+1aca3413.x86_64
php-7.2.11-1.module+el8+2561+1aca3413.x86_64
mariadb-common-10.3.11-1.module+el8+2765+cfa4f87b.x86_64
mariadb-connector-c-3.0.7-1.el8.x86_64
mariadb-10.3.11-1.module+el8+2765+cfa4f87b.x86_64
serverd | CHANGED | rc=0 >>
php-common-7.2.11-1.module+el8+2561+1aca3413.x86_64
mariadb-connector-c-config-3.0.7-1.el8.noarch
php-cli-7.2.11-1.module+el8+2561+1aca3413.x86_64
php-fpm-7.2.11-1.module+el8+2561+1aca3413.x86_64
php-7.2.11-1.module+el8+2561+1aca3413.x86_64
mariadb-common-10.3.11-1.module+el8+2765+cfa4f87b.x86_64
mariadb-connector-c-3.0.7-1.el8.x86_64
mariadb-10.3.11-1.module+el8+2765+cfa4f87b.x86_64
serverc | CHANGED | rc=0 >>
php-common-7.2.11-1.module+el8+2561+1aca3413.x86_64
mariadb-connector-c-config-3.0.7-1.el8.noarch
php-cli-7.2.11-1.module+el8+2561+1aca3413.x86_64
php-fpm-7.2.11-1.module+el8+2561+1aca3413.x86_64
php-7.2.11-1.module+el8+2561+1aca3413.x86_64
mariadb-common-10.3.11-1.module+el8+2765+cfa4f87b.x86_64
mariadb-connector-c-3.0.7-1.el8.x86_64
mariadb-10.3.11-1.module+el8+2765+cfa4f87b.x86_64
servera | CHANGED | rc=0 >>
mariadb-connector-c-config-3.0.7-1.el8.noarch
php-cli-7.2.11-1.module+el8+2561+1aca3413.x86_64
mariadb-connector-c-3.0.7-1.el8.x86_64
mariadb-10.3.11-1.module+el8+2765+cfa4f87b.x86_64
php-fpm-7.2.11-1.module+el8+2561+1aca3413.x86_64
php-7.2.11-1.module+el8+2561+1aca3413.x86_64
mariadb-common-10.3.11-1.module+el8+2765+cfa4f87b.x86_64
php-common-7.2.11-1.module+el8+2561+1aca3413.x86_64
[devops@workstation ansible]$ ansible dev -m shell -a 'yum grouplist installed "Development Tools"'
[WARNING]: Consider using the yum module rather than running 'yum'. If you need to use command because yum is insufficient you can add
'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
servera | CHANGED | rc=0 >>
Last metadata expiration check: 2:23:14 ago on Thu 07 Jan 2021 12:15:22 PM CST.
Installed Groups:
Development Tools
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内容为
6> 第二个剧本名为 Test internet web server
7> 利用uri模块从控制节点测试serverb上运行web服务,检查返回状态代码200.
8> 验证internet.yml playbook的语法。
9> 使用ansible-playbook 运行剧本。
1、配置文件
vim /home/devops/ansible/internet.yml
---
- name: Enable internet services
hosts: serverb
tasks:
- name: install software
yum:
name:
- firewalld
- httpd
- mariadb-server
- php
- php-mysqlnd
state: latest
- name: start firewalld
service:
name: firewalld
state: started
enabled: yes
- name: firewalld roles
firewalld:
service: http
permanent: yes
permanent: yes
immediate: yes
state: enabled
- name: start httpd
service:
name: httpd
state: started
enabled: yes
- name: index.php
copy:
src: files/index.php
dest: /var/www/html/index.php
- name: Test internet web server
hosts: localhost
tasks:
- name: test web server
uri:
url: http://serverb/index.php
return_content: yes
status_code: 200
2、创建files文件
mkdir files
vim files/index.php
<?
echo "Hello Ansible"
?>
3、验证语法
ansible-playbook internet.yml --syntax-check