playbook简介
Playbook Language Example
实例一:apache.yml
1、mkdir /etc/ansible/playbooks
2、vim apache.yml
1 --- 2 - hosts: web1 3 vars: 4 http_port: 80 5 max_clients: 200 6 remote_user: root 7 tasks: 8 - name: ensure apache is at the latest version 9 yum: pkg=httpd state=latest 10 - name: write the apache config file 11 template: src=/srv/httpd.j2 dest=/etc/httpd.conf 12 notify: 13 - restart apache 14 - name: ensure apache is running 15 service: name=httpd state=started 16 handlers: 17 - name: restart apache 18 service: name=httpd state=restarted
3、mkdir /static_files
在这里您将存储自定义index.html文件
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="utf-8"/> 5 </script> 6 </head> 7 <body> 8 <h1>Apache was started in this host via Ansible</h1><br> 9 <h2>Brought to you by Howtoing.com</h2> 10 </body> 11 </html>
4、执行
ansible-playbook /etc/ansible/playbooks/apache.yml
语言格式说明:
YAML:一种数据序列化工具的语言格式数据结构:
key:value - item1 - item2 - item3 例如{name:jerry, age:21}PlayBook核心元素: Tasks:任务,由模块定义的操作的列表; Variables:变量 Templates:模板,即使用了模板语法的文本文件; Handlers:由特定条件触发的Tasks; Roles:角色; playbook的基础组件: Hosts:运行指定任务的目标主机; remote_user:在远程主机以哪个用户身份执行; sudo_user:非管理员需要拥有sudo权限; tasks:任务列表 模块,模块参数: 格式: (1) action: module arguments (2) module: arguments运行playbook,使用ansible-playbook命令 (1) 检测语法 ansible-playbook --syntax-check /path/to/playbook.yaml (2) 测试运行 ansible-playbook -C /path/to/playbook.yaml --list-hosts -list-tasks --list-tags (3) 运行 ansible-playbook /path/to/playbook.yaml -t TAGS, --tags=TAGS --skip-tags=SKIP_TAGS --start-at-task=START_AT示例1:定义一个playbook任务来新增用户和组定义一个yaml的模板1 [root@lvs_S ~]# cat group.yml 2 - hosts: all 3 remote_user: root 4 tasks: 5 - name: add a group 6 group: name=pbgroup system=true 7 - name: add a user 8 user: name=pbuser group=pgroup system=true
检查语法:[root@lvs_S ~]# ansible-playbook --syntax-check group.yml实例:
#将创建的脚本文件分发到远程
ansible webservers -m copy -a "src=/root/a.sh dest=/tmp/a.sh owner=root group=root mode=0755" # 远程执行
ansible webservers -m shell -a "/tmp/a.sh" #批量添加用户密码
ansible webservers -m shell -a 'echo 123 |passwd --stdin abc'cron模块:管理计划任务条目用法:
-a "" minute= hour= day= month= weekday= job= name= user= state={present|absent} 创建一个同步时间的计划任务,每5分钟同步一下服务器的时间 ansible all -m cron -a "minute='*/5' job='/usr/sbin/ntpdate windowstime &>/dev/null' name='sync time'"
ansible all -m cron -a "name='sync time' state=absent" //删除
hostname模块:管理主机名
ansible all -a 'hostname' 获取主机明
ansible 192.168.128.172 -m hostname -a "name=web_01"
yum模块
用法:
-a "" (1) name= state={present|latest} (2) name= state=absent
yum安装vsftpd
ansible all -m yum -a 'name=vsftpd'
ansible all -m yum -a 'name=vsftpd state=absent' 卸载
service模块:服务管理
开启主机的httpd服务
ansible all -m service -a "name=httpd state=started enabled=true"
user模块 用户管理
使用格式:
name= : 创建的用户名 state= : present新增,absent删除 force= : 删除用户的时候删除家目录 system= : 创建系统用户 uid= : 指定UID shell= : 指定shell home= : 指定用户家目录 添加一个系统用户
ansible all -m user -a "name=ccc system=true"ansible模块:
获取模块列表:ansible-doc -l
获取指定模块的使用帮助:ansible-doc -s MOD_NAME
nginx.yml:
1 --- 2 - hosts: web2 3 tasks: 4 - name: Copy Nginx.conf 5 template: src=/etc/yum.repos.d/nginx.repo dest=/etc/yum.repos.d/ ##复制nginx.repo源到远程主机 6 7 - name: Install Nginx Package 8 yum: name=nginx state=present ##yum安装nginx 9 10 - name: start nginx 11 service: name=nginx state=started ##启动nginx 12 13 - name: Copy Nginx.conf 14 template: src=/etc/ansible/playbooks/test1/nginx.conf.j2 dest=/etc/nginx/nginx.conf ##复制nginx.conf模板到远程主机 15 16 notify: 17 - restart nginx ##重启nginx 18 handlers: 19 - name: restart nginx 20 service: name=nginx state=restarted

浙公网安备 33010602011771号