ansible中使用的yaml基本元素
變量
Inventory
條件測試
迭代
playbook的組成結構
Inventory
Modules
Ad Hoc Commands
Playbook:
tasks:
variable:
template:
handlers:
roles:
基本結構:
- hosts: 192.168.19.139
remote_user: root
roles:
- websrs
1、創建nginx用戶組和用戶,使用最簡單的playbooks。
[root@linux-host1 ansibles]# vim nginx.yml
- hosts: websrs
remote_user: root
tasks:
- name: create nginx group
group: name=nginx system=yes gid=2005
- name: create nginx user
user: name=nginx uid=2008 group=nginx system=yes
- hosts: dbsrs
remote_user: root
tasks:
- name: copy file to dbsrs
copy: src=/etc/inittab dest=/tmp/inittab.ans
[root@linux-host1 ansibles]# ansible-playbook nginx.yml
[root@master ~]# tail -1 /etc/group
nginx:x:2005:
[root@master ~]# tail -1 /etc/passwd
nginx:x:2008:2005:Nginx web server:/var/lib/nginx:/sbin/nologin
[root@linux-host1 ~]# cd /root/ansibles/httpds
[root@linux-host1 httpds]# mkdir templates
[root@linux-host1 httpds]# grep '{{' templates/httpd.conf.j2
Listen {{ http_port }}
ServerName {{ ansible_fqdn }}
[root@linux-host1 httpds]# cat /etc/ansible/hosts
[websrs]
192.168.19.132 http_port=81
192.168.19.139 http_port=82
[root@linux-host1 httpds]# cat apache.yml
- hosts: websrs
remote_user: root
vars:
- package: httpd
- service: httpd
tasks:
- name: install httpd package
yum: name={{ package }} state=latest
- name: install configuration file for httpd
template: src=/root/ansibles/httpds/templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd
service: enabled=true name={{ service }} state=started
handlers:
- name: restart httpd
service: name=httpd state=restarted
[root@linux-host1 httpds]#
Notify跟handler匹配結合使用
驗證:
[root@master ~]# curl -I 192.168.19.132:81
[root@master ~]# curl -I 192.168.19.139:82
只想運行某一個task,沒必要每次都執行。
每個都可以打標記。
role避免代碼多次調用
[root@linux-host1 ansibles]# mkdir -pv ansible_playbooks/roles/{websrs,dbsrs}/{templates,files,tasks,meta,handlers,vars}
[root@linux-host1 ansibles]# tree ansible_playbooks/
[root@linux-host1 ansibles]# cd ansible_playbooks/roles/websrs/
[root@linux-host1 websrs]# cp /etc/httpd/conf/httpd.conf files/
[root@linux-host1 websrs]# cd ../../
[root@linux-host1 ansible_playbooks]# grep "888" roles/websrs/files/httpd.conf
Listen 888
[root@linux-host1 ansible_playbooks]# cat roles/websrs/tasks/main.yml
- name: install httpd packages
yum: name=httpd
- name: install configuration file
copy: src=httpd.conf dest=/etc/httpd/conf/httpd.conf
tags:
- conf
notify:
- restart httpd
- name: start httpd
service: name=httpd state=started
[root@linux-host1 ansible_playbooks]# cat roles/websrs/handlers/main.yml
- name: restart httpd
service: name=httpd state=restarted
[root@linux-host1 ansible_playbooks]#
[root@linux-host1 ansible_playbooks]# cat site.yml
- hosts: websrs
remote_user: root
roles:
- websrs
[root@linux-host1 ansible_playbooks]#
[root@linux-host1 ansible_playbooks]# ansible-playbook site.yml
[root@master ~]# netstat -lnpt|grep http
tcp6 0 0 :::888 :::* LISTEN 5239/httpd
websrs部分任務完成,驗證通過。
每一個角色能夠獨立應用。
[root@linux-host1 ~]# yum install redis -y
安裝mariadb-server只是為了獲得/etc/my.cnf文件。
[root@linux-host1 ansible_playbooks]# cd roles/dbsrs/
[root@linux-host1 ansible_playbooks]# vim site.yml
- hosts: 192.168.19.139
remote_user: root
roles:
- websrs
- hosts: 192.168.19.130
remote_user: root
roles:
- dbsrs
- hosts: 192.168.19.132
remote_user: root
roles:
- websrs
- dbsrs
[root@linux-host1 ansible_playbooks]# cat roles/dbsrs/tasks/main.yml
- name: install redis-server packages
yum: name=redis state=latest
- name: install configuration file
copy: src=redis.conf dest=/etc/redis.conf
tags:
- myconf
notify:
- restart redis
- name: start redis
service: name=redis enabled=true state=started
[root@linux-host1 ansible_playbooks]# cat roles/dbsrs/handlers/main.yml
- name: restart redis
service: name=redis state=restarted
啟動playbook項目,
[root@master ~]# lsof -i:6379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 8649 redis 4u IPv4 64980 0t0 TCP *:6379 (LISTEN)
完成playbook應用。