1、以all主机组为目标执行id命令
[xiaoming@centos7 chap01]$ ansible all -m command -a 'id'
node01 | CHANGED | rc=0 >>
uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
centos7 | CHANGED | rc=0 >>
uid=0(root) gid=0(root) 组=0(root) 环境=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
node02 | CHANGED | rc=0 >>
uid=0(root) gid=0(root) groups=0(root),1001(mygroup) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
2、使用copy模块修改所有主机上的/etc/motd文件内容为welcome to ansible
[xiaoming@centos7 chap01]$ ansible all -m copy -a 'content="welcom to ansible\n" dest=/etc/motd'
centos7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "ac533aed11b0decf60acd68ec36359c11992d8d3",
"dest": "/etc/motd",
"gid": 0,
"group": "root",
"md5sum": "feb7f11b486bc2178fcadc3c72f26d79",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:etc_t:s0",
"size": 18,
"src": "/home/xiaoming/.ansible/tmp/ansible-tmp-1669134053.79-8034-140887631386806/source",
"state": "file",
"uid": 0
}
node01 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "ac533aed11b0decf60acd68ec36359c11992d8d3",
"dest": "/etc/motd",
"gid": 0,
"group": "root",
"md5sum": "feb7f11b486bc2178fcadc3c72f26d79",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:etc_t:s0",
"size": 18,
"src": "/home/xiaoming/.ansible/tmp/ansible-tmp-1669134053.79-8033-163661273656350/source",
"state": "file",
"uid": 0
}
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "ac533aed11b0decf60acd68ec36359c11992d8d3",
"dest": "/etc/motd",
"gid": 0,
"group": "root",
"md5sum": "feb7f11b486bc2178fcadc3c72f26d79",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:etc_t:s0",
"size": 18,
"src": "/home/xiaoming/.ansible/tmp/ansible-tmp-1669134053.82-8032-189534451343401/source",
"state": "file",
"uid": 0
}
3、使用command模块查看/etc/motd文件的内容
[xiaoming@centos7 chap01]$ ansible all -m command -a 'cat /etc/motd'
node01 | CHANGED | rc=0 >>
welcom to ansible
centos7 | CHANGED | rc=0 >>
welcom to ansible
node02 | CHANGED | rc=0 >>
welcom to ansible
4、使用user模块创建用户wukong,uid为2000
[xiaoming@centos7 chap01]$ ansible all -m user -a 'name=wukong state=present uid=2000'
centos7 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"append": false,
"changed": true,
"comment": "",
"group": 1001,
"home": "/home/wukong",
"move_home": false,
"name": "wukong",
"shell": "/bin/bash",
"state": "present",
"uid": 2000
}
node01 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"append": false,
"changed": true,
"comment": "",
"group": 1003,
"home": "/home/wukong",
"move_home": false,
"name": "wukong",
"shell": "/bin/bash",
"state": "present",
"uid": 2000
}
node02 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"append": false,
"changed": true,
"comment": "",
"group": 3462,
"home": "/home/wukong",
"move_home": false,
"name": "wukong",
"shell": "/bin/bash",
"state": "present",
"uid": 2000
}
5、使用yum模块安装httpd软件包并使用service模块启动该服务
#yum模块安装httpd
[xiaoming@centos7 chap01]$ ansible all -m yum -a 'name=httpd'
node01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"msg": "Nothing to do",
"rc": 0,
"results": []
}
node02 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"msg": "Nothing to do",
"rc": 0,
"results": []
}
centos7 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"msg": "",
"rc": 0,
"results": [
"httpd-2.4.6-95.el7.centos.x86_64 providing httpd is already installed"
]
}
#service模块启动该服务
[xiaoming@centos7 chap01]$ ansible all -m service -a 'name=httpd state=started'
centos7 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"name": "httpd",
"state": "started",
"status": {
node01 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"name": "httpd",
"state": "started",
"status": {
node02 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": false,
"name": "httpd",
"state": "started",
"status": {