[root@server ~]# cd /etc/ansible/
[root@server ansible]# ls
ansible.cfg hosts httpd.yml mysqld.yml php.yml roles
[root@server ansible]# vim hosts
[dev]
node1
node2
[harproxy]
node3
[root@server ansible]# cd roles/
[root@server roles]# ls
apache mysql php
[root@server roles]# ansible-galaxy init httpd
- Role httpd was created successfully
[root@server roles]# ansible-galaxy init haproxy
- Role haproxy was created successfully
[root@server roles]# ls
apache haproxy httpd mysql php
[root@server roles]# vim httpd/tasks/main.yml
[root@server roles]# cat httpd/tasks/main.yml
---
# tasks file for httpd
- name: set firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/sysconfig/selinux
regexp: '^SELINUX='
line: 'SELINUX=disabled'
- name: setenforce 0
shell: setenforce 0
- name: install httpd
yum:
name: httpd
state: present
- name: index.html.j2
template:
src: index.html.j2
dest: /var/www/html/index.html
- name: restart httpd
service:
name: httpd
state: restarted
enabled: yes
[root@server roles]# vim httpd/templates/index.html.j2
[root@server roles]# cat httpd/templates/index.html.j2
hello {{ ansible_fqdn }} is {{ ansible_ens33.ipv4.address }}
[root@server ansible]# vim httpd-2.yml
[root@server ansible]# cat httpd-2.yml
---
- name: use httpd roles
hosts: node2
roles:
- httpd
[root@server ansible]# ansible-playbook httpd-2.yml
PLAY [use httpd roles] ********************************************************************************
TASK [Gathering Facts] ********************************************************************************
ok: [node2]
TASK [httpd : set firewalld] **************************************************************************
changed: [node2]
TASK [httpd : stop selinux] ***************************************************************************
changed: [node2]
TASK [httpd : setenforce 0] ***************************************************************************
changed: [node2]
TASK [install httpd] **********************************************************************************
changed: [node2]
TASK [httpd : index.html.j2] **************************************************************************
changed: [node2]
TASK [restart httpd] **********************************************************************************
changed: [node2]
PLAY RECAP ********************************************************************************************
node2 : ok=7 changed=6 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
[root@server ansible]# curl http://node2
hello node2.example.com is 192.168.181.182
[root@server ansible]# vim roles/haproxy/tasks/main.yml
[root@server ansible]# cat roles/haproxy/tasks/main.yml
---
# tasks file for haproxy
- name: - name: set firewalld
service:
name: firewalld
state: stopped
enabled: no
- name: stop selinux
lineinfile:
path: /etc/sysconfig/selinux
regexp: '^SELINUX='
line: 'SELINUX=disabled'
- name: setenforce 0
shell: setenforce 0
- name: install haproxy
yum:
name: haproxy
state: present
- name: haproxy.cfg.j2
template:
src: haproxy.cfg.j2
dest: /etc/haproxy/haproxy.cfg
- name: restart haproxy
sercice:
name: haproxy
state: restarted
enabled: yes
先在ansible节点yum安装haproxy复制他的haproxy.cfg,编写模板文件
[root@server ~]# yum -y install haproxy
[root@server ansible]# cp /etc/haproxy/haproxy.cfg roles/haproxy/templates/haproxy.cfg.j2
[root@server ansible]# vim roles/haproxy/templates/haproxy.cfg.j2
frontend main
bind *:80
backend app
balance roundrobin
{% for host in groups.dev %}
server {{ hostvars[host].ansible_fqdn }} {{ hostvars[host].ansible_ens33.ipv4.address }}:80 check
{% endfor %}
[root@server ansible]# vim haproxy.yml
---
- name: dev shishi
hosts: dev
- name: use haproxy roles
hosts: node3
roles:
- haproxy
[root@server ansible]# ansible-playbook haproxy.yml
PLAY [dev shishi] *******************************************************************************************
TASK [Gathering Facts] **************************************************************************************
ok: [node2]
ok: [node1]
PLAY [use haproxy roles] ************************************************************************************
TASK [Gathering Facts] **************************************************************************************
ok: [node3]
TASK [haproxy : set firewalld] ******************************************************************************
ok: [node3]
TASK [haproxy : stop selinux] *******************************************************************************
ok: [node3]
TASK [haproxy : setenforce 0] *******************************************************************************
changed: [node3]
TASK [install haproxy] **************************************************************************************
ok: [node3]
TASK [haproxy.cfg.j2] ***************************************************************************************
changed: [node3]
TASK [restart haproxy] **************************************************************************************
changed: [node3]
PLAY RECAP **************************************************************************************************
node1 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node2 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
node3 : ok=7 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0