Linux ansible常用模块的使用

Linux ansible常用模块的使用

ansible的基本用法如下

ansible 机器名 ‐m 模块× ‐a "模块的参数”

对被管理机器执行不同的操作,只需要调用不同的模块就可以了。ansible中内置了很多的模块,可以通过ansible-doc -l查看系统中所有的模块。

[blab@rh1 ~]$ ansible-doc -l
a10_server                                                    Manage A10 Networks AX/SoftAX/Thunde...
a10_server_axapi3                                             Manage A10 Networks AX/SoftAX/Thunde...
a10_service_group                                             Manage A10 Networks AX/SoftAX/Thunde...
a10_virtual_server                                            Manage A10 Networks AX/SoftAX/Thunde...
aci_aaa_user                                                  Manage AAA users (aaa:User)         
aci_aaa_user_certificate                                      Manage AAA user certificates (aaa:Us...
aci_access_port_block_to_access_port                          Manage port blocks of Fabric interfa...
aci_access_port_to_interface_policy_leaf_profile              Manage Fabric interface policy leaf ...

按【Enter】键会一行一行地往下显示,按空格键会一页一页地往下显示,按【q】键退出。

不同的模块有不同的参数,如果要查看某个模块的参数,可以通过如下语法来查看。

ansibe-doc 模块名

ansible中有很多模块,每个模块也有很多参数,我们是不可能把所有的模块、每个模块的所有参数都掌握的。所以,下面我们只讲解最常见的模块及这些模块中最常见的参数的使用方法。

1.shell模块

shell模块可以在远端执行操作系统命令,具体用法如下。

ansible 主机组 ‐m shell ‐a "系统命令"

在rh2上执行hostname命令,命令如下。

[blab@rh1 ~]$ ansible rh2 -m shell -a "hostname"
rh2 | CHANGED | rc=0 >>
rh2

这里rc=0的意思是执行此命令之后的返回值为0,rc的意思是returm code(返回值),为0说明正确执行了,非零说明没有正确执行。

在 server2上执行一个错误的命令,命令如下。

[blab@rh1 ~]$ ansible rh2 -m shell -a "hostnamexxxx"
rh2 | FAILED | rc=127 >>
/bin/sh: hostnamexxxx: 未找到命令non-zero return code

这里rc=127的意思是执行此命令之后的返回值为127,非零说明没有正确执行。

2.文件管理的file模块

file模块用于创建和删除文件/目录,修改文件/目录属性,其常见的参数包括以下几个。

1)path:用于指定文件/目录的路径,此选项可以用name或dest替代。
(2)state:指定行为。
(3)touch:创建文件。
(4)directory:创建目录。
(5)file:对已存文件进行修改。
(6)absent:删除。
(7)link:软链接。
(8)hard:硬链接。
(9)其他参数:owner指定所有者,group指定所属组,mode指定权限,setype指定上下文。

在server2上创建一个文件/opt/hosts,并设置所有者为root,所属组为tom,权限为444,命令如下。

[blab@rh1 ~]$ ansible rh2 -m file -a "path=/opt/hosts owner=root group=tom mode=444 state=touch"
rh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/opt/hosts",
    "gid": 1001,
    "group": "tom",
    "mode": "0444",
    "owner": "root",
    "secontext": "unconfined_u:object_r:usr_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0
}

需要注意的是,此处用path指定的文件,替换成name也是可以的,即
name=/opt/hosts。查看文件的属性,命令如下。

[blab@rh1 ~]$ ansible rh2 -m shell -a "ls -l /opt/hosts"
rh2 | CHANGED | rc=0 >>
-r--r--r--. 1 root tom 0 12月 18 11:40 /opt/hosts

为/opt/hosts创建一个软链接/opt/hosts123,命令如下。

[blab@rh1 ~]$ ansible rh2 -m file -a "src=/opt/hosts dest=/opt/hosts123 state=link"
rh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/opt/hosts123",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:usr_t:s0",
    "size": 10,
    "src": "/opt/hosts",
    "state": "link",
    "uid": 0
}

验证,命令如下

[blab@rh1 ~]$ ansible rh2 -m shell -a "ls -l /opt"
rh2 | CHANGED | rc=0 >>
总用量 0
-r--r--r--. 1 root tom   0 12月 18 11:40 hosts
lrwxrwxrwx. 1 root root 10 12月 18 11:51 hosts123 -> /opt/hosts

删除/opt/hosts123,命令如下。

[blab@rh1 ~]$ ansible rh2 -m file -a 'name=/opt/hosts123 state=absent'
rh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "path": "/opt/hosts123",
    "state": "absent"
}

创建目录/opt/xx,上下文设置为default_t,命令如下。

[blab@rh1 ~]$ ansible rh2 -m file -a 'name=/opt/xx state=directory setype=default_t'
rh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 0,
    "group": "root",
    "mode": "0755",
    "owner": "root",
    "path": "/opt/xx",
    "secontext": "unconfined_u:object_r:default_t:s0",
    "size": 6,
    "state": "directory",
    "uid": 0
}

把/opt/hosts的权限改成000,所有者改成zhangsan,所属组改成users,命令如下。

[blab@rh1 ~]$ ansible rh2 -m file -a "name=/opt/hosts owner=zhangsan group=users mode=000"
rh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 100,
    "group": "users",
    "mode": "0000",
    "owner": "zhangsan",
    "path": "/opt/hosts",
    "secontext": "unconfined_u:object_r:usr_t:s0",
    "size": 0,
    "state": "file",
    "uid": 1001
}

验证,命令如下。

[blab@rh1 ~]$ ansible rh2 -m shell -a "ls -l /opt/hosts"
rh2 | CHANGED | rc=0 >>
----------. 1 zhangsan users 0 12月 18 11:40 /opt/hosts

清空server2 上/opt中所有的内容,命令如下。

[blab@rh1 ~]$ ansible rh2 -m shell -a "rm -rf /opt/*"
[WARNING]: Consider using the file module with state=absent rather than running 'rm'.  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.
rh2 | CHANGED | rc=0 >>

面的WARNING可以忽略不管,如果不想显示此消息,则在ansible.cfg的[defaults]字段下添加 command warnings=False即可。

3.copy和fetch模块

copy用于把本地的文件拷贝到被管理机器,语法如下。

ansible 主机组 ‐m copy ‐a "src=/path1/file1 dest=path2/"

作用是把本地的/path1/file1拷贝到目的主机的/path2中。

copy模块常见的参数包括以下几个。
(1) src:源文件。
(2)dest:目的地,即拷贝到哪里。
(3)owner:所有者。
(4)group:所属组。(5)mode:权限。

把本地的文件/etc/ansible/hosts拷贝到目标机器的/opt目录中,并设置权限为000,所有者为zhangsan,命令如下。

[blab@rh1 ~]$ ansible rh2 -m copy -a "src=/etc/ansible/hosts mode=000 owner=zhangsan dest=/opt"
rh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "da996f1a52dbae3b6b43a6c50d761e4ed5ec9a9f",
    "dest": "/opt/hosts",
    "gid": 0,
    "group": "root",
    "mode": "0000",
    "owner": "zhangsan",
    "path": "/opt/hosts",
    "secontext": "system_u:object_r:usr_t:s0",
    "size": 1016,
    "state": "file",
    "uid": 1001
}

验证,命令如下

[blab@rh1 ~]$ ansible rh2 -m shell -a "ls -l /opt/hosts"
rh2 | CHANGED | rc=0 >>
----------. 1 zhangsan root 1016 12月 18 12:14 /opt/hosts

copy模块也可以利用content参数往某个文件中写内容,如果此文件不存在则会创建出来。

在被管理机器的/opt目录中创建11.txt,内容为123123,命令如下。

[blab@rh1 ~]$ ansible rh2 -m copy -a 'content="123123" dest=/opt/11.txt'
rh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "601f1889667efaebb33b8c12572835da3f027f78",
    "dest": "/opt/11.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "4297f44b13955235245b2497399d7a93",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:usr_t:s0",
    "size": 6,
    "src": "/home/blab/.ansible/tmp/ansible-tmp-1702873012.9409525-3650-49907528301059/source",
    "state": "file",
    "uid": 0
}

验证/opt/11.txt的内容,命令如下。

[blab@rh1 ~]$ ansible rh2 -m shell -a "cat /opt/11.txt"
rh2 | CHANGED | rc=0 >>
123123

fetch用于把文件从被管理机器拷贝到本机当前目录中,命令如下。

[blab@rh1 ~]$ ansible rh2 -m fetch -a "src=/opt/hosts dest=."
rh2 | CHANGED => {
    "changed": true,
    "checksum": "da996f1a52dbae3b6b43a6c50d761e4ed5ec9a9f",
    "dest": "/home/blab/rh2/opt/hosts",
    "md5sum": "1564b951dc7c8511c6f9ee842653c541",
    "remote_checksum": "da996f1a52dbae3b6b43a6c50d761e4ed5ec9a9f",
    "remote_md5sum": null
}

查看,命令如下。

[blab@rh1 ~]$ tree rh2 
rh2
└── opt
    └── hosts

1 directory, 1 file

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值