role_name:定义的role名字
/etc/ansible/ansible.cfg
中定义:roles_path = /etc/ansible/roles
su - devops
cd ansible
mkdir roles
[devops@server1 ansible]$ vim ansible.cfg
[defaults]
inventory = ./inventory
roles_path = ./roles #当前目录下
[privilege_escalation]
become=True
become_method=sudo
become_user=root
become_ask_pass=False
[devops@server1 roles]$ ansible-galaxy init apache
[devops@server1 roles]$ cd apache/
[devops@server1 apache]$ ls
defaults files handlers meta README.md tasks templates tests vars
[devops@server1 apache]$ rm -fr README.md tests #可以删除这两个文件目录
[devops@server1 ansible]$ pwd
/home/devops/ansible
[devops@server1 ansible]$ ansible-galaxy list #列出所有的galaxy
[devops@server1 apache]$ cp ~/ansible/templates/httpd.conf.j2 templates/
[devops@server1 apache]$ cd templates/
[devops@server1 templates]$ ls
httpd.conf.j2
[devops@server1 apache]$ \vi tasks/main.yml
---
- name: install httpd
yum:
name: httpd
state: present
- name: copy httpd
copy:
content: "{{ ansible_facts['hostname'] }}"
dest: /var/www/html/index.html
- name: configure httpd
template:
src: 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
[devops@server1 apache]$ \vi handlers/main.yml
---
- name: restart httpd
service:
name: httpd
state: restarted
[devops@server1 ansible]$ vim roles/apache/vars/main.yml
---
http_host: "{{ ansible_facts['default_ipv4']['address'] }}"
http_port: 80
[devops@server1 ansible]$ vim apache.yml
---
- hosts: webserver
roles:
- apache
[devops@server1 ansible]$ vim inventory
[test]
server2
[prod]
server3
server4
[webserver:children]
test
prod
[devops@server1 ansible]$ ansible-playbook apache.yml -C #只检测,不做任何修改
[devops@server1 ansible]$ ansible-playbook apache.yml
[devops@server1 roles]$ ansible-galaxy init haproxy
- haproxy was created successfully
[devops@server1 roles]$ ls
apache haproxy
[devops@server1 haproxy]$ \vi handlers/main.yml
---
- name: restart haproxy
service:
name: haproxy
state: restarted
[devops@server1 haproxy]$ cp ~/ansible/templates/haproxy.cfg.j2 templates/
[devops@server1 haproxy]$ cd templates/
[devops@server1 templates]$ ls
haproxy.cfg.j2
[devops@server1 haproxy]$ \vi tasks/main.ym
---
- name: install haproxy
yum:
name: haproxy
state: present
- name: configure haproxy
template:
src: haproxy.cfg.j2 #注意修改路径
dest: /etc/haproxy/haproxy.cfg
notify: restart haproxy
- name: start haproxy
service:
name: haproxy
state: started
---
- hosts: all
tasks:
- import_role:
name: apache
when: ansible_hostname in groups['webserver']
- import_role:
name: haproxy
when: ansible_hostname in groups['lb']
[devops@server1 ansible]$ vim inventory
[lb]
server1 #写localhost则会提示skipping
[test]
server2
[prod]
server3
[webserver:children]
test
prod
[devops@server1 ansible]$ ansible-playbook apache.yml
[devops@server1 ansible]$ systemctl status haproxy
[devops@server1 ansible]$ netstat -antlp
在博客https://blog.csdn.net/even160941/article/details/99128262中。