Skip to content

Commit

Permalink
ADD Notes
Browse files Browse the repository at this point in the history
  • Loading branch information
dwe committed Mar 18, 2024
1 parent aebee80 commit 84e7334
Show file tree
Hide file tree
Showing 309 changed files with 64,342 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
ansible command与shell模块

两个模块都是用于执行linux命令的,这对于命令熟悉的工程师来说,用起来非常high。

shell模块与command模块差不多(command模块不能执行一些类似$HOME,>,<,|等符号,但shell可以)

https://docs.ansible.com/ansible/latest/modules/command_module.html

https://docs.ansible.com/ansible/latest/modules/shell_module.html

## 一、shell模块

```
[root@manage01 ~]# ansible -m shell 192.168.98.201 -a "ls /root"
192.168.98.201 | CHANGED | rc=0 >>
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
initial-setup-ks.cfg
nginx.service
nginx_study
[root@manage01 ~]# ansible -m shell 192.168.98.201 -a "echo 'hello world' > /tmp/baishuming"
192.168.98.201 | CHANGED | rc=0 >>
[root@manage01 ~]# ansible -m shell 192.168.98.201 -a "cat /tmp/baishuming"
192.168.98.201 | CHANGED | rc=0 >>
hello world
注意shell模块不是什么命令都能使用,比如vim这样的交互命令,不建议大家去记忆哪些命令不可以,大家只要养成任何在生产环境里的命令都要先在测试环境里测试一下的习惯就好。
```

## 二、command模块

```
[root@manage01 ~]# ansible -m command 192.168.98.201 -a "ls /root"
192.168.98.201 | CHANGED | rc=0 >>
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
initial-setup-ks.cfg
nginx.service
nginx_study
[root@manage01 ~]# ansible -m command 192.168.98.201 -a "echo 'baism hello' > /tmp/baism_123"
192.168.98.201 | CHANGED | rc=0 >>
baism hello > /tmp/baism_123
[root@manage01 ~]# ansible -m command 192.168.98.201 -a "cat /tmp/baism_123"
192.168.98.201 | FAILED | rc=1 >>
cat: /tmp/baism_123: 没有那个文件或目录non-zero return code
发现没有/tmp/baism_123 证明上一条命令未能执行成功
```

## 三、学习视频

[视频: command与shell模块](https://www.bilibili.com/video/BV19J41167sM?p=23)
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
## 一、copy模块(重点)

copy模块用于对文件的远程拷贝操作(如把本地的文件拷贝到远程的机器上)

https://docs.ansible.com/ansible/latest/modules/copy_module.html#copy-module

| 参数 | 说明 |
| -------- | ------------------------------------------------------ |
| src | 文件源路径 |
| dest | 目标路径 |
| content | 往目标文件输入内容 |
| force | 强制 yes or no |
| backup | 是否备份有冲突的源文件[文件名相同,内容不同] yes or no |
| checksum | 拷贝完整性校验,使用sha1sum生成校验码 |
| owner | 目标文件所有者 |
| group | 目标文件所属组 |
| mode | 目标文件权限 |

**拷贝manage01机器/root/readme文件到group1组的机器。**

1. 要求校验完整性,注意[checksum 是根据sha1算法做校验的]
2. 所有者、所属组都是sko
3. 权限0400

```
[root@manage01 ~]# sha1sum readme
f8182e9ccdbe6efd13eb36a056a7db203fe66e40 readme
[root@manage01 ~]# ansible -m copy group1 -a "src=/root/readme dest=/opt checksum=f8182e9ccdbe6efd13eb36a056a7db203fe66e40 owner=sko group=sko mode=0400"
192.168.98.203 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "f8182e9ccdbe6efd13eb36a056a7db203fe66e40",
"dest": "/opt/readme",
"gid": 1000,
"group": "sko",
"md5sum": "f8c2686842f9fa79361e8928867a1983",
"mode": "0400",
"owner": "sko",
"size": 1214,
"src": "/root/.ansible/tmp/ansible-tmp-1571366236.6664524-201027506158575/source",
"state": "file",
"uid": 1000
}
192.168.98.202 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "f8182e9ccdbe6efd13eb36a056a7db203fe66e40",
"dest": "/opt/readme",
"gid": 1001,
"group": "sko",
"md5sum": "f8c2686842f9fa79361e8928867a1983",
"mode": "0400",
"owner": "sko",
"size": 1214,
"src": "/root/.ansible/tmp/ansible-tmp-1571366236.6522918-97522631781022/source",
"state": "file",
"uid": 1001
}
192.168.98.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "f8182e9ccdbe6efd13eb36a056a7db203fe66e40",
"dest": "/opt/readme",
"gid": 1001,
"group": "sko",
"md5sum": "f8c2686842f9fa79361e8928867a1983",
"mode": "0400",
"owner": "sko",
"size": 1214,
"src": "/root/.ansible/tmp/ansible-tmp-1571366236.6274443-88161541412737/source",
"state": "file",
"uid": 1001
}
```

copy模块拷贝时要注意拷贝目录后面是否带"/"符号

```
/etc/yum.repos.d后面不带/符号,则表示把/etc/yum.repos.d整个目录拷贝到/tmp/目录下
[root@manage01 ~]# ansible group1 -m copy -a 'src=/etc/yum.repos.d dest=/tmp/'
/etc/yum.repos.d/后面带/符号,则表示把/etc/yum.repos.d/目录里的所有文件拷贝到/tmp/目录下
[root@manage01 ~]# ansible group1 -m copy -a 'src=/etc/yum.repos.d/ dest=/tmp/'
```

使用content参数直接往远程文件里写内容(会覆盖原内容)

```
[root@manage01 ~]# ansible -m file group1 -a "path=/tmp/zutuanxue_333 state=touch"
[root@manage01 ~]# ansible -m copy group1 -a "content='baism\nhello world\n' dest=/tmp/zutuanxue_333"
注意:ansible中-a后面的参数里也有引号时,记得要单引双引交叉使用,如果都为双引会出现问题
```

使用force参数控制是否强制覆盖

```
如果目标文件已经存在,则不覆盖
[root@manage01 ~]# ansible group1 -m copy -a "src=/tmp/zutuanxue_222 dest=/tmp/zutuanxue_333 force=no"
如果目标文件已经存在,则会强制覆盖
[root@manage01 ~]# ansible group1 -m copy -a "src=/tmp/zutuanxue_222 dest=/tmp/zutuanxue_333 force=yes"
```

使用backup参数控制是否备份文件

```
backup=yes表示如果拷贝的文件内容与原内容不一样,则会备份一份
如果拷贝过来的文件本机存在,group1的机器上会将/tmp/333备份一份(备份文件命名加上时间),再远程拷贝新的文件为/tmp/333
[root@manage01 ~]# ansible group1 -m copy -a "src=/etc/fstab dest=/tmp/zutuanxue_333 backup=yes"
```

## 二、学习视频

[视频:copy模块](https://www.bilibili.com/video/BV19J41167sM?p=14)
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
## 一、cron模块

cron模块用于管理周期性时间任务。

https://docs.ansible.com/ansible/latest/modules/cron_module.html#cron-module

| 参数 | 说明 |
| ------------ | ------------------------ |
| name | 计划任务的名称 |
| user | 执行计划任务的用户 |
| job | 计划任务命令 |
| minute | 执行计划任务的分 默认为* |
| hour | 执行计划任务的时 默认为* |
| day | 执行计划任务的日 默认为* |
| month | 执行计划任务的月 默认为* |
| week | 执行计划任务的周 默认为* |
| state absent | 删除计划任务 |

创建一个cron任务,不指定user的话,默认就是root(因为我这里是用root操作的)。
如果minute,hour,day,month,week不指定的话,默认都为*

每天14:23 执行echo “haha”>/tmp/test

```
[root@manage01 ~]# ansible -m cron group1 -a 'name="cron test" user=root job="echo haha > /tmp/test" minute=23 hour=12'
192.168.98.203 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"envs": [],
"jobs": [
"cron test"
]
}
192.168.98.202 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"envs": [],
"jobs": [
"cron test"
]
}
192.168.98.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"envs": [],
"jobs": [
"cron test"
]
}
```

删除cron任务

```
[root@manage01 ~]# ansible -m cron group1 -a 'name="cron test" state=absent'
192.168.98.203 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"envs": [],
"jobs": []
}
192.168.98.202 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"envs": [],
"jobs": []
}
192.168.98.201 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"envs": [],
"jobs": []
}
```

## 二、学习视频

[视频:cron模块](https://www.bilibili.com/video/BV19J41167sM?p=18)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
## 一、fetch模块

fetch模块与copy模块类似,但作用相反。用于把远程机器的文件拷贝到本地。

https://docs.ansible.com/ansible/latest/modules/fetch_module.html#fetch-module

将group1组机器的/opt/readme 拷贝到manage01的/opt目录

注意:不管是拷贝多个机器还是一个机器的文件,在管理机本地目录都会按照

IP/路径/文件名

的方式命名,防止冲突

```
[root@manage01 ~]# ansible -m fetch group1 -a "src=/opt/readme dest=/opt"
192.168.98.203 | CHANGED => {
"changed": true,
"checksum": "f8182e9ccdbe6efd13eb36a056a7db203fe66e40",
"dest": "/opt/192.168.98.203/opt/readme",
"md5sum": "f8c2686842f9fa79361e8928867a1983",
"remote_checksum": "f8182e9ccdbe6efd13eb36a056a7db203fe66e40",
"remote_md5sum": null
}
192.168.98.202 | CHANGED => {
"changed": true,
"checksum": "f8182e9ccdbe6efd13eb36a056a7db203fe66e40",
"dest": "/opt/192.168.98.202/opt/readme",
"md5sum": "f8c2686842f9fa79361e8928867a1983",
"remote_checksum": "f8182e9ccdbe6efd13eb36a056a7db203fe66e40",
"remote_md5sum": null
}
192.168.98.201 | CHANGED => {
"changed": true,
"checksum": "f8182e9ccdbe6efd13eb36a056a7db203fe66e40",
"dest": "/opt/192.168.98.201/opt/readme",
"md5sum": "f8c2686842f9fa79361e8928867a1983",
"remote_checksum": "f8182e9ccdbe6efd13eb36a056a7db203fe66e40",
"remote_md5sum": null
}
```

## 二、学习视频

[视频:fetch模块](https://www.bilibili.com/video/BV19J41167sM?p=15)
Loading

0 comments on commit 84e7334

Please sign in to comment.