参考:https://yaml.org/
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应用。