Ansible 使用循环搭建lamp
实验环境
ansible |
192.168.200.142 |
lamp |
192.168.200.145 |
1.关闭防火墙
[root@localhost opt]# ansible-playbook lamp/firewalld.yml
PLAY [apache] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.145]
TASK [stop firewalld] **********************************************************
ok: [192.168.200.145]
TASK [lineinfile] **************************************************************
ok: [192.168.200.145]
TASK [shell] *******************************************************************
changed: [192.168.200.145]
PLAY RECAP *********************************************************************
192.168.200.145 : ok=4 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2.编写playbook
[root@localhost opt]# vim lamp.yml
---
- hosts: apache
tasks:
- name: create user
user:
name: "{{ item.name }}"
shell: /sbin/nologin
system: yes
state: present
loop:
- name: apache
- name: mysql
- name: install packages
yum:
name: "{{ item }}"
state: present
loop:
- httpd
- httpd-devel
- mariadb
- mariadb-server
- mariadb-devel
- php
- php-mysql*
- php-*
- name: start service
service:
name: "{{ item }}"
state: started
enabled: yes
loop:
- httpd
- mariadb
- name: create html
shell: 'echo "<?php phpinfo(); ?>" > /var/www/html/index.php'
3.运行
[root@localhost opt]# ansible-playbook lamp.yml
PLAY [apache] ******************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.145]
TASK [create user] *************************************************************
ok: [192.168.200.145] => (item={'name': 'apache'})
ok: [192.168.200.145] => (item={'name': 'mysql'})
TASK [install packages] ********************************************************
changed: [192.168.200.145] => (item=httpd)
changed: [192.168.200.145] => (item=httpd-devel)
changed: [192.168.200.145] => (item=mariadb)
changed: [192.168.200.145] => (item=mariadb-server)
changed: [192.168.200.145] => (item=mariadb-devel)
changed: [192.168.200.145] => (item=php)
changed: [192.168.200.145] => (item=php-mysql*)
changed: [192.168.200.145] => (item=php-*)
TASK [start service] ***********************************************************
changed: [192.168.200.145] => (item=httpd)
changed: [192.168.200.145] => (item=mariadb)
TASK [create html] *************************************************************
changed: [192.168.200.145]
PLAY RECAP *********************************************************************
192.168.200.145 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@localhost opt]# vim lamp.yml
4.访问