用ansible循环安装lamp
环境
主机名 | IP地址 | 安装服务 |
---|
control | 192.168.56.129 | ansible | server | 192.168.56.132 | apache |
编写playbook
[root@control lamp]# cat LL.yml
---
- name: firewalld
hosts: server3
tasks:
- name: stop firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/selinux/config
regexp: "^SELINUX=enforcing"
line: SELINUX=disabled
- name: setenforce 0
shell: "setenforce 0"
failed_when: false
- name: create users
user:
name: "{{ item }}"
system: yes
state: present
loop:
- name
- my
- name: install service
yum:
name: " {{ item }}"
state: present
loop:
- httpd
- php
- mariadb
- mariadb-server
- name: start service
service:
name: "{{ item }}"
state: started
enabled: yes
loop:
- httpd
- php-fpm
- mariadb
- name: Create a file if it does not exist
file:
path: /var/www/html/index.php
state: touch
mode: 0775
- name: ehco index.php
lineinfile:
path: /var/www/html/index.php
line: |
<?php
phpinfo();
?>
运行
[root@control book]# ansible-playbook lamp/site.yaml
PLAY [firewalld] ***************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.56.132]
TASK [stop firewalld] **********************************************************
ok: [192.168.56.132]
TASK [stop selinux] ************************************************************
changed: [192.168.56.132]
TASK [setenforce 0] ************************************************************
changed: [192.168.56.132]
TASK [create users] ************************************************************
changed: [192.168.56.132] => (item=name)
changed: [192.168.56.132] => (item=my)
TASK [install service] ********************************************************
changed: [192.168.56.132] => (item=httpd)
changed: [192.168.56.132] => (item=php)
changed: [192.168.56.132] => (item=mariadb)
changed: [192.168.56.132] => (item=mariadb-server)
TASK [start service] ***********************************************************
changed: [192.168.56.132] => (item=httpd)
changed: [192.168.56.132] => (item=php-fpm)
changed: [192.168.56.132] => (item=mariadb)
TASK [Create a file if it does not exist] **************************************
changed: [192.168.56.132]
TASK [ehco index.php] ********************************************************
changed: [192.168.56.132]
PLAY RECAP *********************************************************************
192.168.56.132 : ok=9 changed=7 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
查看端口
[root@server3 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
查看结果
[外链图片转存中…(img-cRV7lc4R-1627267591781)]
|