做网站建设平台/推广注册app拿佣金平台
剧本语言
剧本使用的yaml语言
yaml文件的后缀为.yml或者.yaml
使用空格做为缩进
相同层级的元素左侧对齐即可
缩进时不允许使用 Tab 键,只允许使用空格
创建剧本
直接编辑不存在会自动创建这个文件,先用touch新建也行
vim juben.yml
编写剧本
hosts:指定的需要操作的主机
vars:指定的是变量(非必须)
tasks: 具体要执行的任务
name:任务名称
shell:shell模块
- hosts: alltasks:- name: 在所有主机上新建monster.ymlshell: touch /data/monster/monster.yml
剧本执行
执行前可以加"–check"或"-C"检查剧本格式是否有错误
[root@monster1 ~]# ansible-playbook juben.yml -CPLAY [all] ********************************************************************************************************************************************TASK [Gathering Facts] ********************************************************************************************************************************
ok: [192.168.71.137]TASK [在所有主机上新建monster.yml] ****************************************************************************************************************************
skipping: [192.168.71.137]PLAY RECAP ********************************************************************************************************************************************
192.168.71.137 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 [root@monster1 ~]#
没有问题再把-C去掉正式执行,中间有个告警直接无视掉,它建议新建文件使用file模块,我们剧本里面使用的shell模块
[root@monster1 ~]# ansible-playbook juben.ymlPLAY [all] ********************************************************************************************************************************************TASK [Gathering Facts] ********************************************************************************************************************************
ok: [192.168.71.137]TASK [在所有主机上新建monster.yml] ****************************************************************************************************************************[WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because file is insufficient you
can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.changed: [192.168.71.137]PLAY RECAP ********************************************************************************************************************************************
192.168.71.137 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@monster1 ~]#
趣味风格cowsay
执行过程中输出一大堆信息,你觉得比较乏味可以安装cowsay
安装cowsay后默认将你的过程输出信息显示奶牛图案
yum install -y cowsay
安装好后,再执行脚本看效果
[root@monster1 ~]# ansible-playbook juben.yml____________
< PLAY [all] >------------\ ^__^\ (oo)\_______(__)\ )\/\||----w ||| ||________________________
< TASK [Gathering Facts] >------------------------\ ^__^\ (oo)\_______(__)\ )\/\||----w ||| ||ok: [192.168.71.137]____________________________
< TASK [在所有主机上新建monster.yml] >----------------------------\ ^__^\ (oo)\_______(__)\ )\/\||----w ||| ||[WARNING]: Consider using the file module with state=touch rather than running 'touch'. If you need to use command because file is insufficient you
can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.changed: [192.168.71.137]____________
< PLAY RECAP >------------\ ^__^\ (oo)\_______(__)\ )\/\||----w ||| ||192.168.71.137 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@monster1 ~]#
更换图案
前面说了奶牛是默认图案,可以使用cowsay -l显示所有可以选择的图案。比如龙,执行cowsay -f 图案 内容可以看到预览效果。
[root@monster1 ~]# cowsay -l
Cow files in /usr/share/cowsay:
beavis.zen blowfish bong bud-frogs bunny cheese cower default dragon
dragon-and-cow elephant elephant-in-snake eyes flaming-sheep ghostbusters
head-in hellokitty kiss kitty koala kosh luke-koala mech-and-cow meow milk
moofasa moose mutilated ren satanic sheep skeleton small sodomized
stegosaurus stimpy supermilker surgery telebears three-eyes turkey turtle
tux udder vader vader-koala www
[root@monster1 ~]# cowsay -f dragon 火龙喷死你!!!__________
< 火龙喷死你!!! >----------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~
[root@monster1 ~]#
效果知道想要哪个了,怎么更改默认输出自己想要的表情图案呢
编辑ansible.cfg文件
vim /etc/ansible/ansible.cfg
在223行,复制一行出来修改成自己要修改的图案,不复制直接修改default也行
执行剧本查看效果
剧本任务案例
1.在所有主机/data/下创建monster目录
2.在monster下创建monster.log文件
3.docker安装redis
4.编写定时任务每分钟记录一下docker所有容器性能状态日志到monster.log
编写案例剧本
vim anli.yml
- hosts: alltasks:- name: 任务1、创建monster目录file: path=/data/monster state=directory- name: 任务2、创建monster.logfile: path=/data/monster/monster.log state=touch- name: 任务3、安装redisshell: |docker pull redisdocker run --restart=always --log-opt max-size=100m --log-opt max-file=2 -p 6379:6379 --name redis -d redis- name: 任务4、docker所有容器性能监控写入monster.logcron: name=dockerlog minute=*/1 job="docker stats --no-stream | awk '{if(NR > 1){print $0}}' >> /data/monster/monster.log" state=present
检查有无问题
[@monster1 ~]# ansible-playbook anli.yml -C____________
< PLAY [all] >------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~________________________
< TASK [Gathering Facts] >------------------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~ok: [192.168.71.137]________________________
< TASK [任务1、创建monster目录] >------------------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~changed: [192.168.71.137]__________________________
< TASK [任务2、创建monster.log] >--------------------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~ok: [192.168.71.137]____________________
< TASK [任务3、安装redis] >--------------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~skipping: [192.168.71.137]________________________________________
< TASK [任务4、docker所有容器性能监控写入monster.log] >----------------------------------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~skipping: [192.168.71.137]____________
< PLAY RECAP >------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~192.168.71.137 : ok=3 changed=1 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0
执行剧本
[root@monster1 ~]# ansible-playbook anli.yml ____________
< PLAY [all] >------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~________________________
< TASK [Gathering Facts] >------------------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~ok: [192.168.71.137]________________________
< TASK [任务1、创建monster目录] >------------------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~ok: [192.168.71.137]__________________________
< TASK [任务2、创建monster.log] >--------------------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~changed: [192.168.71.137]____________________
< TASK [任务3、安装redis] >--------------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~changed: [192.168.71.137]________________________________________
< TASK [任务4、docker所有容器性能监控写入monster.log] >----------------------------------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~changed: [192.168.71.137]____________
< PLAY RECAP >------------\ / \ //\\ |\___/| / \// \\/0 0 \__ / // | \ \ / / \/_/ // | \ \ @_^_@'/ \/_ // | \ \ //_^_/ \/_ // | \ \( //) | \/// | \ \( / /) _|_ / ) // | \ _\( // /) '/,_ _ _/ ( ; -. | _ _\.-~ .-~~~^-.(( / / )) ,-{ _ `-.|.-~-. .~ `.(( // / )) '/\ / ~-. _ .-~ .-~^-. \(( /// )) `. { } / \ \(( / )) .----~-.\ \-' .~ \ `. \^-.///.----..> \ _ -~ `. ^-` ^-_///-._ _ _ _ _ _ _}^ - - - - ~ ~-- ,.-~/.-~192.168.71.137 : ok=5 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@monster1 ~]#
到被分发的服务器验证,目录创建了,查看monster.log,里面也在记录,查看定时任务里面的内容正是我们剧本里面编辑好的