#################################
剧本
#################################
表示后面的内容是一行,|表示后面的内容是多行
shell: touch ~/shell。txt 调用什么模块,执行什么命令
一个 -name: 为一个任务,任务名字随便写,下面跟着接调用什么模块,执行什么任务。
vim ~/.vimrc
autocmd FileType yaml setlocal ai ts=2 sw=2 et
#相当于在yml结尾的文件中按一下tab等于两下空格,yml文件中不允许存在tab出来的空格
剧本的样式,格式参考课程PPT
[root@control ansible]# vim test.yml
---
- hosts: all #定义需要远程管理的主机是谁
tasks: #定义需要执行的任务是什么
- name: ping all #给第一个任务取个名称
ping: #第一个任务调用ping模块
[root@control ansible]# ansible-playbook test.yml
hosts由一个或多个组或主机组成,逗号分隔,tasks由一个或多个任务组成,多个任务按顺序执行,可以使用-f选项自定义并发量。
[root@control ansible]# vim ~/ansible/test.yml
- hosts: test,webserver #远程谁
tasks:
- name: This is my first playbook #定义名字
ping: #模块
- name: Run a shell command #第二个任务
shell: touch ~/shell.txt #模块
[root@control ansible]# ansible-playbook ~/ansible/test.yml -f 5
#############################
多个play的Playbook文件
#############################
[root@control ansible]# vim ~/ansible/test.yml
#第一个play
---
- hosts: test
tasks:
- name: This is first play
ping:
#第二个play
- hosts: webserver
tasks:
- name: This is second play
ping:
为了更好的让vim自动使用两个空格缩进可以做如下操作:
修改VIM配置文件,自动使用空格替换tab键。
[root@control ansible]# vim ~/.vimrc
autocmd FileType yaml setlocal ai ts=2 sw=2 et
#使用2个空格自动替换tab键,tabstop=2、expandtab
#开启自动缩进对齐,缩进宽度为2个空格,shiftwidth=2 根据个人习惯可以不设置
[root@control ansible]# vim ~/ansible/test_john.yml
---
- hosts: webserver
tasks:
- name: Add the user 'johnd'
user:
name: johnd
uid: 1040
group: daemon
password: "{
{ '123' | password_hash('sha512') }}"
#hosts定义需要远程的对象是webserver组
#tasks是需要执行的任务
-name是第一个任务的描述信息
user是需要调用的模块,user下面的缩进内容是给user的参数
name是需要创建的用户名,uid是用户ID号
group是用户属于哪个基本组
password是用户的密码,密码是123,密码经过sha512算法加密
用户管理,创建系统账户、账户属性、设置密码。
[root@control ansible]# cat ~/ansible/test_john.yml
---
hosts: webserver
tasks:
- name: Add the user