1. 程式人生 > 其它 >ansible-playbook批量安裝httpd

ansible-playbook批量安裝httpd

安裝ansible

 [root@ansible ~]#yum install ansible 

編寫ansible主機清單:

[root@ansible ansible]# cat /etc/ansible/hosts 

[centos8]
192.168.18.130
192.168.18.132

編寫playbook實現批量安裝httpd

[root@ansible ansible]# vim install-httpd.yml 
---
- hosts: centos8
  gather_facts: yes

  tasks:
          - name: Install httpd
            yum: name=httpd state=present
          - name: create group
            group: name=apache gid=80 system=yes
          - name: create user
            user: name=apache uid=80 group=apache shell=/sbin/nologin
          - name: Install configure file
            copy: content="welcome {{ ansible_nodename }} \n" dest=/var/www/html/index.html
          - name: modify config
            lineinfile: path=/etc/httpd/conf/httpd.conf regexp='^Listen' line='Listen 8080'
          - name: start service
            service: name=httpd state=started enabled=yes

執行playbook


[root@ansible ansible]# ansible-playbook install-httpd.yml 

測試驗證

[root@ansible ansible]# curl 192.168.18.130:8080
welcome slave1 
[root@ansible ansible]# curl 192.168.18.132:8080
welcome slave2