标签:总结 creat install roc psr 自定义 改变 元素 没有
介绍ansible-playbook <filename.yml> ... [options]
---
- hosts: all # 代表在所有主机执行
remote_user: root #以root身份执行
tasks:
- name: hello world #标签名
command: /usr/bin/wall hello world #执行的命令
SHELL脚本
#!/bin/bash
# 安装Apache
yum install --quiet -y httpd
# 复制配置文件
cp /tmp/httpd.conf /etc/httpd/conf/httpd.conf
cp/tmp/vhosts.conf /etc/httpd/conf.d/
# 启动Apache,并设置开机启动
service httpd start
chkconfig httpd on
Playbook定义
---
- hosts: all
tasks:
- name: "安装Apache"
yum: name=httpd
- name: "复制配置文件"
copy: src=/tmp/httpd.conf dest=/etc/httpd/conf/
- name: "复制配置文件"
copy: src=/tmp/vhosts.conf dest=/etc/httpd/conf.cd/
- name: "启动Apache,并设置开机启动"
service: name=httpd state=started enabled=yes
- Handlers
是task列表,这些task与前述的task并没有本质上的不同,用于当关注的资源发生
变化时,才会采取一定的操作- Notify此action可用于在每个play的最后被触发,这样可避免多次有改变发生
时每次都执行指定的操作,仅在所有的变化发生完成后一次性地执行指定操作。
在notify中列出的操作称为handler,也即notify中调用handler中定义的操作
#install httpd
- hosts: appsrvs
remote_user: root
tasks:
- name: install package
yum: name=httpd
- name: config file
copy: src=files/httpd.conf dest=/etc/httpd/conf/ backup=yes
notify: restart service
tags: config
- name: service
service: name=httpd state=started enabled=yes
tags: service
- name: restart service
service: name=httpd state=restarted
tags: service
handlers:
- name: restart service
service: name=httpd state=restarted
---
# file var
- hosts: app
remote_user: root
tasks:
- name: file
file: name=/data/{{item}}{{mark}}{{ansible_hostname}}.log state=touch
with_items:
- abc
- xyz
- 123
- hosts: appsrvs
remote_user: root
tasks:
- name: create group
group: name={{item}}
with_items:
- agroup
- bgroup
- cgroup
- name: create user
user: name={{item.name}} group={{item.group}}
with_items:
- {name: "aaa",group: "agroup"}
- {name: "bbb",group: "bgroup"}
- {name: "ccc",group: "cgroup"}
---
- hosts: all
- vars:
test_ports:
- 81
- 82
- 83
tasks:
- name: test
template: src=test.conf.j2 dest=/data/test.conf
{%for i in test_ports %}
server {
listen {{i}}
server_name www.a.com
root /app/websitea/
}
{%endfor%}
---
# file var
- hosts: appsrvs
remote_user: root
vars_files:
- vars.yml
vars:
- port: 1234
tasks:
- name: file
file: name=/data/{{ansible_hostname}}{{mark}}{{port}}.log state=touch
ansible--playbook用法与自己的yml范例总结
标签:总结 creat install roc psr 自定义 改变 元素 没有
原文地址:https://blog.51cto.com/9019400/2385520