1. 程式人生 > >ansible 批量在遠程主機上執行命令

ansible 批量在遠程主機上執行命令

lin must 不用 director enter 空白行 direct host 使用

ansible 和 saltstack

都是為了同時在多臺主機上執行相同的命令, 但是 salt配置麻煩,ansible基本不用配置, ansible 通過ssh來連接並控制被控節點

1. 安裝

第一步: 下載epel源

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

第二步: 安裝ansible

yum install -y ansible

2. 管控主機秘鑰登錄被控主機

ssh 秘鑰登錄

ssh-keygen # 用來生成ssh的密鑰對
ssh-copy-id 192.168.107.131
# 復制秘鑰到遠程主機

3. ansible 命令格式

技術分享圖片

-a MODULE_ARGS, --args=MODULE_ARGS   # 模塊的參數
-C, --check                          # 檢查
-f FORKS, --forks=FORKS              #用來做高並發的
--list-hosts                         #列出主機列表
-m MODULE_NAME                       #模塊名稱
--syntax-check                       # 語法檢查
                                                

4. ansible hosts

查看ansible 生成的文件

技術分享圖片

ansible/hosts 文件

# This is the default ansible hosts file.
#
# 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 # 一臺主機可以在多個組裏面

[web]
192.168.181.133
192.168.181.134

[db]
192.168.181.134
192.168.181.135

5. 模塊

ansible-doc 查看文檔

 ansible-doc [-l|-F|-s] [options] [-t <plugin type> ] [plugin]
 -j           #以json的方式返回ansible的所有模塊
 -l, --list   #列出所有的ansible的模塊
 -s           #以片段式顯示ansible的幫助信息

技術分享圖片

1. ping模塊

host-pattern格式

技術分享圖片

2. 系統默認模塊 command, 可以不指定-m

第一個command命令

技術分享圖片

ansible web -a chdir=/tmp pwd# 切換目錄執行命令,使用場景是編譯安裝時使用
ansible web -a creates=/tmp pwd  # 用來判斷/tmp目錄是否存在,存在就不執行操作
ansible web -a creates=/data pwd # 因為data不存在,所有才會執行pwd命令
ansible web -a removes=/tmp pwd  # 用來判斷tmp目錄是否存在,存在就執行操作
ansible web -a removes=/data pwd # 因為data不存在,所有才不會執行

如果命令裏包含特殊符號, 需要同shell模塊 : $ < > | will not work use shell module

3. shell 執行遠程文件

ansible web -m shell -a echo "123" | passwd --stdin alex   # 批量創建密碼
ansible 192.168.107.131 -m shell -a bash a.sh              # 執行遠程文件方式一
ansible 192.168.107.131 -m shell -a /root/a.sh             # 執行遠程文件方式二,文件必須有執行權限, 需要 chmod +x
ansible 192.168.107.131 -m shell -a /root/a.py             # 執行遠端的Python腳本

被管控機192.168.107.131

技術分享圖片

管控機就會創建 bulijngbuling2文件夾

4.script 執行本地文件

ansible web -m script -a /root/m.sh                # 執行本地的文件,即管控機上的腳本
ansible web -m script -a removes=/root/m.sh /root/m.sh    # 用來判斷被管控機上是不是存在文件,如果存在,存在就執行,不存在就不執行
ansible web -m script -a creates=/root/a.sh /root/m.sh    #用來判斷被管控機上是不是存在文件,如果存在,就不執行

管控機

技術分享圖片

db組群 就會創建 hhhhhh文件夾

5. copy : Copies files to remote locations 把文件批量拷到被管控機上

backup     # 備份,以時間戳結尾
src # 源文件 dest      # 目的地址 group     # 文件的屬組 mode      # 文件的權限 r
4 w 2 x 1 owner      #文件的屬主 # 通過md5碼來判斷是否需要復制 ansible web -m copy -a src=/root/m.sh dest=/tmp/a.sh               # 復制本地文件的到遠程主機 ansible web -m copy -a src=/root/m.sh dest=/tmp/a.sh mode=755          # 修改文件的權限 ansible web -m copy -a src=/root/m.sh dest=/tmp/a.sh mode=755 owner=ryan   # 修改文件的屬主 ansible web -m copy -a src=/etc/init.d dest=/tmp/ mode=755 owner=ryan   # 復制本地目錄到遠程主機,如果改變文件的屬性,則文件夾內的文件也會被改變 ansible web -m copy -a src=/etc/init.d/ dest=/tmp/ mode=755 owner=ryan # 復制本地目錄內的所有文件到遠程主機 ansible web -m copy -a "content=‘白雲深處有人家\n‘ dest=/tmp/b.txt"         # 直接將文本內容註入到遠程主機的文件中

6. fetch : Fetches a file from remote nodes 把遠程文件傳到管控機, 如各機的log日誌等, 與copy相反

dest        # 目的地址 (required) A directory to save the file into
src         # 源地址 (required) The file on the remote system to fetch,This `must be a file, not a directory
ansible web -m fetch -a src=/var/log/cron dest=/tmp    # 下載被控節點的文件,在管控機/tmp目錄下以每臺機器ip為名創建一個文件夾,並保留原來的目錄結構

技術分享圖片

7. file

path:  # (required) Path to the file being managed.
src:   # path of the file to link to (applies only to `state=link and `state=hard)
state: # directory  touch  link absent
owner: # chown
ansible db -m file -a path=/lzmly2  state=directory       #在遠程機器上創建文件夾
ansible db -m file -a path=/root/q.txt  state=touch       #用來在遠程機器上創建文件
ansible db -m file -a path=/tmp/f src=/etc/fstab state=link   #創建軟連接src是源地址,path是目標地址
ansible db -m file -a path=/tmp/f state=absent          #用來刪除文件或者文件夾
gruop /owner /mode

明天繼續補充

ansible 批量在遠程主機上執行命令