Copy模块将ansible主机文件copy到远端主机:
[root@es1 ~]# ansible webservers -m copy -a 'src=elasticsearch-7.6.2-x86_64.rpm dest=/tmp' -u root -k
SSH password:
192.168.0.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "83c2903d4410723ddb3dc7e7cac2f9540c41d9e7",
"dest": "/tmp/elasticsearch-7.6.2-x86_64.rpm",
"gid": 0,
"group": "root",
"md5sum": "b2548723ce328318c8ffb3cc4b6221db",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:admin_home_t:s0",
"size": 296535521,
"src": "/root/.ansible/tmp/ansible-tmp-1604676732.66-15971-134196282688/source",
"state": "file",
"uid": 0
}
File模块创建目录和文件:
在目标主机创建目录:
[root@es1 ansible]# ansible webservers -m file -a 'dest=/tmp/abc mode=600 state=directory' -u root -k
SSH password:
192.168.0.13 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0600",
"owner": "root",
"path": "/tmp/abc",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 6,
"state": "directory",
"uid": 0
}
在目标主机创建文件:
[root@es1 ansible]# ansible webservers -m file -a 'dest=/tmp/abc/aa.txt mode=600 state=touch' -u root -k
SSH password:
192.168.0.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/abc/aa.txt",
"gid": 0,
"group": "root",
"mode": "0600",
"owner": "root",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 0,
"state": "file",
"uid": 0
}
删除目录,目录内容也会删除:
[root@es1 ansible]# ansible webservers -m file -a 'dest=/tmp/abc/ mode=600 state=absent' -u root -k
SSH password:
192.168.0.13 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/tmp/abc/",
"state": "absent"
}
yum模块:
[root@es1 ansible]# ansible webservers -m yum -a 'name=memcached state=present' -u root -k
SSH password:
192.168.0.13 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"memcached"
]
},
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * extras: mirrors.ustc.edu.cn\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package memcached.x86_64 0:1.4.15-10.el7_3.1 will be installed\n--> Processing Dependency: libevent-2.0.so.5()(64bit) for package: memcached-1.4.15-10.el7_3.1.x86_64\n--> Running transaction check\n---> Package libevent.x86_64 0:2.0.21-4.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n memcached x86_64 1.4.15-10.el7_3.1 base 85 k\nInstalling for dependencies:\n libevent x86_64 2.0.21-4.el7 base 214 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package (+1 Dependent package)\n\nTotal download size: 299 k\nInstalled size: 901 k\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal 634 kB/s | 299 kB 00:00 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : libevent-2.0.21-4.el7.x86_64 1/2 \n Installing : memcached-1.4.15-10.el7_3.1.x86_64 2/2 \n Verifying : memcached-1.4.15-10.el7_3.1.x86_64 1/2 \n Verifying : libevent-2.0.21-4.el7.x86_64 2/2 \n\nInstalled:\n memcached.x86_64 0:1.4.15-10.el7_3.1 \n\nDependency Installed:\n libevent.x86_64 0:2.0.21-4.el7 \n\nComplete!\n"
]
}
user模块创建用户:
[root@es1 ansible]# ansible webservers -m user -a 'name=appuser password=123456 shell=/sbin/nologin' -u root -k
SSH password:
[WARNING]: The input password appears not to have been hashed. The 'password' argument must be encrypted for this module to work properly.
192.168.0.12 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1000,
"home": "/home/appuser",
"name": "appuser",
"password": "NOT_LOGGING_PASSWORD",
"shell": "/sbin/nologin",
"state": "present",
"system": false,
"uid": 1000
}
Git模块:
[root@es1 ansible]# ansible webservers -m git -a 'repo=https://github.com/ansible/ansible.git dest=/tmp/ansible' -u root -k
Service模块:
[root@es1 ansible]# ansible webservers -m service -a 'name=memcached state=started' -u root -k
SSH password:
setup模块:
[root@es1 ansible]# ansible webservers -m setup -u root -k
SSH password:
过滤出你想要的值:
[root@es1 ansible]# ansible webservers -m setup -a "filter=ansible_*_mb" -u root -k
SSH password:
192.168.0.13 | SUCCESS => {
"ansible_facts": {
"ansible_memfree_mb": 1564,
"ansible_memory_mb": {
"nocache": {
"free": 1789,
"used": 195
},
"real": {
"free": 1564,
"total": 1984,
"used": 420
},
"swap": {
"cached": 0,
"free": 511,
"total": 511,
"used": 0
}
},
"ansible_memtotal_mb": 1984,
"ansible_swapfree_mb": 511,
"ansible_swaptotal_mb": 511,
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
192.168.0.12 | SUCCESS => {
"ansible_facts": {
"ansible_memfree_mb": 1525,
"ansible_memory_mb": {
"nocache": {
"free": 1777,
"used": 207
},
"real": {
"free": 1525,
"total": 1984,
"used": 459
},
"swap": {
"cached": 0,
"free": 511,
"total": 511,
"used": 0
}
},
"ansible_memtotal_mb": 1984,
"ansible_swapfree_mb": 511,
"ansible_swaptotal_mb": 511,
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
[root@es1 ansible]#