版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
编写apache安装文件
[devlops@server1 ansible]$ ls
ansible.cfg inventory
[devlops@server1 ansible]$ vim playbook.yml
[devlops@server1 ansible]$ cat playbook.yml
---
- hosts: prod
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: start httpd
service:
name: httpd
state: started
[devlops@server1 ansible]$ ansible-playbook playbook.yml --syntax-check
[devlops@server1 ansible]$ ansible-playbook playbook.yml --list-hosts
[devlops@server1 ansible]$ ansible-playbook playbook.yml
[devlops@server1 ansible]$ vim playbook.yml
[devlops@server1 ansible]$ mkdir files
[devlops@server1 ansible]$ cd files/
[devlops@server1 files]$ scp server3:/etc/httpd/conf/httpd.conf .
httpd.conf 100% 11KB 11.5KB/s 00:00
[devlops@server1 files]$ cd ..
[devlops@server1 ansible]$ ansible-playbook playbook.yml
添加触发器
playbook.yml
---
- hosts: prod
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: configure httpd
copy:
src: files/httpd.conf
dest: /etc/httpd/conf/http.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd
service:
name: httpd
state: started
handlers: ##触发器
- name: restart httpd
service:
name: httpd
state: restarted
[devlops@server1 ansible]$ ansible-playbook playbook.yml --syntax-check
playbook: playbook.yml
[devlops@server1 ansible]$ ansible-playbook playbook.yml
添加测试文件
playbook.yml
---
- hosts: prod
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: test
copy:
src: files/index.html
dest: /var/www/html/index.html
- name: configure httpd
copy:
src: files/httpd.conf
dest: /etc/httpd/conf/http.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd and firewalld
service:
name: "{{ item }}"
state: started
loop:
- httpd
- firewalld
- name: configure firewalld
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
[devlops@server1 ansible]$ ansible-playbook playbook.yml --syntax-check
[devlops@server1 ansible]$ ansible-playbook playbook.yml
启动防火墙
[devlops@server1 ansible]$ cat playbook.yml
---
- hosts: prod
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: test
copy:
src: files/index.html
dest: /var/www/html/index.html
- name: configure httpd
copy:
src: files/httpd.conf
dest: /etc/httpd/conf/http.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd and firewalld
service:
name: "{{ item }}"
state: started
loop:
- httpd
- firewalld
- name: configure firewalld
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- hosts: localhost
become: no
tasks:
- name: test httpd
uri:
url: http://172.25.31.3
status_code: 200
修改变量
[devlops@server1 ansible]$ ls
ansible.cfg files inventory playbook.retry playbook.yml templates
[devlops@server1 ansible]$ cd templates/
[devlops@server1 templates]$ ls
httpd.conf.j2
[devlops@server1 templates]$ vim httpd.conf.j2
[devlops@server1 ansible]$ vim inventory
[devlops@server1 ansible]$ cat inventory
localhost
[test]
server2 http_host=172.25.31.2
[prod]
server3 http_host=172.25.31.3
[webserver:children]
test
prod
[devlops@server1 ansible]$ cat playbook.yml
---
- hosts: webserver
vars:
http_port: 80
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: copy index.html
copy:
src: files/index.html
dest: /var/www/html/index.html
- name: configure httpd
template:
src: templates/httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd and firewalld
service:
name: "{{ item }}"
state: started
loop:
- httpd
- firewalld
- name: configure firewalld
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- hosts: localhost
become: no
tasks:
- name: test httpd
uri:
url: http://172.25.31.3
status_code: 200
[devlops@server1 ansible]$ ansible-playbook playbook.yml
server2:
server3:
获取管理节点的信息
[devlops@server1 ansible]$ vim file.yml
[devlops@server1 ansible]$ cat file.yml
---
- hosts: webserver
tasks:
- name: create file
template:
src: templates/file.j2
dest: /tmp/file
[devlops@server1 ansible]$ ls
ansible.cfg files inventory playbook.yml
file.retry file.yml playbook.retry templates
[devlops@server1 ansible]$ cat templates/file.j2
主机名: {{ ansible_facts['hostname'] }}
主机IP: {{ ansible_facts['default_ipv4']['address'] }}
主机DNS: {{ ansible_facts['dns']['nameservers'][-1] }}
boot分区: {{ ansible_facts['devices']['sda']['partitions']['sda1']['size'] }}
内核: {{ ansible_facts['kernel'] }}
内存空闲: {{ ansible_facts['memfree_mb'] }}
[devlops@server1 ansible]$ ansible-playbook file.yml
server2:
server3:
ansible自动部署haproxy实现负载均衡
[root@server1 ansible]# vim /etc/sudoers
[root@server1 ansible]# su - devlops
[devlops@server1 ansible]$ cat playbook.yml
---
- hosts: webserver
vars:
http_port: 80
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: copy index.html
copy:
content: "{{ ansible_facts['hostname'] }}"
dest: /var/www/html/index.html
- name: configure httpd
template:
src: templates/httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd and firewalld
service:
name: "{{ item }}"
state: started
loop:
- httpd
- firewalld
- name: configure firewalld
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- hosts: localhost
tasks:
- name: install haproxy
yum:
name: haproxy
state: present
- name: configure haproxy
template:
src: templates/haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
notify: restart haproxy
- name: start haproxy
service:
name: haproxy
state: started
handlers:
- name: restart haproxy
service:
name: haproxy
state: restarted
获取haproxy配置文件
[devlops@server1 templates]$ vim haproxy.cfg.j2
[devlops@server1 ansible]$ ansible-playbook playbook.yml
测试:
添加server4到haproxy组里
[devlops@server1 ansible]$ vim inventory
[devlops@server1 ansible]$ cat inventory
[test1]
server4
[test]
server2 http_host=172.25.31.2
[prod]
server3 http_host=172.25.31.3
[webserver:children]
test
prod
test1
[devlops@server1 ansible]$ tail templates/haproxy.cfg.j2
#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
{% for host in groups['webserver'] %}
server {{ hostvars[host]['ansible_facts']['hostname'] }} {{ hostvars[host]['ansible_facts']['eth0']['ipv4']['address'] }}:80 check
{% endfor %}
---
- hosts: webserver
vars:
http_port: 80
tasks:
- name: install httpd
yum:
name: httpd
state: present
- name: copy index.html
copy:
content: "{{ ansible_facts['hostname'] }}"
dest: /var/www/html/index.html
- name: configure httpd
template:
src: templates/httpd.conf.j2
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 644
notify: restart httpd
- name: start httpd and firewalld
service:
name: "{{ item }}"
state: started
loop:
- httpd
- firewalld
- name: configure firewalld
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
- hosts: localhost
tasks:
- name: install haproxy
yum:
name: haproxy
state: present
- name: configure haproxy
template:
src: templates/haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
notify: restart haproxy
- name: start haproxy
service:
name: haproxy
state: started
handlers:
- name: restart haproxy
service:
name: haproxy
state: restarted
[devlops@server1 ansible]$ ansible-playbook playbook.yml
测试: