环境准备:
主机 | ip |
---|---|
ansible | 192.168.235.135 |
lamp | 192.168.235.145 |
创建目录及文件
[root@ansible lamp]# tree
.
├── files
│ ├── httpd.conf
│ ├── httpd-vhosts.conf
│ ├── index.php
│ └── www.conf
├── tasks
│ └── lamp.yml
└── vars
├── package.yml
├── server.yml
├── user.yml
└── vault.yml
编辑配置文件
[root@ansible files]# cat httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/var/www/html"
ServerName www.example.com
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
<Directory "/var/www/html">
Options none
AllowOverride none
Require all granted
</Directory>
</VirtualHost>
[root@ansible files]# cat index.php
<?php
phpinfo();
?>
[root@ansible vars]# cat server.yml
server:
- httpd
- mariadb
- php-fpm
[root@ansible vars]# cat user.yml
name:
- apache
- mysql
- php
[root@ansible vars]# cat package.yml
tools:
- httpd*
- mariadb*
- php*
[root@ansible vars]# cat vault.yml
.pass=1
[root@ansible lamp]# cat tasks/lamp.yml
---
- name: "install httpd* mariadb* php* service"
hosts: lamp
gather_facts: no
vars_files:
- /etc/ansible/lamp/vars/package.yml
- /etc/ansible/lamp/vars/user.yml
- /etc/ansible/lamp/vars/server.yml
tasks:
- name: "install"
yum:
name: "{{ item }}"
state: present
loop: "{{ tools }}"
- name: "create user"
user:
name: "{{ item }}"
state: present
system: yes
create_home: no
shell: /sbin/nologin
loop: "{{ name }}"
- name: "copy httpd.conf "
copy:
src: /etc/ansible/lamp/files/httpd.conf
dest: /etc/httpd/conf/httpd.conf
- name: "copy vhosts.conf"
copy:
src: /etc/ansible/lamp/files/httpd-vhosts.conf
dest: /etc/httpd/conf.d/httpd-vhosts.conf
- name: "copy index.php"
copy:
src: /etc/ansible/lamp/files/index.php
dest: /var/www/html/index.php
- name: "copy www.conf"
copy:
src: /etc/ansible/lamp/files/www.conf
dest: /etc/php-fpm.d/www.conf
- name: "stopped firewalld"
service:
name: firewalld
state: stopped
enabled: no
- name: "httpd mariadb php enabled"
service:
name: "{{ item }}"
state: started
enabled: yes
loop: "{{ server }}"
运行:
[root@ansible lamp]# ansible-playbook --vault-id @prompt tasks/lamp.yml
Vault password (default): //输入密码
[WARNING]: Found variable using reserved name: name
PLAY [install httpd* mariadb* php* service] *******************************************
TASK [install] ************************************************************************
ok: [192.168.235.145] => (item=httpd*)
ok: [192.168.235.145] => (item=mariadb*)
ok: [192.168.235.145] => (item=php*)
TASK [create user] ********************************************************************
ok: [192.168.235.145] => (item=apache)
ok: [192.168.235.145] => (item=mysql)
ok: [192.168.235.145] => (item=php)
TASK [copy httpd.conf] ****************************************************************
ok: [192.168.235.145]
TASK [copy vhosts.conf] ***************************************************************
ok: [192.168.235.145]
TASK [copy index.php] *****************************************************************
ok: [192.168.235.145]
TASK [copy www.conf] ******************************************************************
ok: [192.168.235.145]
TASK [stopped firewalld] **************************************************************
ok: [192.168.235.145]
TASK [httpd mariadb php enabled] ******************************************************
ok: [192.168.235.145] => (item=httpd)
ok: [192.168.235.145] => (item=mariadb)
ok: [192.168.235.145] => (item=php-fpm)
PLAY RECAP ****************************************************************************
192.168.235.145 : ok=8 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0