Ansible的常用模块
ansible默认提供了很多的模块供我们使用。在Linux中,我们可以通过 ansible-doc -l 命令查看当前ansible支持的所有模块,通过 ansible-doc -s 模块名 来查看帮助文档中有哪些参数可以使用,通过 ansible 受管主机 -m 模块名 -a ‘参数’ 来执行命令。
ansible常用模块 shell command **raw **的区别:
- shell模块调用的/bin/sh指令执行
- command模块不是调用的shell的指令,所以没有bash的环境变量
- raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块
ping模块
ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@localhost ansible]# ansible 192.168.100.42 -m ping
192.168.100.42 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"ping": "pong"
}
[root@localhost ansible]#
shell模块
shell模块用于在受管机上执行受管机上的脚本,也可直接在受管机上执行命令。
shell模块支持管道与重定向
[root@localhost ansible]# ansible all -m shell -a 'ls -l /root'
192.168.100.42 | CHANGED | rc=0 >>
total 1
-rwxr-xr-x. 1 root root 23 Jul 18 01:07 ll.sh
[root@localhost ansible]# ansible all -m shell -a 'cat /root/ll.sh'
192.168.100.42 | CHANGED | rc=0 >>
#!/bin/bash
touch test
[root@localhost ansible]# ansible all -m shell -a '/root/ll.sh'
192.168.100.42| CHANGED | rc=0 >>
[root@localhost ansible]# ansible all -m shell -a 'ls /root'
192.168.100.42 | CHANGED | rc=0 >>
ll.sh
test
[root@localhost ansible]#
command模块
command模块用于在远程主机上执行命令,ansible默认就是使用command模块。
command模块有一个缺陷就是不能使用管道符和重定向功能
[root@localhost ansible]# ansible all -m command -a 'ls /tmp'
192.168.100.42 | CHANGED | rc=0 >>
ansible_command_payload_5poztdyc
hsperfdata_root
ks-script-a85snvsm
....
[root@localhost ansible]# ansible all -m command -a 'touch /tmp/abc'
[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.
192.168.220.8 | CHANGED | rc=0 >>
[root@localhost ansible]# ansible all -m command -a 'ls /tmp'
#查看受管主机上/tmp目录下是否创建了abc文件
192.168.100.42 | CHANGED | rc=0 >>
ansible_command_payload_9563eekh
hsperfdata_root
ks-script-a85snvsm
abc
[root@localhost ansible]# ansible all -m command -a 'echo "hello" > /tmp/tabc'
192.168.100.42 | CHANGED | rc=0 >>
hello > /tmp/test
[root@localhost ansible]# ansible all -m command -a 'cat /tmp/abc'
192.168.100.42 | CHANGED | rc=0 >>
[root@localhost ansible]# ansible all -m command -a 'ps -aux | grep ssh'
192.168.100.42 | FAILED | rc=1 >>
error: user name does not exist
Usage:
ps [options]
Try 'ps --help <simple|list|output|threads|misc|all>'
or 'ps --help <s|l|o|t|m|a>'
for additional help text.
For more details see ps(1).non-zero return code
raw模块
raw模块用于在远程主机上执行命令,支持管道符与重定向
执行原始的命令,而不是通过模块子系统。在任何情况下,使用shell或命令模块是合适的。给定原始的参数直接通过配置的远程shell运行。可返回标准输出、错误输出和返回代码。此模块没有变更处理程序支持。这个模块不需要远程系统上的Python,就像脚本模块一样。此模块也支持Windows目标。
[root@localhost ansible]# ansible all -m raw -a 'echo "bbb" > /tmp/abc'
192.168.220.8 | CHANGED | rc=0 >>
Shared connection to 192.168.100.42 closed.
[root@localhost ansible]# ansible all -m raw -a 'cat /tmp/abc'
192.168.100.42 | CHANGED | rc=0 >>
bbb
Shared connection to 192.168.100.42 closed.
[root@localhost ansible]# ansible all -m raw -a 'ps -aux|grep ssh'
192.168.100.42 | CHANGED | rc=0 >>
root 940 0.0 0.4 92296 7808 ? Ss 04:44 0:00 /usr/sbin/sshd -D -oCiphers=aes256-gcm@openssh.com,chacha20-poly1305@openssh.com,aes256-ctr,aes256-cbc,aes128-gcm@openssh.com,aes128-ctr,aes128-cbc -oMACs=hmac-sha2-256-etm@openssh.com,hmac-sha1-etm@open
[root@localhost ansible]# ansible all -m raw -a 'cat /abc/test |grep bbb '
192.168.100.42 | CHANGED | rc=0 >>
bbb
Shared connection to 192.168.100.42 closed.
user模块
user模块用于管理受管主机的用户帐号
[root@localhost ansible]# ansible all -m user -a 'name=tom uid=10101 system=yes create_home=no shell=/sbin/nologin'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": false,
"group": 132,
"home": "/home/tom",
"name": "tom",
"shell": "/sbin/nologin",
"state": "present",
"system": true,
"uid": 10101
}
[root@localhost ansible]# ansible all -m shell -a 'ls /home'
192.168.100.42 | CHANGED | rc=0 >>
abc
qqq
tom
top
[root@localhost ansible]# ansible all -m user -a 'name=tom uid=10101 system=yes create_home=no shell=/sbin/nologin'
192.168.100.42 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"append": false,
"changed": false,
"comment": "",
"group": 10003,
"home": "/home/tom",
"move_home": false,
"name": "tom",
"shell": "/sbin/nologin",
"state": "present",
"uid": 10101
}
[root@localhost ansible]#
[root@localhost ansible]# ansible all -m shell -a 'grep tom /etc/passwd'
192.168.100.42 | CHANGED | rc=0 >>
tom:x:10101:10003::/home/tom:/sbin/nologin
[root@localhost ansible]# ansible all -m user -a 'name=tom state=absent'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "tom",
"remove": false,
"state": "absent"
}
[root@localhost ansible]# ansible all -m shell -a 'grep tom /etc/passwd'
192.168.100.42 | FAILED | rc=1 >>
non-zero return code
[root@localhost ansible]#
group模块
group模块用于在受管主机上添加或删除组
[root@localhost ansible]# ansible all -m group -a 'name=abc gid=300'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 300,
"name": "abc",
"state": "present",
"system": false
}
[root@localhost ansible]# ansible all -m shell -a 'grep abc /etc/group'
192.168.100.42 | CHANGED | rc=0 >>
abc:x:300:
[root@localhost ansible]#
//删除受管主机上的组
[root@localhost ansible]# ansible all -m group -a 'name=test state=absent'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "abc",
"state": "absent"
}
[root@localhost ansible]# ansible all -m shell -a 'grep test /etc/group'
192.168.100.42 | FAILED | rc=1 >>
non-zero return code
service模块
service模块用于管理受管主机上的服务
[root@localhost ansible]# ansible all -m shell -a 'systemctl is-active mariadb'
192.168.100.42 | FAILED | rc=3 >>
inactivenon-zero return code
[root@localhost ansible]# ansible all -m service -a 'name=mariadb state=started'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "mariadb",
"state": "started",
"status": {
[root@localhost ansible]# ansible all -m shell -a 'systemctl is-active mariadb'
192.168.100.42| CHANGED | rc=0 >>
activ
[root@localhost ansible]# ansible all -m shell -a 'systemctl is-enabled mariadb'
192.168.100.42 | FAILED | rc=1 >>
disablednon-zero return code
[root@localhost ansible]# ansible all -m service -a 'name=mariadb enabled=yes'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"enabled": true,
"name": "mariadb",
"status": {
[root@localhost ansible]# ansible all -m shell -a 'systemctl is-enabled mariadb'
192.168.100.42 | CHANGED | rc=0 >>
enabled
root@localhost ansible]# ansible all -m service -a 'name=mariadb state=stopped'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "mariadb",
"state": "stopped",
"status": {
[root@localhost ansible]# ansible all -m shell -a 'systemctl is-active mariadb'
192.168.100.42 | FAILED | rc=3 >>
inactivenon-zero return code
[root@localhost ansible]# ansible all -m shell -a 'ss -antl'
192.168.100.42 | CHANGED | rc=0 >>
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
copy模块
copy模块用于复制文件至远程受管主机
ansible 172.16.103.129 -m copy -a 'src(源地址)=路径 dest(目的地)=路径’
使用force参数控制是否强制覆盖
ansible 受管主机 -m copy -a 'src=源路径 dest=目标路径 force=yes/no
froce=no 如果目标文件已经存在,则不操作;目标文件不存在,则会拷贝过去
froce=yes 如果目标文件已经存在,则会强制覆盖
使用backup参数控制是否备份文件
ansible 被管理机名称 -m copy -a 'src=原路径 dest=复制过去的路径 backup=yes owner= group= mode= ’
backup=yes表示如果拷贝的文件内容与原内容不一样,则会备份一份
[root@localhost ansible]# ansible all -m command -a 'ls /root'
192.168.100.42 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg
[root@localhost ansible]#
[root@localhost ansible]# ansible all -m copy -a 'src=/tmp/project dest=/root'
192.168.100.42 | CHANGED => {
"changed": true,
"dest": "/root/",
"src": "/root/project"
}
[root@localhost ansible]# ansible all -m command -a 'ls /root'
192.168.100.42 | CHANGED | rc=0 >>
anaconda-ks.cfg
initial-setup-ks.cfg
project
fetch模块
fetch模块与copy模块类似,但作用相反。用于把远程机器的文件拷贝到本地,不支持复制目录。
ansible 被管理机名称 -m fetch -a 'src=原路径 dest=目的路径‘
root@localhost ansible]# ansible all -m fetch -a 'src=/root/test1 dest=/root'
192.168.100.42 | CHANGED => {
"changed": true,
"checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"dest": "/root/192.168.220.8/root/test1",
"md5sum": "d41d8cd98f00b204e9800998ecf8427e",
"remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
"remote_md5sum": null
}
[root@localhost ~]# ll
drwxr-xr-x. 3 root root 18 7月 15 04:46 192.168.100.42
[root@localhost ~]# ls 192.168.100.42/root/
test1
file模块
file模块主要用于远程主机上的文件操作,file模块包含如下选项:
-
force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
-
group:定义文件/目录的属组
-
mode:定义文件/目录的权限
-
owner:定义文件/目录的属主
-
path:必选项,定义文件/目录的路径
-
recurse:递归的设置文件的属性,只对目录有效
-
src:要被链接的源文件的路径,只应用于state=link的情况
-
dest:被链接到的路径,只应用于state=link的情况
-
state:
directory:如果目录不存在,创建目录
file:即使文件不存在,也不会被创建
link:创建软链接
hard:创建硬链接
touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
absent:删除目录、文件或者取消链接文件
[root@localhost ansible]# ansible all -m shell -a 'ls -l /root/'
192.168.220.8 | CHANGED | rc=0 >>
total 4
-rw-------. 1 root root 1023 Jul 13 12:06 anaconda-ks.cfg
-rw-r--r--. 1 root root 1445 7月 13 16:23 initial-setup-ks.cfg
drwxr-xr-x. 3 root root 17 Jul 17 07:22 project
[root@localhost ansible]# ansible all -m file -a 'path=/root/project owner=harry group=harry mode=0644'
192.168.100.42| CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 1000,
"group": "harry",
"mode": "0644",f
"owner": "harry",
"path": "/root/project",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 17,
"state": "directory",
"uid": 1005
}
[root@localhost ansible]# ansible all -m command -a 'ls -l /root/'
192.168.ansible all -m shell -a 'ls -l /root/' | CHANGED | rc=0 >>
total 8
-rw-------. 1 root root 1023 Jul 13 12:06 anaconda-ks.cfg
drw-r--r--. 3 harry harry 17 Jul 17 07:22 project
[root@localhost ansible]# ansible all -m file -a 'path=/root/school state=touch owner=root group=harry mode=0644'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/root/school",
"gid": 1000,
"group": "harry",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 0,
"state": "file",
"uid": 0
}
[root@localhost ansible]# ansible all -m shell -a 'ls -l /root'
192.168.100.42 | CHANGED | rc=0 >>
total 1
drwxrwxrwx. 3 root root 15 Jul 17 18:57 qq
[root@localhost ansible]# ansible all -m file -a 'path=/root/qq mode=0644'
192.168.220.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"path": "/root/qq",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 15,
"state": "directory",
"uid": 0
}
[root@localhost ansible]# ansible all -m shell -a 'ls -l /root'
192.168.100.42 | CHANGED | rc=0 >>
total 1
drw-r--r--. 3 root root 15 Jul 17 18:57 qq
[root@localhost ansible]# ansible all -m file -a 'path=/root/cd/d state=directory recurse=yes owner=harry'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 1000,
"group": "root",
"mode": "0755",
"owner": "harry",
"path": "/root/cd/d",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 6,
"state": "directory",
"uid": 1000
}
[root@localhost ansible]# ansible all -m shell -a 'ls -l /root/cd/'
192.168.100.42 | CHANGED | rc=0 >>
total 0
drwxr-xr-x. 2 harry root 6 Jul 17 19:03 d
[root@localhost ansible]# ansible all -m file -a 'src=/root/school dest=/tmp/ss state=link'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/tmp/ss",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 12,
"src": "/root/school",
"state": "link",
"uid": 0
}
[root@localhost ansible]# ansible all -m shell -a 'ls -l /tmp'
192.168.100.42 | CHANGED | rc=0 >>
total 4
drwx------. 2 root root 41 Jul 17 22:26 ansible_command_payload_i4oj3u65
drwxr-xr-x. 2 root root 19 Jul 17 03:40 hsperfdata_root
-rwx------. 1 root root 701 Jul 13 12:06 ks-script-a85snvsm
lrwxrwxrwx. 1 root root 12 Jul 17 22:25 ss -> /root/school
[root@localhost ansible]# ansible all -m file -a 'src=/root/qqq dest=/tmp/qq state=hard'192.168.100.42| CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/tmp/qq",
"gid": 0,
"group": "root",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 0,
"src": "/root/qqq",
"state": "hard",
"uid": 0
}
lineinfile模块
确保特定行是否在文件中
[root@localhost ansible]# ansible all -m shell -a 'cat /etc/selinux/config'
192.168.100.42| CHANGED | rc=0 >>
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
[root@localhost ansible]# ansible all -m lineinfile -a 'path=/etc/selinux/config regexp="SELINUX=" line="SELINUX=disabled"'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[root@localhost ansible]# ansible all -m shell -a 'cat /etc/selinux/config'
192.168.100.42 | CHANGED | rc=0 >>
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
[root@localhost ansible]# ansible all -m lineinfile -a 'path=/etc/selinux/config line=SELINUX=permissive'
192.168.220.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@localhost ansible]# ansible all -m shell -a 'cat /etc/selinux/config'
192.168.100.42 | CHANGED | rc=0 >>
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
SELINUX=permissive
[root@localhost ansible]# ansible all -m lineinfile -a 'path=/etc/selinux/config state=absent line=SELINUX=permissive'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"found": 1,
"msg": "1 line(s) removed"
}
[root@localhost ansible]# ansible all -m shell -a 'cat /etc/selinux/config'192.168.220.8 | CHANGED | rc=0 >>
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
[root@localhost ansible]# ansible all -m shell -a 'ls -l /root'
192.168.220.8 | CHANGED | rc=0 >>
total 1
-rw-r--r--. 1 root root 5 Jul 18 02:01 test
[root@localhost ansible]# ansible all -m lineinfile -a 'path=/root/test owner=harry group=zzz line=jjyy state=present'
192.168.220.8 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "ownership, perms or SE linux context changed"
}
[root@localhost ansible]# ansible all -m shell -a 'ls -l /root'
192.168.220.8 | CHANGED | rc=0 >>
total 1
-rw-r--r--. 1 harry zzz 5 Jul 18 02:01 test
[root@localhost ansible]# ansible all -m shell -a 'cat /etc/selinux/config'
192.168.220.8 | CHANGED | rc=0 >>
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
[root@localhost ansible]# ansible all -m lineinfile -a 'path=/etc/selinux/config insertafter="SELINUX=disabled" line="SELINUX=permissive"'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line added"
}
[root@localhost ansible]# ansible all -m shell -a 'cat /etc/selinux/config'
192.168.100.42 | CHANGED | rc=0 >>
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
SELINUX=permissive
yum 模块
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
- name:要管理的包名
- state:要进行的操作
state常用的值:
- latest:安装软件
- installed:安装软件
- present:安装软件
- removed:卸载软件
- absent:卸载软件
若想使用yum来管理软件,请确保受控机上的yum源无异常
[root@localhost ansible]# ansible all -m shell -a 'rpm -qa |grep vsftpd'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'. If
you need to use command because yum, dnf or zypper 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.
192.168.100.42 | FAILED | rc=1 >>
non-zero return code
[root@localhost ansible]# ansible all -m yum -a 'name=vsftpd state=present'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: vsftpd-3.0.3-34.el8.x86_64"
]
}
[root@localhost ansible]# ansible all -m shell -a 'rpm -qa |grep vsftpd'
[WARNING]: Consider using the yum, dnf or zypper module rather than running 'rpm'. If
you need to use command because yum, dnf or zypper 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.
192.168.100.42 | CHANGED | rc=0 >>
vsftpd-3.0.3-34.el8.x86_64
script模块
script模块用于在受管主机上执行主控机上的脚本
ansible 受管主机 -m 模块 -a '脚本路径’
[root@localhost ansible]# ls -l /root/
总用量 1
-rwxr-xr-x. 1 root root 23 7月 17 21:43 a.sh
[root@localhost ansible]# cat /root/a.sh
#!/bin/bash
touch test
[root@localhost ansible]# ansible all -m script -a '/root/a.sh'
192.168.100.42| CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to 192.168.220.8 closed.\r\n",
"stderr_lines": [
"Shared connection to 192.168.220.8 closed."
],
"stdout": "",
"stdout_lines": []
}
[root@localhost ansible]# ansible all -m shell -a 'ls -l /root'
192.168.100.42 | CHANGED | rc=0 >>
total 0
-rw-r--r--. 1 root root 0 Jul 17 23:51 test
template模块
template模块用于生成一个模板,并可将其传输至远程主机上
[root@localhost ~]# ls
ansible.cfg
[root@localhost ~]# cat ansible.cfg
abcd
[root@localhost ansible]# ansible all -m template -a 'src=~/ansible.cfg dest=/tmp/'
192.168.100.42 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "dce58c1e5d0ee30acff9282898b28b54b27c4e0a",
"dest": "/tmp/ansible.cfg",
"gid": 0,
"group": "root",
"md5sum": "6b6804d918ecdf41b7363b7ee7027346",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 5,
"src": "/root/.ansible/tmp/ansible-tmp-1626584618.565992-357179-252666804746292/source",
"state": "file",
"uid": 0
}
[root@localhost ansible]# ansible all -m shell -a 'cat /tmp/ansible.cfg'
192.168.100.42 | CHANGED | rc=0 >>
abcd