Tasks:任务,由模板定义的操作列表
Variables:变量
Templates:模板,即使用模板语法的文件
Handlers:处理器.当某条件满足时,触发执行的操作
Roles:角色
--- #标记文件的开始
- hosts: webservers #指定该playbook在哪个服务器上执行
vars: #表示下面是定义的变量,
http_port: 80 #变量的形式,key: value,这里http_port是变量名,80是值
max_clients: 200
remote_user: root #指定远程的用户名,这里缩进和vars保持了一致,说明变量的代码块已经结束。
tasks: #下面构成playbook的tasks,每个task都有 - name: 开始,name指定该任务的名称。
- name: ensure apache is at the latest version #指定该任务的名称。
yum: pkg=httpd state=latest #yum说明要是用的模板名称,后面指定对应的参数,这两行结合起来就相当于一个shell命令。
- name: write the apache config file #每个task之间可以使用空行来做区分。
template: src=/srv/httpd.j2 dest=/etc/httpd.conf
#需要说明的是缩进的意义和python中缩进的意义是一样,是来区分代码块的。
ansible-playbook playbook.yml --syntax-check
检查语法是否有误
ansible-playbook playbook.yml
执行
---
- hosts: test
tasks:
- name: install apache
yum:
name: httpd
state: present
- name: start apache
service:
name: httpd
state: started
enabled: yes
- name: start firewalld
service:
name: firewalld
state: started
enabled: yes
- name: enable http
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
mkdir ansible/httpd
存放httpd的配置文件
[devops@server1 ansible]$ cd httpd/
[devops@server1 httpd]$ ls
httpd.conf
---
- hosts: test
tasks:
- name: install apache
yum:
name: httpd
state: present
- name: config apache
copy:
src: httpd/httpd.conf
dest: /etc/httpd/httpd.conf
- name: start apache
service:
name: httpd
state: started
enabled: yes
- name: start firewalld
service:
name: firewalld
state: started
enabled: yes
- name: enable http
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
[devops@server1 httpd]$ mv httpd.conf httpd.conf.j2
[devops@server1 httpd]$ cd ..
[devops@server1 ansible]$ mkdir templates
[devops@server1 ansible]$ mv httpd/httpd.conf.j2 templates/
[devops@server1 ansible]$ cd templates/
[devops@server1 templates]$ ls
httpd.conf.j2
vim hosts
vim templates/httpd.conf.j2
---
- hosts: test
tasks:
- name: install apache
yum:
name: httpd
state: present
- name: config apache
template:
src: templates/httpd.conf.j2
dest: /etc/httpd/httpd.conf
notify: restart apache
- name: start apache
service:
name: httpd
state: started
enabled: yes
- name: start firewalld
service:
name: firewalld
state: started
enabled: yes
- name: enable http
firewalld:
service: http
permanent: yes
immediate: yes
state: enabled
handlers:
- name: restart apache
service:
name: httpd
state: restarted
vim playbook2.yml
---
- hosts: all
tasks:
- name: create hostinfo
template:
src: templates/hostinfo.j2
dest: /tmp/hostinfo
vim templates/hostinfo.j2
hostname: {
{ ansible_facts['hostname'] }}
hostip: {
{ ansible_facts["eth0"]["ipv4"]["address"] }}
DNS: {
{ ansible_facts['dns']['nameservers'][-1] }}
kernel: {
{ ansible_facts['kernel'] }}
memory_free: {
{ ansible_facts.memfree_mb }}MB
boot partition size: {
{ ansible_facts.devices.vda.partitions.vda1.size }}
[root@server20 ~]# cat /temp/hostinfo
hostname: server2
hostip: 192.168.43.2
DNS: 114.114.114.114
kernel: 3.10.0-957.el7.x86_64
memory_free: 582MB
boot partition size: 1.00 GB
ansible-vault encrypt playbook.yml
对它进行加密<