做化妆品网站的意义,泰和县网站免费建站,温州房产信息网,婴儿网站模板华子目录 Ansible四个命令模块1.组成2.特点3.区别3.1command、shell模块3.2raw模块 4.command模块4.1参数表4.2free_form参数 5.shell模块5.1作用5.2例如 6.script模块6.1示例 7.raw模块7.1参数7.2示例 文件操作模块1.file模块1.1参数1.2示例 2.copy模块2.1参数 Ansible四个命令… 华子目录 Ansible四个命令模块1.组成2.特点3.区别3.1command、shell模块3.2raw模块 4.command模块4.1参数表4.2free_form参数 5.shell模块5.1作用5.2例如 6.script模块6.1示例 7.raw模块7.1参数7.2示例  文件操作模块1.file模块1.1参数1.2示例 2.copy模块2.1参数   Ansible四个命令模块 
1.组成 
command、shell、raw 
2.特点 
应尽量避免使用这三个模块来执行命令因为其他模块大部分都是幂等性的可以自动进行更改跟踪。幂等性输入相同输出相同无论多少次执行。比如说确认接口如果传入订单号返回确认ok如果已经确认过了再次调用确认接口返回如果还是确认ok那么这个接口就是满足幂等性command、shell、raw不具备幂等性 
3.区别 
3.1command、shell模块 
相同点要求受管主机上安装python不同点command可以在受管主机上执行Linux命令但是不支持环境变量和操作符例如|   shell模块需要调用/bin/sh指令执行 
3.2raw模块 
不需要受管主机安装python直接使用远程shell运行命令通常用于无法安装python的系统例如网络设备等 
4.command模块 
4.1参数表 
名称必选备注chdirno不是必选参数运行command命令前先cd到这个目录createsno如果这个参数对应的文件存在就不运行commandfree_formyes需要执行的脚本没有真正的参数为free_formexecutableno改变用来执行命令的shell是可执行文件的绝对路径removesno如果这个参数对应的文件不存在就不运行command与creates参数作用相反stdinno2.4后的新增将命令的stdin设置为指定的值 
4.2free_form参数 
必须参数指定需要远程执行的命令。free_form 参数与其他参数如果想要使用一个参数那么则需要为这个参数赋值也就是namevalue模式并不相同。如需要在远程主机上执行 ls 命令时错误写法free_formls 因为并没有任何参数的名字是 free_form若要在远程主机中执行 ls 命令时直接写成 ls 即可。因为 command 模块的作用是执行命令所以任何一个可以在远程主机上执行的命令都可以被称为free_form例 
[rootserver ~]# ansible-inventory --graph  #分组查看
all:|--ungrouped:|  |--node1.example.com|  |--node2.example.com
[rootserver ~]# ansible all -m command -a ls /root   #查看目录
node2.example.com | CHANGED | rc0 
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
node1.example.com | CHANGED | rc0 
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
frp_0.56.0_linux_amd64
frp_0.56.0_linux_amd64.tar.gz
mysql-8.0.37-linux-glibc2.17-x86_64.tar.xz[rootserver ~]# ansible all -m command -a cd /
node2.example.com | CHANGED | rc0 node1.example.com | CHANGED | rc0  
[rootserver ~]# ansible all -m command -a pwd
node2.example.com | CHANGED | rc0 
/root
node1.example.com | CHANGED | rc0 
/root[rootserver ~]# ansible all -m command -a touch file.ansible
node1.example.com | CHANGED | rc0 node2.example.com | CHANGED | rc0 [rootnode1 ~]# ls
公共  文档   模板  下载  anaconda-ks.cfg   file.ansible[rootnode2 ~]# ls
公共  模板  视频    anaconda-ks.cfg  file.ansible[rootserver ~]# ansible all -m command -a ls /root createsfile.ansible   #当文件file.ansible存在则就不执行前面的命令
node1.example.com | SUCCESS | rc0 
skipped, since file.ansible existsDid not run command since file.ansible exists
node2.example.com | SUCCESS | rc0 
skipped, since file.ansible existsDid not run command since file.ansible exists[rootserver ~]# ansible all -m command -a ls /root removesfile.ansible  #当文件file.ansible不存在则就不执行前面的命令
node2.example.com | CHANGED | rc0 
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
file.ansible
node1.example.com | CHANGED | rc0 
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
file.ansible
frp_0.56.0_linux_amd64
frp_0.56.0_linux_amd64.tar.gz
mysql-8.0.37-linux-glibc2.17-x86_64.tar.xz#无法使用管道符和输入输出重定向
[rootserver ~]# ansible all -m command -a echo hello world  file.ansible
node1.example.com | CHANGED | rc0 
hello world  file.ansible
node2.example.com | CHANGED | rc0 
hello world  file.ansible
[rootserver ~]# ansible all -m command -a cat file.ansible
node2.example.com | CHANGED | rc0 node1.example.com | CHANGED | rc0 [rootserver ~]# ansible all -m command -a ls /root | grep file.ansible
node2.example.com | FAILED | rc2 
file.ansible/root:
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
file.ansiblels: 无法访问 |: 没有那个文件或目录
ls: 无法访问 grep: 没有那个文件或目录non-zero return code
node1.example.com | FAILED | rc2 
file.ansible/root:
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
file.ansible
frp_0.56.0_linux_amd64
frp_0.56.0_linux_amd64.tar.gz
mysql-8.0.37-linux-glibc2.17-x86_64.tar.xzls: 无法访问 |: 没有那个文件或目录
ls: 无法访问 grep: 没有那个文件或目录non-zero return code5.shell模块 
5.1作用 让远程主机在shell进程下执行命令从而支持shell的特性如管道等参数与command模块几乎相同但在执行命令的时候调用的是/bin/sh  
5.2例如 
[rootserver ~]# ansible all -m shell -a tree chdir/root
node2.example.com | CHANGED | rc0 
.
├── 公共
├── 模板
├── 视频
├── 图片
├── 文档
├── 下载
├── 音乐
├── 桌面
├── anaconda-ks.cfg
└── file.ansible8 directories, 2 files
node1.example.com | CHANGED | rc0 
.
├── 公共
├── 模板
├── 视频
├── 图片
├── 文档
├── 下载
├── 音乐
├── 桌面
├── anaconda-ks.cfg
├── file.ansible
├── frp_0.56.0_linux_amd64
│   ├── frpc
│   ├── frpc.toml
│   └── LICENSE
├── frp_0.56.0_linux_amd64.tar.gz
└── mysql-8.0.37-linux-glibc2.17-x86_64.tar.xz9 directories, 7 files[rootserver ~]# ansible node1.example.com -m shell -a echo hello world  file.ansible
node1.example.com | CHANGED | rc0 [rootserver ~]# ansible node1.example.com -m shell -a cat /root/file.ansible
node1.example.com | CHANGED | rc0 
hello world6.script模块 
script 与shell 类似都可以执行脚本区别script执行的脚本在ansible管理机上而shell执行的脚本必须先放到目标节点上去才能执行shell执行可以使用环境变量bash等但是script只是执行.sh脚本不能带 bash 
6.1示例 
在server端写一个t2.sh的脚本 
[rootserver ~]# vim t2.sh
#!/bin/bash
echo hello world[rootserver ~]# ansible all -m script -a t2.sh
node2.example.com | CHANGED  {changed: true,rc: 0,stderr: Shared connection to node2.example.com closed.\r\n,stderr_lines: [Shared connection to node2.example.com closed.],stdout: hello world\r\n,stdout_lines: [hello world]
}
node1.example.com | CHANGED  {changed: true,rc: 0,stderr: Shared connection to node1.example.com closed.\r\n,stderr_lines: [Shared connection to node1.example.com closed.],stdout: hello world\r\n,stdout_lines: [hello world]
}在目标节点上 
[rootnode1 ~]# vim t2.sh
#!/bin/bash
echo hello world[rootnode2 ~]# vim t2.sh
#!/bin/bash
echo hello world[rootserver ~]# ansible all -m shell -a bash t2.sh
node2.example.com | CHANGED | rc0 
hello world
node1.example.com | CHANGED | rc0 
hello world7.raw模块 
raw模块主要用于执行一些低级的命令一般适用于下列两种场景 
第一种在较老的python2.4和之前的版本主机上执行命令第二种对任何没有安装python的设备如路由器注意在任何其他情况下使用shell或command模块更为合适 
7.1参数 
名称必选备注executableno可以不选改变用来执行命令的shell是可执行文件的绝对路径free_formyes需要执行的脚本没有真正的参数为free_form 
7.2示例 
[rootserver ~]# ansible all -m raw -a pwd
node1.example.com | CHANGED | rc0 
/root
Shared connection to node1.example.com closed.node2.example.com | CHANGED | rc0 
/root
Shared connection to node2.example.com closed.[rootserver ~]# ansible all -m raw -a echo hello world
node1.example.com | CHANGED | rc0 
hello world
Shared connection to node1.example.com closed.node2.example.com | CHANGED | rc0 
hello world
Shared connection to node2.example.com closed.文件操作模块 
1.file模块 
作用实现对文件的基本操作如创建目录或文件删除目录或文件修改文件权限等 
1.1参数 
path必须参数用于指定要操作的文件或目录在之前版本的ansible中使用dest参数或者name参数指定要操作的文件或目录为了兼容之前的版本使用dest或name也可以state: 格式path“路径” state touch | directory | link | hard | absent此参数使用灵活如在远程主机中创建一个目录则使用path参数指定对应的目录路径假设在远程主机上创建/testdir/a/b目录则设置路径path/testdir/a/b但ansible无法从/testdir/a/b这个路径看出b是一个文件还是一个目录所以需要通过state参数进行说明  
参数值含义stateabsent删除远程机器上的指定文件或目录statedirectory创建一个空目录statefile查看指定目录是否存在statetouch创建一个空文件statehard/link创建链接文件 src当state设置为link或者hard时表示创建一个软链或硬链则必须通过指明src参数即可指定链接源  force : 当statelink的时使用forceyes 参数表示强制创建链接文件该文件分为三种情况: 当要创建的链接文件指向的源文件并不存在时使用此参数可以先强制创建出链接文件当存储目录中已经存在与链接文件同名的文件时会将同名文件覆盖为链接文件相当于删除同名文件创建链接文件当你要创建链接文件的目录中已经存在与链接文件同名的文件并且链接文件指向的源文件也不存在这时会强制替换同名文件为链接文件  owner用于指定被操作文件的属主信息属主对应的用户必须在远程主机中存在否则会报错  group用于指定被操作文件的属组属组对应的组必须在远程主机中存在否则会报错  mode用于指定被操作文件的权限如: 要将文件权限设置为: “rw-r-x---”则可以使用mode650进行设置或者使用mode0650要设置特殊权限如为二进制文件设置suid则可以使用mode4700  recurse当要操作的文件为目录时recurse设置为yes可以递归的修改目录中文件的属性和权限  
1.2示例 
在所有远程主机上创建一个名为 data的目录如果存在则不做操作 
[rootserver ~]# ansible all -m file -a path/root/data statedirectory
node1.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,gid: 0,group: root,mode: 0755,owner: root,path: /root/data,size: 6,state: directory,uid: 0
}
node2.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,gid: 0,group: root,mode: 0755,owner: root,path: /root/data,size: 6,state: directory,uid: 0
}[rootserver ~]# ansible all -m command -a ls chdir/root
node1.example.com | CHANGED | rc0 
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
data
file.ansible
frp_0.56.0_linux_amd64
frp_0.56.0_linux_amd64.tar.gz
mysql-8.0.37-linux-glibc2.17-x86_64.tar.xz
t2.sh
node2.example.com | CHANGED | rc0 
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
data
file.ansible
t2.sh在node1主机上创建一个名为testfile1的文件如果testfile1文件已经存在则会更新文件的时间戳与touch命令的作用相同 
[rootserver ~]# ansible node1.example.com -m file -a path/root/data/testfile1 statetouch
node1.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,dest: /root/data/testfile1,gid: 0,group: root,mode: 0644,owner: root,size: 0,state: file,uid: 0
}[rootserver ~]# ansible node1.example.com -m command -a ls chdir/root/data
node1.example.com | CHANGED | rc0 
file1
testfile1在node1上为testfile1文件创建软链接文件软链接名为linkfile1 
[rootserver ~]# ansible node1.example.com -m file -a path/root/data/linkfile1 statelink src/root/data/testfile1
node1.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,dest: /root/data/linkfile1,gid: 0,group: root,mode: 0777,owner: root,size: 20,src: /root/data/testfile1,state: link,uid: 0
}[rootserver ~]# ansible node1.example.com -m command -a ls chdir/root/data
node1.example.com | CHANGED | rc0 
file1
linkfile1
testfile1在node1上为 testfile1 文件创建硬链接文件硬链接名为 hardfile1类似于复制 
[rootserver ~]# ansible node1.example.com -m file -a path/root/data/hardfile1 statehard src/root/data/testfile1
node1.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,dest: /root/data/hardfile1,gid: 0,group: root,mode: 0644,owner: root,size: 0,src: /root/data/testfile1,state: hard,uid: 0
}[rootserver ~]# ansible node1.example.com -m command -a ls /root/data
node1.example.com | CHANGED | rc0 
file1
hardfile1
linkfile1
testfile1在创建链接文件时如果源文件不存在或者链接文件与其他文件同名时强制覆盖同名文件或者创建链接文件参考上述force参数的解释 
[rootserver ~]# ansible node1.example.com -m file -a path/root/data/linkfile3 statelink src/root/data/123 forceyes   #注意123文件不存在
[WARNING]: Cannot set fs attributes on a non-existent symlink target. follow should be
set to False to avoid this.
node1.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,dest: /root/data/linkfile3,src: /root/data/123
}[rootserver ~]# ansible node1.example.com -m command -a ls chdir/root/data
node1.example.com | CHANGED | rc0 
file1
hardfile1
linkfile1
linkfile3
testfile1删除node1上的/root/data目录 
[rootserver ~]# ansible node1.example.com -m file -a path/root/data stateabsent
node1.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,path: /root/data,state: absent
}[rootserver ~]# ansible node1.example.com -m command -a ls chdir/root
node1.example.com | CHANGED | rc0 
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
file.ansible
frp_0.56.0_linux_amd64
frp_0.56.0_linux_amd64.tar.gz
mysql-8.0.37-linux-glibc2.17-x86_64.tar.xz
t2.sh创建文件或目录的时候指定属主或者修改远程主机上的文件或目录的属主 
[rootserver ~]# ansible all -m file -a path/root/testfile1 statetouch ownerredhat  #新建文件并指定为属主为redhat组默认为root
node2.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,dest: /root/testfile1,gid: 0,group: root,mode: 0644,owner: redhat,size: 0,state: file,uid: 1000
}
node1.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,dest: /root/testfile1,gid: 0,group: root,mode: 0644,owner: redhat,size: 0,state: file,uid: 1000
}[rootserver ~]# ansible all -m file -a path/root/testfile2 statetouch   #新建文件默认为root
node2.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,dest: /root/testfile2,gid: 0,group: root,mode: 0644,owner: root,size: 0,state: file,uid: 0
}
node1.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,dest: /root/testfile2,gid: 0,group: root,mode: 0644,owner: root,size: 0,state: file,uid: 0
}#修改属主和组
[rootserver ~]# ansible all -m file -a path/root/testfile2 statetouch ownerredhat groupredhat
node1.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,dest: /root/testfile2,gid: 1000,group: redhat,mode: 0644,owner: redhat,size: 0,state: file,uid: 1000
}
node2.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,dest: /root/testfile2,gid: 1000,group: redhat,mode: 0644,owner: redhat,size: 0,state: file,uid: 1000
}创建文件或目录的时候指定权限或者修改远程主机上的文件或目录的权限 
[rootserver ~]# ansible all -m file -a path/root/testfile1 statetouch mode777
node1.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,dest: /root/testfile1,gid: 0,group: root,mode: 0777,owner: redhat,size: 0,state: file,uid: 1000
}
node2.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,dest: /root/testfile1,gid: 0,group: root,mode: 0777,owner: redhat,size: 0,state: file,uid: 1000
}递归方式将目录中的文件的属主属组都设置为redhat 
[rootserver ~]# ansible all -m file -a path/data/test/demo statedirectory ownerredhat groupredhat recurseyes
node2.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,gid: 1000,group: redhat,mode: 0755,owner: redhat,path: /data/test/demo,size: 6,state: directory,uid: 1000
}
node1.example.com | CHANGED  {ansible_facts: {discovered_interpreter_python: /usr/bin/python3},changed: true,gid: 1000,group: redhat,mode: 0755,owner: redhat,path: /data/test/demo,size: 6,state: directory,uid: 1000
}[rootnode1 ~]# ll /data/test
总用量 0
drwxr-xr-x 2 redhat redhat 6  7月  7 22:15 demo[rootnode2 ~]# ll /data/test/
总用量 0
drwxr-xr-x 2 redhat redhat 6  7月  7 22:15 demo2.copy模块 
作用拷贝文件将ansible主机上的文件拷贝到远程受控主机中 
2.1参数 
参数默认值含义src用于指定需要copy的文件或目录backupno,yes当远程主机的目标路径中已存在同名文件并且与ansible主机中的文件内容不同时是否对远程主机的文件进行备份设为yes时会先备份远程主机中的文件然后再拷贝到远程主机content当不使用src指定拷贝的文件时可以使用content直接指定文件内容src与content两个参数必有其一否则会报错dest用于指定文件将被拷贝到远程主机的哪个目录中dest为必须参数group指定文件拷贝到远程主机后的属组但是远程主机上必须有对应的组否则会报错owner指定文件拷贝到远程主机后的属主但是远程主机上必须有对应的用户否则会报mode错文指定文件拷贝到远程主机后的权限如果你想将权限设置为rw-r–r–则可以使用mode0644表示如果你想要在user对应的权限位上添加执行权限则可以使用modeux表示forceno,yes当远程主机的目标路径中已经存在同名文件并且与ansible主机中的文件内容不同时是否强制覆盖默认值为yes表示覆盖如果设置为no则不会执行覆盖拷贝操作远程主机中的文件保持不变