script模块
script模块可以帮助我们在远程主机上执行 ansible 管理主机上的脚本,也就是说,脚本一直存在于 ansible 管理主机本地,不需要手动拷贝到远程主机后再执行
[root@ansible ~]# ansible 192.168.194.133 -m script -a '/root/testst.sh'
template模板模块
可以把指定的文件复制到指定的位置上
[root@ansible yum]# vi CentOS-Base.repo
:%s/$releasever/7/g
[root@ansible yum]# ansible 192.168.194.133 -m template -a'src=/files/yum/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo'
192.168.194.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "1db98d747fdc3a4144709b328cd3ee76de23719f",
"dest": "/etc/yum.repos.d/CentOS-Base.repo",
"gid": 0,
"group": "root",
"md5sum": "d70130cb2886538d7ec67a061699d4d3",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:system_conf_t:s0",
"size": 2323,
"src": "/root/.ansible/tmp/ansible-tmp-1598783249.2869942-2807-26677647496332/source",
"state": "file",
"uid": 0
}
[root@ansible yum]# ansible 192.168.194.133 -m template -a'src=/files/yum/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo'
copy复制模块,
将文件复制到被管理主机
[root@ansible ~]# ansible localhost -m dnf -a 'name=zsh download_only=yes download_dir=/tnp/'
[root@ansible ~]# ansible 192.168.194.133 -m yum -a 'name=zsh state=present'
192.168.194.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"zsh"
]
},
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror\nLoading mirror speeds from cached hostfile\n * base: mirrors.aliyun.com\n * extras: mirrors.aliyun.com\n * updates: mirrors.aliyun.com\nResolving Dependencies\n--> Running transaction check\n---> Package zsh.x86_64 0:5.0.2-34.el7_8.2 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package Arch Version Repository Size\n================================================================================\nInstalling:\n zsh x86_64 5.0.2-34.el7_8.2 updates 2.4 M\n\nTransaction Summary\n================================================================================\nInstall 1 Package\n\nTotal download size: 2.4 M\nInstalled size: 5.6 M\nDownloading packages:\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n Installing : zsh-5.0.2-34.el7_8.2.x86_64 1/1 \n Verifying : zsh-5.0.2-34.el7_8.2.x86_64 1/1 \n\nInstalled:\n zsh.x86_64 0:5.0.2-34.el7_8.2 \n\nComplete!\n"
]
}
group
模块用于在受控机添加或删除组
[root@ansible ~]# ansible 192.168.194.133 -m group -a 'name=runtime state=present'
192.168.194.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 1000,
"name": "runtime",
"state": "present",
"system": false
}
[root@ansible ~]#
[root@ansible ~]# ansible 192.168.194.133 -m group -a 'name=runtime state=absent'
192.168.194.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "runtime",
"state": "absent"
}
ansible常用模块之user
user模块用于管理受控机的用户帐号。
[root@ansible ~]# ansible 192.168.194.133 -m user -a 'name=tom uid=1000 state=present'
192.168.194.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "",
"create_home": true,
"group": 1000,
"home": "/home/tom",
"name": "tom",
"shell": "/bin/bash",
"state": "present",
"system": false,
"uid": 1000
}
service 用于管理受控机上的服务
[root@ansible ~]# ansible 192.168.194.133 -m group -a 'name=runtime state=absent'
192.168.194.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "runtime",
"state": "absent"
}
[root@ansible ~]# ansible 192.168.194.133 -m user -a 'name=tom uid=1000 state=present'
192.168.194.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
}
[root@ansible ~]# ansible 192.168.194.133 -m service -a 'name=postfix state=stopped'
192.168.194.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "postfix",
"state": "stopped",
"status": {
"ActiveEnterTimestamp": "Sun 2020-08-30 13:57:00 CST",
"ActiveEnterTimestampMonotonic": "6416965",
"ActiveExitTimestampMonotonic": "0",
"ActiveState": "active",
root@ansible ~]# ansible 192.168.194.133 -m service -a 'n
ame=postfix enabled=no'
192.168.194.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"enabled": false,
"name": "postfix",
"status": {
```
```
[root@ansible ~]# ansible 192.168.194.133 -m service -a 'name=postfix state=stopped'
192.168.194.133 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "postfix",
"state": "stopped",
"status": {
playbook
是 一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活。简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的模式,可作为一个适合部署复杂应用程序的基础。Playbook可以定制配置,可以按照指定的操作步骤有序执行,支持同步和异步方式。值得注意的是playbook是通过YAML格式来进行描述定义的
[root@ansible ~]# vim mvpiay.yml
---
- name: fuwu
hosts: 192.168.194.132
tasks:
- name: guanbipostfixfwu
service:
name: postfix
state: stopped
enabled: no
~
[root@ansible ~]# ansible-playbook mvpiay.yml
PLAY [fuwu] ********************************************************************
TASK [Gathering Facts] *********************************************************
ok: [192.168.194.132]
TASK [guanbipostfixfwu] ********************************************************
changed: [192.168.194.132]
PLAY RECAP *********************************************************************
192.168.194.132 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0