---
description: ansible
---

# 20190605

ansible重點:

{% embed url="https://blog.51cto.com/11886307/2385720" %}



![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgaJDSGVzI2vBtu5Svh%2F-LgaBBMouaC1Y3ujGFa5%2Fimage.png?alt=media\&token=b0c3fad2-1cc8-4a99-adf0-a2187ef287bc)

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-Lgaa4R1LPfUEDpLKTGf%2Fimage.png?alt=media\&token=2aafb765-35a0-4dee-aa32-45f4017cd591)

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-LgaaCUjyFpwTS3Fexkl%2Fimage.png?alt=media\&token=451591cb-4121-4010-a8f2-23783c5cafe0)



\*先確保 sshd有啟動

\[監控端]:

```
# yum install epel-release
# yum install ansible -y
# vim /etc/ansible/hosts
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgaZdJi-w9FA3C6Z36y%2F-LgaZrBVPvelXsTLyn6b%2Fimage.png?alt=media\&token=43511cf5-5c14-450a-ad23-e69c6df21fe0)

```
# ansible app1 -m command -a "ls /tmp"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgaZdJi-w9FA3C6Z36y%2F-Lga_9fqdjq_Xlb0Fvaw%2Fimage.png?alt=media\&token=a88c0c2b-7b2c-410a-8e97-f341d0450bb1)

```
# ansible app1 -m command -a "ls /root"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgaZdJi-w9FA3C6Z36y%2F-Lga_YthLYNFDH5Vh0SF%2Fimage.png?alt=media\&token=c3330846-d1f4-4a09-b5d7-118c8ce07fe3)

```
# ansible app1 -m command -a "ls /root" -v
# ansible app1 -m command -a "ls /root" -vv
# ansible app1 -m command -a "ls /root" -vvv
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-LgaaPPuMQ3MHryWB3gs%2Fimage.png?alt=media\&token=3f80d025-5210-4f26-9414-b4bb2fe7b613)

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-Lgaan0EgKhKI7ZHYcYl%2Fimage.png?alt=media\&token=efd0f5b4-72c1-4a5b-b41e-50b960b76a7d)

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-Lgaar4AOS6nr-70cYwM%2Fimage.png?alt=media\&token=0ba1a24e-bcf4-4274-bd32-c365d6620055)

```
# ansible all --list-hosts
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-Lgab--sz1MY5U6CdRWY%2Fimage.png?alt=media\&token=facc8eb6-ba2d-4f55-8e6e-2fa0f308746e)

```
# ansible all -m shell -a "ls /root/a*"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-LgabBHSnpqHIFjvywmt%2Fimage.png?alt=media\&token=b07411dc-1aea-4f7e-87e6-7bf382206eb7)

```
# ansible app1 -m script -a a.sh
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-LgabNA-8b_q28Mil2v9%2Fimage.png?alt=media\&token=fc356237-f485-40b8-9c5d-503aed62e217)

\[被控端]

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-LgabghqQvfAvvKmShXx%2Fimage.png?alt=media\&token=9052cc34-47e4-43eb-902d-c26e5f7ca96b)

\[監控端]

```
# ansible app1 -m copy -a "src=/root/mya.txt dest=/root/myb.txt backup=yes"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-Lgabr5L_skvBYCUCYxH%2Fimage.png?alt=media\&token=afbb17a7-84eb-4897-956d-8d52b3a29cc9)

\[被控端]

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-Lgac5I7PsGAd-dx9IPv%2Fimage.png?alt=media\&token=4f25d4d4-fddf-4f1b-8bcf-d54381dd2b13)

\[監控端]

```
# ansible app1 -m copy -a "src=/root/mya.txt dest=/root/myb.txt backup=yes mode=600 owner=user"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-LgacR-mDQQDZdY7sXKC%2Fimage.png?alt=media\&token=1ee4bfdf-7fa2-4914-a9d4-145e42c01670)

\[被控端]

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-LgacW02dMao23kliylL%2Fimage.png?alt=media\&token=7fe971a1-4f4f-4b31-b29d-03a47528f44e)

\[監控端]

```
# ansible app1 -m fetch -a "src=/etc/passwd dest=/tmp"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-LgadIwzeOeV-ZiRv_z3%2Fimage.png?alt=media\&token=9344ee55-d04a-4d17-a00a-713bb59a9b21)

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-Lga_lwPYf19KS0_yrEw%2F-Lgad-BFFVRcVtTkT4KX%2Fimage.png?alt=media\&token=0506ff04-d383-44ea-9ba1-59827130ddba)

```
# ansible app1 -m file -a "path=/root/aa owner=user mode=600"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgbITVZv4i1e1iLnnti%2F-LgbIvcpuvHhEPRTYU7N%2Fimage.png?alt=media\&token=b88afa34-3072-4689-89a0-919ab02da461)

\[被控端]

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgbITVZv4i1e1iLnnti%2F-LgbJA2tk3IZ_Oe5R_Lt%2Fimage.png?alt=media\&token=156c7002-992d-4b3e-8af0-e2d30f87d636)

\[監控端]

```
# ansible app1 -a command -a "rpm -q v"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgbITVZv4i1e1iLnnti%2F-LgbJOtTIrM4HYx3RHDR%2Fimage.png?alt=media\&token=915621f9-ca7a-4cda-833f-a16382e1dd66)

```
# ansible app1 -m command -a "rpm -q vsftpd"
# ansible app1 -m yum -a "name=vsftpd state=absent"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgbITVZv4i1e1iLnnti%2F-LgbNBVVFh7R_Rnn7ZIx%2Fimage.png?alt=media\&token=cece5666-f1da-487b-8fdc-4bb8c42b2638)

```
# ansible app1 -m command -a "rpm -q vsftpd"
# ansible app1 -m yum -a "name=vsftpd"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgbITVZv4i1e1iLnnti%2F-LgbQFgsu_oLJbqGl8XR%2Fimage.png?alt=media\&token=cdc52661-aeb1-4397-8728-36a86500308d)

```
# ansible app1 -m command -a "rpm -q vsftpd"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgbITVZv4i1e1iLnnti%2F-LgbQquK3vfdmqjm6DWa%2Fimage.png?alt=media\&token=f8c76f2a-f9d3-4a0d-82ba-83f965620f6a)

```
# ansible app1 -m command -a "systemctl status httpd"
# ansible app1 -m service -a "name=httpd state=started"
# ansible app1 -m service -a "name=httpd state=stoped"
# ansible app1 -m service -a "name=httpd state=restarted"
# ansible app1 -m service -a "name=httpd state=reloaded"
```

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgbITVZv4i1e1iLnnti%2F-LgbR7b0j8WLM_i9P-JK%2Fimage.png?alt=media\&token=ed242576-ae4d-4edf-a3f5-329ae88dd9f0)

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgbITVZv4i1e1iLnnti%2F-LgbhwTUf9MiF_bl3yWN%2Fimage.png?alt=media\&token=8b20f24e-b4ae-4fa0-a14d-180022506d97)

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgbITVZv4i1e1iLnnti%2F-Lgbi3gXEZ8un92JPSXm%2Fimage.png?alt=media\&token=6125394b-e9ab-46ee-b9c6-40c8b93a916d)

![](https://1005948644-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LZgzp55XQBZ7U2SrFtZ%2F-LgbITVZv4i1e1iLnnti%2F-LgbiFDA_yaBM0TpGsEr%2Fimage.png?alt=media\&token=adba0e29-bbbc-4372-95ac-c7bc39873c50)













