1、环境
主机 | IP |
---|
ansible | 192.168.129.133 |
lamp | 192.168.192.135 |
2、编写playbook
[root@ansible project]
---
- hosts: httpd
tasks:
- name: Prints various Ansible facts
debug:
msg: >
The host named {{ ansible_facts["fqdn"] }} is
ip is {{ ansible_facts["default_ipv4"]["address"] }}
- name: stop firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: selinux
lineinfile:
path: /etc/selinux/config
regexp: '^SELINUX='
line: 'SELINUX=disabled'
state: present
- name: install all server
yum:
name: "{{ item }}"
state: present
loop:
- httpd*
- mariadb*
- php*
- php-*
- name: start service
service:
name: "{{ item }}"
state: started
enabled: yes
loop:
- httpd
- mariadb
- php-fpm
- name: create index.php
lineinfile:
path: /var/www/html/index.php
line: |
<?php
phpinfo();
?>
create: yes
- name: restart
service:
name: "{{ item }}"
state: started
loop:
- httpd.service
- php-fpm.service
- mariadb.service
3、文件加密
//加密
[root@ansible project]
Encryption successful
//解密
[root@ansible project]
Decryption successful
4、验证语法
[root@ansible project]
playbook: playbook/install.yml
5、 运行playbook
[root@ansible project]
PLAY [httpd] ************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************
ok: [192.168.129.135]
TASK [Prints various Ansible facts] *************************************************************************************
ok: [192.168.129.135] => {
"msg": "The host named LAMP.example.com is ip is 192.168.129.135\n"
}
TASK [stop firewalld] ***************************************************************************************************
changed: [192.168.129.135]
TASK [selinux] **********************************************************************************************************
changed: [192.168.129.135]
TASK [create user] ******************************************************************************************************
changed: [192.168.129.135] => (item={'name': 'apache'})
changed: [192.168.129.135] => (item={'name': 'mysql'})
TASK [install all server] ***********************************************************************************************
changed: [192.168.129.135] => (item=httpd*)
changed: [192.168.129.135] => (item=mariadb*)
changed: [192.168.129.135] => (item=php*)
changed: [192.168.129.135] => (item=php-*)
TASK [start service] ****************************************************************************************************
changed: [192.168.129.135] => (item=httpd)
changed: [192.168.129.135] => (item=mariadb)
changed: [192.168.129.135] => (item=php-fpm)
TASK [create index.php] *************************************************************************************************
changed: [192.168.129.135]
PLAY RECAP **************************************************************************************************************
192.168.129.135 : ok=7 changed=7 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
6、访问测试
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c38f1683ddb646446fccce0ce7e37ccb.png#pic_center)