1、以all主机组为目标执行id命令
#使用ansible的command模块查看普通用户
[xh@server78 ansible]$ ansible all -m command -a 'id xiaobai'
node1 | CHANGED | rc=0 >>
uid=1001(xh) gid=1001(xh) groups=1001(xh)
server79 | CHANGED | rc=0 >>
uid=1001(xh) gid=1001(xh) groups=1001(xh)
node0 | CHANGED | rc=0 >>
uid=1000(xh) gid=1000(xh) groups=1000(xh)
2、使用copy模块修改所有主机上的/etc/motd文件内容为welcome to ansible
[xh@server78 ansible]$ ansible all -m copy -a 'content="welcome to ansible\n" dest=/etc/motd'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "be2ff9fcbc3a14e6a25b7b3b9694bd609932c2a8",
"dest": "/etc/motd",
"gid": 0,
"group": "root",
"md5sum": "3cf0bed579ed98f458513f63bc965156",
"mode": "0644",
"owner": "root",
"size": 19,
"src": "/home/xiaobai/.ansible/tmp/ansible-tmp-1669125286.63-5466-72797344474556/source",
"state": "file",
"uid": 0
}
node0 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "be2ff9fcbc3a14e6a25b7b3b9694bd609932c2a8",
"dest": "/etc/motd",
"gid": 0,
"group": "root",
"md5sum": "3cf0bed579ed98f458513f63bc965156",
"mode": "0644",
"owner": "root",
"size": 19,
"src": "/home/xiaobai/.ansible/tmp/ansible-tmp-1669125286.71-5465-123861559876151/source",
"state": "file",
"uid": 0
}
server78 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "be2ff9fcbc3a14e6a25b7b3b9694bd609932c2a8",
"dest": "/etc/motd",
"gid": 0,
"group": "root",
"md5sum": "3cf0bed579ed98f458513f63bc965156",
"mode": "0644",
"owner": "root",
"size": 19,
"src": "/home/xiaobai/.ansible/tmp/ansible-tmp-1669125286.63-5467-213316770980505/source",
"state": "file",
"uid": 0
}
3、使用command模块查看/etc/motd文件的内容
#查看写入内容
[xh@server78 ansible]$ ansible all -m command -a 'cat /etc/motd'
node1 | CHANGED | rc=0 >>
welcome to ansible
server78 | CHANGED | rc=0 >>
welcome to ansible
node0 | CHANGED | rc=0 >>
welcome to ansible
4、使用user模块创建用户wukong,uid为2000
#创建wukong用户uid为2000
[xh@server78 ansible]$ ansible all -m user -a 'name=wukong uid=2000 state=present'
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 2000,
"home": "/home/wukong",
"name": "wukong",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 2000
}
node0 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 2000,
"home": "/home/wukong",
"name": "wukong",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 2000
}
server78 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 2000,
"home": "/home/wukong",
"name": "wukong",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 2000
}
#查看wukong用户
[xh@server78 ansible]$ ansible all -m command -a 'id wukong'
node1 | CHANGED | rc=0 >>
uid=2000(wukong) gid=2000(wukong) groups=2000(wukong)
server78 | CHANGED | rc=0 >>
uid=2000(wukong) gid=2000(wukong) groups=2000(wukong)
node0 | CHANGED | rc=0 >>
uid=2000(wukong) gid=2000(wukong) groups=2000(wukong)
5、使用yum模块安装httpd软件包并使用service模块启动该服务
#用yum模块安装httpd
[xh@server78 ansible]$ ansible all -m yum -a 'name=httpd state=latest'
server78 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"changes": {
"installed": [],
"updated": []
},
"msg": "",
"rc": 0,
"results": [
"All packages providing httpd are up to date",
""
]
}
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"httpd"
],
"updated": []
},
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\nResolving Dependencies\n--> Running transaction check\n---> Package httpd.x86_64 0:2.4.6-95.el7.centos will be installed\n--> Processing Dependency: httpd-tools = 2.4.6-95.el7.centos for package: httpd-2.4.6-95.el7.centos.x86_64\n--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-95.el7.centos.x86_64\n--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-95.el7.centos.x86_64\n--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-95.el7.centos.x86_64\n--> Running transaction check\n---> Package apr.x86_64 0:1.4.8-7.el7 will be installed\n---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed\n---> Package httpd-tools.x86_64 0:2.4.6-95.el7.centos will be installed\n---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n httpd x86_64 2.4.6-95.el7.centos aliyun 2.7 M\nInstalling for dependencies:\n apr x86_64 1.4.8-7.el7 aliyun 104 k\n apr-util x86_64 1.5.2-6.el7 aliyun 92 k\n httpd-tools x86_64 2.4.6-95.el7.centos aliyun 93 k\n mailcap noarch 2.1.41-2.el7 aliyun 31 k\n\nTransaction Summary\n================================================================================\nInstall 1 Package (+4 Dependent packages)\n\nTotal download size: 3.0 M\nInstalled size: 10 M\nDownloading packages:\n--------------------------------------------------------------------------------\nTotal 242 kB/s | 3.0 MB 00:12 \nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : apr-1.4.8-7.el7.x86_64 1/5 \n Installing : apr-util-1.5.2-6.el7.x86_64 2/5 \n Installing : httpd-tools-2.4.6-95.el7.centos.x86_64 3/5 \n Installing : mailcap-2.1.41-2.el7.noarch 4/5 \n Installing : httpd-2.4.6-95.el7.centos.x86_64 5/5 \n Verifying : httpd-tools-2.4.6-95.el7.centos.x86_64 1/5 \n Verifying : mailcap-2.1.41-2.el7.noarch 2/5 \n Verifying : apr-1.4.8-7.el7.x86_64 3/5 \n Verifying : httpd-2.4.6-95.el7.centos.x86_64 4/5 \n Verifying : apr-util-1.5.2-6.el7.x86_64 5/5 \n\nInstalled:\n httpd.x86_64 0:2.4.6-95.el7.centos \n\nDependency Installed:\n apr.x86_64 0:1.4.8-7.el7 apr-util.x86_64 0:1.5.2-6.el7 \n httpd-tools.x86_64 0:2.4.6-95.el7.centos mailcap.noarch 0:2.1.41-2.el7 \n\nComplete!\n"
]
}
node0 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"msg": "",
"rc": 0,
"results": [
"Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
"Installed: httpd-2.4.37-21.module_el8.2.0+494+1df74eae.x86_64",
"Installed: httpd-filesystem-2.4.37-21.module_el8.2.0+494+1df74eae.noarch",
"Installed: mod_http2-1.11.3-3.module_el8.2.0+486+c01050f0.1.x86_64",
"Installed: httpd-tools-2.4.37-21.module_el8.2.0+494+1df74eae.x86_64",
"Installed: mailcap-2.1.48-3.el8.noarch",
"Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
"Installed: centos-logos-httpd-80.5-2.el8.noarch",
"Installed: apr-1.6.3-9.el8.x86_64",
"Installed: apr-util-1.6.1-6.el8.x86_64"
]
}
#使用service模块启动该服务
[xh@server78 ansible]$ ansible all -m service -a 'name=httpd state=started'
server78| CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "httpd",
"state": "started"
node0 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"name": "httpd",
"state": "started"
node1 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "httpd",
"state": "started"