当前位置: 代码迷 >> 综合 >> Ansible-Playbook部署lamp(循环)
  详细解决方案

Ansible-Playbook部署lamp(循环)

热度:51   发布时间:2023-12-02 12:35:28.0

Ansible-Playbook部署lamp(循环)

环境

主机 ip
Ansible 192.168.200.152
受管主机 192.168.200.154
//首先这两台主机是能ping通的
[root@ansible playbook]# ansible 192.168.200.154 -m ping
192.168.200.154 | SUCCESS => {
    "ansible_facts": {
    "discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"
}
//编写部署lamp的install.yml
[root@ansible playbook]# vim install.yml
---
- hosts: 192.168.200.154tasks: - name: install serviceyum: name: "{
    { item }}"state: presentloop: - httpd- httpd-devel- mariadb- mariadb-server- mariadb-devel- php- php-mysql*- php-*- name: create useruser: name: "{
    { item }}"system: yesshell: /sbin/nologinloop: - httpd- mysql- www- name: start serviceservice: name: "{
    { item }}"state: startedenabled: yesloop: - httpd- mariadb- name: create index.phpshell: 'echo "<?php phpinfo(); ?>" > /var/www/html/index.php'

执行结果

[root@ansible ansible]# ansible-playbook /opt/playbook/install.yml 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   

访问

在这里插入图片描述