# ansible
批量在遠程主機上執行命令
python2.7編寫
## 安裝
第一步:下載epel源
```shell
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
```
第二步:安裝
```shell
yum install -y ansible
```
## ansible 命令格式
```shell
Usage: ansible <host-pattern> [options]
-a MODULE_ARGS 模塊參數
-C, --check 檢查語法
-f FORKS 并發
--list-hosts 列出主機列表
-m MODULE_NAME 模塊名字
```
ssh 認證方式
- 密碼
- 秘鑰
- ssh-keygen 生成密鑰對
- ssh-copy-id 復制公鑰到遠程主機
- 私鑰加密,公鑰解密
查看ansible生成的文件
```shell
rpm -ql ansible
/etc/ansible
/etc/ansible/ansible.cfg # ansible 配置文件
/etc/ansible/hosts
/etc/ansible/roles
```
## ping 走的是ICMP協議
## ansible第一條命令
```shell
ansible 192.168.12.26 -m ping ping 一臺機器
ansible 192.168.12.26,192.168.12.28 -m ping ping多臺機器
ansible all -m ping ping所有機器
ansible web -m ping ping 一個組
ansible 'web:!db' -m ping ping web中有但是db中沒有
ansible "web:&db" -m ping ping web和db的并集
ansible "web:db" -m ping ping web和db的交集
```
hosts文件內容
```shell
# It should live in /etc/ansible/hosts
#
# - Comments begin with the '#' character #是注釋
# - Blank lines are ignored 空行被忽略
# - Groups of hosts are delimited by [header] elements []表示主機組
# - You can enter hostnames or ip addresses 可以輸入主機名或者ip地址
# - A hostname/ip can be a member of multiple groups 一臺主機可以被分配多個組
www[001:006].example.com www001到www006.example.com
```
### host-pattern格式
- 單個的機器
- 多個的機器,逗號隔開
- 全部機器,all
- 可以寫一個分組
- 可以寫多個分組
- 并集
- 逗號隔開
- 冒號隔開
- 交集,:&隔開
- 差集: :!隔開
## ansible-doc 查看模塊幫助信息
```shell
ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
-j 以json格式顯示所有模塊信息
-l 列出所有的模塊
-s 顯示模塊的摘要信息
# 直接顯示模塊的所有幫助信息
```
ansible 特性: 冪等性 不管執行幾次,結果都是一樣的
# 命令相關
## command
```shell
ansible web -a 'ls'
ansible web -a 'chdir=/tmp pwd' # 先切換目錄,在執行相應的命令,一般情況下在編譯時候使用
ansible web -a 'creates=/tmp pwd' # 如果creates的文件存在,則不執行后面的操作
ansible web -a 'removes=/tmp pwd' # 如果removes的文件存在,則執行后面的操作
ansible web -a 'removes=/tmp mkdir /data' # 會執行后面的mkdir命令
ansible web -a 'creates=/data2 mkdir /data2' #會執行后面的mkdir命令
```
補充
```shell
查看用戶是否被創建成功
tail -1 /etc/passwd
tail -1 /etc/shadow
id
echo '1' | passwd --stdin alex 非交互式設置密碼
```
## shell
```shell
<>|;& $ 這些特殊字符command不支持
ansible web -m shell -a 'echo "1" | passwd --stdin alex' 設置alex的密碼
ansible 192.168.12.25 -m shell -a '/root/a.sh' 執行shell腳本,前提是腳本有可執行權限
ansible 192.168.12.25 -m shell -a '/root/a.py' 執行python腳本,前提是腳本有可執行權限
```
## script
```shell
ansible db -m script -a '/root/m.sh' 執行管控機上的文件
ansible web -m script -a 'creates=/root/a.sh /root/m.sh' # 查看的是被管控機上的文件是否存在
```
# 文件相關的模塊
## copy
```shell
ansible db -m copy -a "dest=/tmp/a.sh src=/root/m.sh" 復制文件到遠程主機
ansible db -m copy -a "dest=/tmp/a.sh src=/root/m.sh backup=yes" 復制文件并備份遠程文件
ansible web -m copy -a "dest=/tmp/a.sh src=/root/m.sh owner=alex mode=700" 修改復制后的文件的屬主和權限
ansible web -m copy -a "src=/etc/init.d dest=/tmp" 復制目錄到遠程主機
ansible web -m copy -a "src=/etc/init.d/ dest=/tmp" 復制目錄里面的文件到遠程主機
ansible web -m copy -a "src=/etc/ansible dest=/tmp owner=alex" 復制目錄到遠程主機,并修改目錄的屬主,并且里面文件的屬主也被修改了
ansible web -m copy -a "content='大弦嘈嘈如急雨,小弦切切如私語' dest=/tmp/b.txt" 直接將content里面的內容添加到dest的文件里面
```
## file
### 補充
```shell\
ln -s 原文件地址 目標文件地址 創建軟連接
ln 創建硬鏈接
```
```shell
ansible cache -m file -a "path=/tmp/wupeiqi state=directory" 創建一個目錄
ansible cache -m file -a "path=/tmp/wupeiqi.txt state=touch" 創建一個文件
ansible cache -m file -a "path=/tmp/t state=link src=/etc/init.d" 創建軟連接 path是目標文件 src是源文件
ansible cache -m file -a "path=/tmp/t state=absent " 刪除文件