淘先锋技术网

首页 1 2 3 4 5 6 7

ansible playbook安装lamp架构(循环)

相关环境:

主机IP地址
ansible192.168.200.135
lnmp192.168.200.136

1.关闭防护墙

---
- hosts: apache
  tasks:
    - name: stop firewalld
      service:
        name: firewalld
        state: stopped

    - name: enabled
      lineinfile:
        path: /etc/selinux/config
        regexp: "^SELINUX=enforcing "
        line: "SELINUX=disabled"
        state: present

    - name: setenforce
      shell: 
        setenforce 0 
//运行playbook
[root@localhost project]# ansible-playbook lnmp/firewalld.yml 

PLAY [apache] ************************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [192.168.200.136]

TASK [stop firewalld] ****************************************************************************************************************************************************
changed: [192.168.200.136]

TASK [enabled] ***********************************************************************************************************************************************************
changed: [192.168.200.136]

TASK [setenforce] ********************************************************************************************************************************************************
changed: [192.168.200.136]

PLAY RECAP ***************************************************************************************************************************************************************
192.168.200.136            : ok=4    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

2.编写lamp架构playbook

---
- hosts: apache
  tasks:
    - name: create user
      user:
        name: "{{item.name}}"
        shell: /sbin/nologin
        system: yes
        state: present
      loop: 
        - name: apache
        - name: mariadb
        - name: php

    - name: install pacages
      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'

//运行playbook
[root@localhost project]# ansible-playbook lnmp.yml 

PLAY [apache] **************************************************************************************

TASK [Gathering Facts] *****************************************************************************
ok: [192.168.200.136]

TASK [create user] *********************************************************************************
ok: [192.168.200.136] => (item={u'name': u'apache'})
ok: [192.168.200.136] => (item={u'name': u'mariadb'})
ok: [192.168.200.136] => (item={u'name': u'php'})

TASK [install pacages] *****************************************************************************
changed: [192.168.200.136] => (item=httpd)
changed: [192.168.200.136] => (item=httpd-devel)
changed: [192.168.200.136] => (item=mariadb)
changed: [192.168.200.136] => (item=mariadb-server)
changed: [192.168.200.136] => (item=mariadb-devel)
changed: [192.168.200.136] => (item=php)
changed: [192.168.200.136] => (item=php-mysql*)
changed: [192.168.200.136] => (item=php-*)

TASK [start service] *******************************************************************************
changed: [192.168.200.136] => (item=httpd)
changed: [192.168.200.136] => (item=mariadb)

TASK [create html] *********************************************************************************
changed: [192.168.200.136]

PLAY RECAP *****************************************************************************************
192.168.200.136            : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
[root@localhost project]# 

3.在网页上测试

在这里插入图片描述