本章主要介绍Ansible中最常见模块的使用
ansible 机器名 -m 模块x -a "模块的参数"
[blab@node01 ~]$ ansible-doc -l
a10_server Manage A10 Ne.>
a10_server_axapi3 Manage A10 Ne.>
a10_service_group Manage A10 Ne.>
ansible-doc 模块名
ansible 主机组 -m shell -a "系统命令"
[blab@node01 ~]$ ansible node02 -m shell -a "hostname"
node02 | CHANGED | rc=0 >>
node02
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "hostnamexxx"
node02 | FAILED | rc=127 >>
/bin/sh: hostnamexxx: 未找到命令non-zero return code
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m file -a "path=/opt/hosts owner=root group=tom mode=444 state=touch"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/opt/hosts",
"gid": 1002,
"group": "tom",
"mode": "0444",
"owner": "root",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 0,
"state": "file",
"uid": 0
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "ls -l /opt/hosts"
node02 | CHANGED | rc=0 >>
-r--r--r--. 1 root tom 0 12月 15 11:53 /opt/hosts
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m file -a "src=/opt/hosts dest=/opt/hosts123 state=link"
node02 | 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@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "ls -l /opt"
node02 | CHANGED | rc=0 >>
总用量 0
-r--r--r--. 1 root tom 0 12月 15 11:53 hosts
lrwxrwxrwx. 1 root root 10 12月 15 11:54 hosts123 -> /opt/hosts
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m file -a "name=/opt/hosts123 state=absent"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"path": "/opt/hosts123",
"state": "absent"
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m file -a "name=/opt/xx state=directory setype=default_t"
node02 | 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
}
[blab@node01 ~]$
验证
[blab@node01 ~]$ ansible node02 -m shell -a "ls -dZ /opt/xx"
node02 | CHANGED | rc=0 >>
unconfined_u:object_r:default_t:s0 /opt/xx
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m file -a "name=/opt/hosts owner=tom group=users mode=000"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 100,
"group": "users",
"mode": "0000",
"owner": "tom",
"path": "/opt/hosts",
"secontext": "unconfined_u:object_r:usr_t:s0",
"size": 0,
"state": "file",
"uid": 1002
}
[blab@node01 ~]$
验证
[blab@node01 ~]$ ansible node02 -m shell -a "ls -l /opt/hosts"
node02 | CHANGED | rc=0 >>
----------. 1 tom users 0 12月 15 11:53 /opt/hosts
[blab@node01 ~]$
注意:指定文件使用name或path都是可以的
[blab@node01 ~]$ ansible node02 -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.
node02 | CHANGED | rc=0 >>
[blab@node01 ~]$
验证:
[blab@node01 ~]$ ansible node02 -m shell -a "ls /opt/"
node02 | CHANGED | rc=0 >>
[blab@node01 ~]$
ansible 主机组 -m copy -a "src=/path1/file1 dest=path2/"
[blab@node01 ~]$ ansible node02 -m copy -a "src=/etc/ansible/hosts mode=000 owner=tom dest=/opt"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "da996f1a52dbae3b6b43a6c50d761e4ed5ec9a9f",
"dest": "/opt/hosts",
"gid": 0,
"group": "root",
"md5sum": "1564b951dc7c8511c6f9ee842653c541",
"mode": "0000",
"owner": "tom",
"secontext": "system_u:object_r:usr_t:s0",
"size": 1016,
"src": "/home/blab/.ansible/tmp/ansible-tmp-1702614292.969332-5748-241468010987805/source",
"state": "file",
"uid": 1002
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "ls -l /opt/hosts"
node02 | CHANGED | rc=0 >>
----------. 1 tom root 1016 12月 15 12:24 /opt/hosts
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m copy -a 'content="123123" dest=/opt/11.txt '
node02 | 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-1702614452.0289679-5825-32381171106061/source",
"state": "file",
"uid": 0
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "cat /opt/11.txt"
node02 | CHANGED | rc=0 >>
123123
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m fetch -a "src=/opt/hosts dest=./"
node02 | CHANGED => {
"changed": true,
"checksum": "da996f1a52dbae3b6b43a6c50d761e4ed5ec9a9f",
"dest": "/home/blab/node02/opt/hosts",
"md5sum": "1564b951dc7c8511c6f9ee842653c541",
"remote_checksum": "da996f1a52dbae3b6b43a6c50d761e4ed5ec9a9f",
"remote_md5sum": null
}
[blab@node01 ~]$
[blab@node01 ~]$ tree node02
node02
└── opt
└── hosts
1 directory, 1 file
[blab@node01 ~]$
[root@node01 ~]# cat /etc/yum.repos.d/aa.repo
[aa]
name=aa
baseurl=file:///mnt/AppStream
enabled=1
gpgcheck=0
[bb]
name=bb
baseurl=///mnt/BaseOS
enabled=1
gpgcheck=0
[root@node01 ~]#
[blab@node01 ~]$ ansible node02 -m yum_repository -a "name=app description='this is appstream' baseurl=ftp://192.168.182.200/dvd/AppStream gpgcheck=yes gpgkey=ftp://192.168.182.200/dvd/RPM-GPG-KEY-redhat-release"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "app",
"state": "present"
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "ls /etc/yum.repos.d"
node02 | CHANGED | rc=0 >>
aa.repo
app.repo
redhat.repo
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "cat /etc/yum.repos.d/app.repo"
node02 | CHANGED | rc=0 >>
[app]
baseurl = ftp://192.168.182.200/dvd/AppStream
gpgcheck = 1
gpgkey = ftp://192.168.182.200/dvd/RPM-GPG-KEY-redhat-release
name = this is appstream
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m yum_repository -a "name=baseos description='this is baseos' baseurl=ftp://192.168.182.200/dvd/BaseOS gpgcheck=yes gpgkey=ftp://192.168.182.200/dvd/RPM-GPG-KEY-redhat-release"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"repo": "baseos",
"state": "present"
}
[blab@node01 ~]$
yum模块常见的参数包括以下几个:
? ? ? ?①present或installed:用于安装软件包,没有指定state时的默认值就是installed。
????????②absent或removed:用于卸载软件包。
注意:yum模块可以用package模块替代,用于在 Ubuntu等其他系统上管理软件包。
[blab@node01 ~]$ ansible node02 -m yum -a "name=vsftpd state=installed"
node02 | 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"
]
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -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.
node02 | CHANGED | rc=0 >>
vsftpd-3.0.3-34.el8.x86_64
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m yum -a "name=vsftpd state=absent"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Removed: vsftpd-3.0.3-34.el8.x86_64"
]
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m yum -a "name=vsftpd state=latest"
[blab@node01 ~]$ ansible node02 -m yum -a "name='@开发工具' state=installed"
node02 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"msg": "Nothing to do",
"rc": 0,
"results": [
"Group development installed."
]
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "systemctl is-active vsftpd"
node02 | FAILED | rc=3 >>
inactivenon-zero return code
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m service -a "name=vsftpd state=started enabled=yes"
验证:
[blab@node01 ~]$ ansible node02 -m shell -a "systemctl is-active vsftpd"
node02 | CHANGED | rc=0 >>
active
[blab@node01 ~]$
[root@node02 ~]# systemctl is-active vsftpd
active
[root@node02 ~]#
[blab@node01 ~]$ ansible node02 -m parted -a "device=/dev/sda number=1 part_end=2GiB state=present"
[blab@node01 ~]$ ansible node02 -m shell -a "lsblk"
node02 | CHANGED | rc=0 >>
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
└─sda1 8:1 0 2G 0 part //可以看到添加成功
sr0 11:0 1 10.2G 0 rom
nvme0n1 259:0 0 30G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 29G 0 part
├─rhel-root 253:0 0 26G 0 lvm /
└─rhel-swap 253:1 0 3G 0 lvm [SWAP]
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m parted -a "device=/dev/sda number=2 part_start=2GiB part_end=4GiB state=present"
[blab@node01 ~]$ ansible node02 -m shell -a "lsblk"
node02 | CHANGED | rc=0 >>
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 2G 0 part
└─sda2 8:2 0 2G 0 part //可以看到第二个已经创建成功
sr0 11:0 1 10.2G 0 rom
nvme0n1 259:0 0 30G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 29G 0 part
├─rhel-root 253:0 0 26G 0 lvm /
└─rhel-swap 253:1 0 3G 0 lvm [SWAP]
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m parted -a "device=/dev/sda number=2 state=absent"
验证:
[blab@node01 ~]$ ansible node02 -m shell -a "lsblk"
node02 | CHANGED | rc=0 >>
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
└─sda1 8:1 0 2G 0 part
sr0 11:0 1 10.2G 0 rom
nvme0n1 259:0 0 30G 0 disk
├─nvme0n1p1 259:1 0 1G 0 part /boot
└─nvme0n1p2 259:2 0 29G 0 part
├─rhel-root 253:0 0 26G 0 lvm /
└─rhel-swap 253:1 0 3G 0 lvm [SWAP]
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "lsblk"
node02 | CHANGED | rc=0 >>
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 2G 0 part
├─sda2 8:2 0 2G 0 part
└─sda3 8:3 0 2G 0 part
[blab@node01 ~]$ ansible node02 -m filesystem -a "dev=/dev/sda3 fstype=xfs"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m filesystem -a "dev=/dev/sda3 fstype=xfs force=yes"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "mkdir /123"
[blab@node01 ~]$ ansible node02 -m mount -a "src=/dev/sda3 path=/123 state=mounted fstype=xfs opts=defaults,ro"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"fstype": "xfs",
"name": "/123",
"opts": "defaults,ro",
"passno": "0",
"src": "/dev/sda3"
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "df -Th | grep sda3"
node02 | CHANGED | rc=0 >>
/dev/sda3 xfs 2.0G 256K 2.0G 1% /123
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m mount -a "src=/dev/sda3 path=/123 state=absent"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dump": "0",
"fstab": "/etc/fstab",
"name": "/123",
"opts": "defaults",
"passno": "0",
"src": "/dev/sda3"
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "vgs"
node02 | CHANGED | rc=0 >>
VG #PV #LV #SN Attr VSize VFree
rhel 1 2 0 wz--n- <29.00g 0
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m lvg -a "pvs=/dev/sda1,/dev/sda2 vg=vg0 pesize=16 state=present"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "vgs"
node02 | CHANGED | rc=0 >>
VG #PV #LV #SN Attr VSize VFree
rhel 1 2 0 wz--n- <29.00g 0
vg0 2 0 0 wz--n- <3.97g <3.97g
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m lvg -a "vg=vg0 pesize=16 state=absent"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true
}
[blab@node01 ~]$
验证:
[blab@node01 ~]$ ansible node02 -m shell -a "vgs"
node02 | CHANGED | rc=0 >>
VG #PV #LV #SN Attr VSize VFree
rhel 1 2 0 wz--n- <29.00g 0
[blab@node01 ~]$
请使用ansible node02 -m lvg -a "pvs=ldev/sda1,/dev/sda2 vg-vg0 pesize=16state-present"命令再次把vg0创建出来。
[blab@node01 ~]$ ansible node02 -m shell -a 'lvs'
node02 | CHANGED | rc=0 >>
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m lvol -a "vg=vg0 lv=lv0 size=1G"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": ""
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "lvs"
node02 | CHANGED | rc=0 >>
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lv0 vg0 -wi-a----- 1.00g
[blab@node01 ~]$
可以看到,此逻辑卷已经创建出来了。
[blab@node01 ~]$ ansible node02 -m lvol -a "vg=vg0 lv=lv0 state=absent force=yes"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m firewalld -a "service=http immediate=yes permanent=yes state=enabled"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "firewall-cmd --list-all"
node02 | CHANGED | rc=0 >>
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client http ssh //可以看到已经添加http服务
ports:
protocols:
forward: no
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m firewalld -a"port=808/tcp immediate=yes permanent=yes state=enabled"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "Permanent and Non-Permanent(immediate) operation, Changed port 808/tcp to enabled"
}
[blab@node01 ~]$
验证:
[blab@node01 ~]$ ansible node02 -m shell -a "firewall-cmd --list-all"
node02 | CHANGED | rc=0 >>
public (active)
target: default
icmp-block-inversion: no
interfaces: ens160
sources:
services: cockpit dhcpv6-client http ssh
ports: 808/tcp //可以看到端口已经添加
protocols:
forward: no
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m firewalld -a "port=808/tcp immediate=yes permanent=yes state=disabled"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "Permanent and Non-Permanent(immediate) operation, Changed port 808/tcp to disabled"
}
[blab@node01 ~]$
[root@node02 ~]# cat /opt/aa.txt
aa=111
bb=222
[root@node02 ~]#
[blab@node01 ~]$ ansible node02 -m replace -a "path=/opt/aa.txt regexp=aa=111 replace=xx=666"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "1 replacements made"
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "cat /opt/aa.txt"
node02 | CHANGED | rc=0 >>
xx=666
bb=222
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m replace -a "path=/opt/aa.txt regexp=^bb.+ replace=xx=666"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "1 replacements made"
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "cat /opt/aa.txt"
node02 | CHANGED | rc=0 >>
xx=666
xx=666
[blab@node01 ~]$
[root@node02 ~]# cat /opt/bb.txt
aa=111
bb=222
[root@node02 ~]#
[blab@node01 ~]$ ansible node02 -m lineinfile -a "path=/opt/bb.txt regexp=^aa line=xx=666"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"backup": "",
"changed": true,
"msg": "line replaced"
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m shell -a "cat /opt/bb.txt"
node02 | CHANGED | rc=0 >>
xx=666
bb=222
[blab@node01 ~]$
注意:var 和 msg 不可以同时使用
[blab@node01 ~]$ ansible node02 -m debug -a "msg='hello luo'"
node02 | SUCCESS => {
"msg": "hello luo"
}
[blab@node01 ~]$
[blab@node01 ~]$ cat test1.sh
#!/bin/bash
hostname
[blab@node01 ~]$ chmod +x test1.sh
[blab@node01 ~]$
[blab@node01 ~]$ ansible db1 -m script -a "./test1.sh"
node02 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to node02 closed.\r\n",
"stderr_lines": [
"Shared connection to node02 closed."
],
"stdout": "node02\r\n",
"stdout_lines": [
"node02"
]
}
[root@node02 ~]# grep group1 /etc/group
[root@node02 ~]#
[blab@node01 ~]$ ansible node02 -m group -a "name=group1 state=present"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 1003,
"name": "group1",
"state": "present",
"system": false
}
[blab@node01 ~]$
[root@node02 ~]# grep group1 /etc/group
group1:x:1003:
[root@node02 ~]#
[blab@node01 ~]$ ansible node02 -m group -a "name=group1 state=absent"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "group1",
"state": "absent"
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m user -a "name=lisi group=root password=123456 state=present"
[WARNING]: The input password appears not to have been hashed. The 'password'
argument must be encrypted for this module to work properly.
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 0,
"home": "/home/lisi",
"name": "lisi",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1003
}
[blab@node01 ~]$
[root@node02 ~]# su - lisi
[lisi@node02 ~]$ exit
注销
[root@node02 ~]#
[blab@node01 ~]$ ansible node02 -m user -a "name=lisi state=absent remove=yes"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"force": false,
"name": "lisi",
"remove": false,
"state": "absent"
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m get_url -a "url=ftp://ftp.rhce.cc/auto/web.tar.gz dest=/opt/"
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum_dest": null,
"checksum_src": "0453d8c9e9ce9bda838b659b901b74a2e709fb15",
"dest": "/opt/web.tar.gz",
"elapsed": 1,
"gid": 0,
"group": "root",
"md5sum": "41c019324f7b8160ce6944102a755de8",
"mode": "0644",
"msg": "OK (1406 bytes)",
"owner": "root",
"secontext": "system_u:object_r:usr_t:s0",
"size": 1406,
"src": "/home/blab/.ansible/tmp/ansible-tmp-1702885918.9421644-8590-163172471685725/tmpy3h7y39h",
"state": "file",
"status_code": null,
"uid": 0,
"url": "ftp://ftp.rhce.cc/auto/web.tar.gz"
}
[blab@node01 ~]$
[root@node02 ~]# ls /opt/
11.txt aa.txt bb.txt hosts web.tar.gz
[root@node02 ~]#
[blab@node01 ~]$ ansible node02 -m setup
1 键 1 : 值2 键 2 :{3 子键 a : 值 a4 子键 b : 值 b5 ...6 }
[blab@node01 ~]$ ansible node02 -m setup -a "filter=ansible_bios_version"
node02 | SUCCESS => {
"ansible_facts": {
"ansible_bios_version": "6.00",
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m setup -a "filter=ansible_default_ipv4"
node02 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.182.193",
"alias": "ens160",
"broadcast": "192.168.182.255",
"gateway": "192.168.182.2",
"interface": "ens160",
"macaddress": "00:0c:29:ba:79:52",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.182.0",
"type": "ether"
},
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
[blab@node01 ~]$
[blab@node01 ~]$ ansible node02 -m setup -a "filter=ansible_default_ipvs.address"
node02 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false
}
[blab@node01 ~]$