咱们完成一个任务,例如安装部署一个httpd服务,咱们须要多个模块(一个模块也能够称之为task)提供功能来完成。而playbook就是组织多个task的容器,他的实质就是一个文件,有着特定的组织格式,它采用的语法格式是YAML(Yet Another Markup Language)。YAML语法可以简单的表示散列表,字典等数据结构。具体请参考YAML详细语法html
参考:https://blog.51cto.com/13589448/2068546web
ansible-playbook [options] playbook.yml [playbook2 ...]
-C, --check # 检查可是不作真正改变
-f FORKS, --forks=FORKS # 指定并发
--list-hosts # 列出符合条件的主机
--syntax-check # 语法校验
-e EXTRA_VARS, --extra-vars=EXTRA_VARS # 传值
应用redis
- hosts: webservers # 用户组
remote_user: root # 远程用户
tasks:
- name: install httpd # 任务名称 yum: name=httpd state=present # 模块 安装 - name: install configure file copy: src=httpd.conf dest=/etc/httpd/conf/ - name: start httpd service service: name=httpd state=started
hosts: web tasks: - name:偷看姑娘 dong: 偷看姑娘 when: 站着 - name: 偷看姑娘 dong: 偷看姑娘 when: 趴着
- hosts: web tasks: - name: content copy: content="xxxxxxxx" dest=/tmp/x.txt when: data=="3" - name: content copy: content="yyyyyyyy" dest=/tmp/x.txt when: data=="4" ansible-playbook -e data=3 p6.yml
使用playbook同时建立ann1,ann2, ann3用户数据结构
- hosts: web tasks: - name: createuser user: name={{item}} with_items: - ann1 - ann2 - ann3
- hosts: web tasks: - name: createuser user: name={{item}} with_items: - ann32 - ann33 - ann34 - name: creategroup group: name={{item}} with_items: - ben23 - ben24 - ben25
需求并发
建立用户组,ben33,ben34 , ben35spa
建立用户,ann53 ,附加组是ben33 , ann54 附加组是ben34, an55 附加组是ben35rest
- hosts: web tasks: - name: creategroup group: name={{item}} with_items: - ben33 - ben34 - ben35 - name: createuser user: name={{item.user}} groups={{item.group}} with_items: - {"user":ann53,"group":ben33} - {"user":ann54,"group":ben34} - {"user":ann55,"group":ben35}
- hosts: web tasks: - name: installredis yum: name=redis - name: copyfile template: dest=/etc/redis.conf src=redis.conf tags: copyfile - name: startredis service: name=redis state=restarted ansible-playbook -t copyfile p12.yml 建立一个tempaltes目录用来放文件
- hosts: web tasks: - name: installredis yum: name=redis - name: copyfile copy: dest=/etc/redis.conf src=/root/playbook/redis.conf tags: copyfile - name: startredis service: name=redis state=restarted ansible-playbook -t copyfile p12.yml
- hosts: web tasks: - name: installredis yum: name=redis - name: copyfile template: dest=/etc/redis.conf src=redis.conf tags: copyfile notify: restartredis - name: startredis service: name=redis state=started handlers: - name: restartredis service: name=redis state=restarted