Ansible-Playbook部署lamp(循环)
环境
主机 | ip |
---|
Ansible | 192.168.200.152 |
受管主机 | 192.168.200.154 |
//首先这两台主机是能ping通的
[root@ansible playbook]
192.168.200.154 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
//编写部署lamp的install.yml
[root@ansible playbook]
---
- hosts: 192.168.200.154
tasks:
- name: install service
yum:
name: "{{ item }}"
state: present
loop:
- httpd
- httpd-devel
- mariadb
- mariadb-server
- mariadb-devel
- php
- php-mysql*
- php-*
- name: create user
user:
name: "{{ item }}"
system: yes
shell: /sbin/nologin
loop:
- httpd
- mysql
- www
- name: start service
service:
name: "{{ item }}"
state: started
enabled: yes
loop:
- httpd
- mariadb
- name: create index.php
shell: 'echo "<?php phpinfo(); ?>" > /var/www/html/index.php'
执行结果
[root@ansible ansible]
PLAY [192.168.200.154] *********************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.200.154]
TASK [install service] *********************************************************
changed: [192.168.200.154] => (item=httpd)
changed: [192.168.200.154] => (item=httpd-devel)
changed: [192.168.200.154] => (item=mariadb)
changed: [192.168.200.154] => (item=mariadb-server)
changed: [192.168.200.154] => (item=mariadb-devel)
changed: [192.168.200.154] => (item=php)
changed: [192.168.200.154] => (item=php-mysql*)
changed: [192.168.200.154] => (item=php-*)
TASK [create user] *************************************************************
changed: [192.168.200.154] => (item=httpd)
ok: [192.168.200.154] => (item=mysql)
changed: [192.168.200.154] => (item=www)
TASK [start service] ***********************************************************
changed: [192.168.200.154] => (item=httpd)
changed: [192.168.200.154] => (item=mariadb)
TASK [create index.php] ********************************************************
changed: [192.168.200.154]
PLAY RECAP *********************************************************************
192.168.200.154 : ok=2 changed=13 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
访问
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e9650fe6276125a4e8721d7988f69791.png)