ansbile和應用模塊
1呻右、ansible
ssh批量管理如何一鍵完成:一秒完成
一鍵完成的命令
1疚宇、ssh-keygen 非交互式創(chuàng)建密鑰對:
具體命令:ssh-keygen -f ~/.ssh/id_rsa P ' ' -q
參數(shù)講解:[-P old_passphrase] 密碼
[-f output_keyfile] 輸出的密鑰文件
[-q] 不輸出信息
[-t dsa] 指定密鑰類型
2淫茵、ssh-copy-id不需要提示yes/no分發(fā)秘鑰
具體命令:ssh-copy-id -f -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no 172.16.1.8
參數(shù)講解:
ssh-copy-id -f -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no root172.16.1.8
ssh-copy-id [-f] [-i [identity_file]] [-p port] [[-o <ssh -o options>] ...] [user@]hostname
說明:
-f: force mode 強制
[-i [identity_file]] 指定秘鑰文件
[[-o <ssh -o options>] ...] 指定ssh參數(shù)選項砸紊。
3咪奖、sshpass工具:指定密碼非人工交互分發(fā)密鑰
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
root@web02 ~]# sshpass -help
Usage: sshpass [-f|-d|-p|-e] [-hV] command parameters
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
sshpass [-f|-d|-p|-e] [-hV] command parameters
參數(shù)講解:
-p password Provide password as argument (security unwise) #指定用戶密碼操作
4.一鍵配置實踐
把web02作為分發(fā)服務器:
web02(8)-->m01(61)
web02(8)-->web01(7)
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.7
!/bin/bash
yum install sshpass -y
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
for ip in 7 61
do
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip
done
test
ssh 172.16.1.7 "ifconfig eth0"
ssh 172.16.1.61 "ifconfig eth0"
1辅鲸、ansible 介紹
ssh秘鑰認證+腳本批量管理成箫,特點:簡單展箱、實用,
但是看起來比較LOW蹬昌,需要人工寫腳本混驰,類似實時復制的inotify工具。
2013以前這種方式很普遍皂贩。
MySQL高可用 MHA集群栖榨,要求所有機器互相秘鑰認證。
大數(shù)據(jù)集群也需要
2明刷、批量管理工具歷史
SSH+腳本 CFEngine婴栽、Puppet、saltstack 辈末、ansible
08年以前 07-08年 10-13年 14-17(python開發(fā)) 16-(python開發(fā))
sina網(wǎng)
3愚争、為什么用ansible?
簡單挤聘、方便轰枝、容易學習、功能同樣強大组去。
ansible有配置文件狸膏,可以多線程直接實現(xiàn)。不需要寫腳本添怔,類似實時復制的sersync湾戳。
4、什么是ansible
ansible是一個用來遠程管理服務器的工具軟件广料。
ansilbe是一個用來批量部署遠程主機上服務的工具砾脑。這里“遠程主機(Remote Host)”是指任
何可以通過SSH登錄的主機,所以它既可以是遠程虛擬機或物理機艾杏,也可以是本地主機韧衣。
Ansible通過SSH協(xié)議實現(xiàn)管理節(jié)點與遠程節(jié)點之間的通信。理論上來說购桑,只要能通過SSH登錄到
遠程主機來完成的操作畅铭,都可以通過Ansible實現(xiàn)批量自動化操作。
涉及管理操作:復制文件勃蜘、安裝服務硕噩、服務啟動停止管理、配置管理等等缭贡。
5炉擅、為什么要用批量管理工具運維?
提高效率阳惹,百度幾萬臺服務器谍失。阿里幾十萬臺服務器
如何省錢?
SSD+ASTA 熱點存儲
6、ansible特點
ansible基于Python語言實現(xiàn)莹汤,有paramiko和PyYAML兩個關鍵模塊構建快鱼。
Shell和Python是Linux運維學員必會的兩門語言。
1)安裝部署過程特別簡單纲岭,學習曲線很平坦抹竹。
2)不需要單獨安裝客戶端,只是利用現(xiàn)有的SSHD服務(協(xié)議)即可荒勇。
3)不需要服務端(no servers)柒莉。
4)ansible playbook,采用yaml配置沽翔,提前編排自動化任務兢孝。
5)ansible功能模塊較多,對于自動化的場景支持豐富仅偎。
7跨蟹、ansible架構介紹
1、連接插件connectior plugins用于連接主機 用來連接被管理端
2橘沥、核心模塊 core modules 連接主機實現(xiàn)操作窗轩, 它依賴于具體的模塊來做具體的事情
3、自定義模塊 custom modules座咆,根據(jù)自己的需求編寫具體的模塊
4痢艺、插件 plugins仓洼,完成模塊功能的補充
5、劇本 playbooks堤舒,ansible的配置文件,將多個任務定義在劇本中色建,由ansible自動執(zhí)行
6、主機清單 inventor舌缤,定義ansible需要操作主機的范圍
最重要的一點是 ansible是模塊化的 它所有的操作都依賴于模塊
(懂Python可以二次開發(fā))
8箕戳、ansbile實踐環(huán)境
61(m01)====>31(nfs01)
61(m01) ====>41(backup)
9、安裝ansbile
m01管理機:
yum install epel-release -y
yum install ansible -y
如果有l(wèi)ibselinux-python就不執(zhí)行下面的命令了国撵。
rpm -qa |grep libselinux-python
yum install libselinux-python -y
其他所有機器:
rpm -qa |grep libselinux-python
yum install libselinux-python -y
10陵吸、主機列表配置
ssh列表實現(xiàn)方法:
for n in 7 8
do
echo 172.16.1.$n
done
/etc/ansible/hosts主機資產(chǎn)清單文件,用于定義被管理主機的認證信息介牙,
例如ssh登錄用戶名壮虫、密碼以及key相關信息。如何配置Inventory文件
1.主機支持主機名通配以及正則表達式耻瑟,例如web[1:3].oldboy.com代表三臺主機
2.主機支持基于非標準的ssh端口旨指,例如web1.oldboyedu.com:6666
3.主機支持指定變量,可對個別主機的特殊配置喳整,如登陸用戶\密碼
4.主機組支持指定變量[group_name:vars]谆构,同時支持嵌套組[game:children]
實踐開始:
cp /etc/ansible/hosts{,.ori}
cat >/etc/ansible/hosts<<EOF
[oldboy]
172.16.1.31
172.16.1.41
[oldgirl]
172.16.1.31
172.16.1.41
172.16.1.51
EOF
cat /etc/ansible/hosts
/etc/ansible/ansible.cfg #ansible的配置文件
11、實踐必會
直接執(zhí)行如下命令會報錯
ansible oldboy -m command -a "free -m"
基于SSH秘鑰認證的前提下:
如果沒有做SSH秘鑰認證框都,可以把用戶名密碼寫到
/etc/ansible/hosts
[oldboy_pass]
172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=123456
172.16.1.8 ansible_ssh_user=root ansible_ssh_pass=123456
ansible oldboy -m command -a "free -m" 要想成功搬素,先解決yes/no的問題。
1魏保、ssh連接一遍熬尺。
2、ssh -o 參數(shù)
上述命令就是sshpass的封裝
==========================================
ansible關閉ssh首次連接時yes/no提示
使用ssh連接時谓罗,可以使用-o參數(shù)將StrictHostKeyChecking設置為no粱哼,
避免使用ssh連接時避免首次連接時讓輸入yes/no部分的提示。
方法1:修改 /etc/ansible/ansible.cfg中的374行
369 [ssh_connection]
370
371 # ssh arguments to use
372 # Leaving off ControlPersist will result in poor performance, so use
373 # paramiko on older platforms rather than removing it, -C controls compression use
374 #ssh_args = -C -o ControlMaster=auto -o ControlPersist=60s
374行改為:
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no
方法2:修改 /etc/ansible/ansible.cfg中的71行
70 # uncomment this to disable SSH key host checking
71 #host_key_checking = False
71行的注釋取消:host_key_checking = False
==========================================
[root@m01 /server/scripts]# > ~/.ssh/known_hosts
[root@m01 /server/scripts]# ssh 172.16.1.31
The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.
ECDSA key fingerprint is SHA256:qZSBkrmOv7xO/63qOU1uLXkPyNVHdkqvrNAcAmXqNEk.
ECDSA key fingerprint is MD5:23:d0:cb:a9:f4:7c:0b:eb:2d:07:00:e1:a3:12:d8:33.
Are you sure you want to continue connecting (yes/no)? ^C
[root@m01 /server/scripts]# ansible oldboy -m command -a "free -m"
172.16.1.31 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 88 592 13 291 708
Swap: 767 0 767
172.16.1.41 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 89 572 13 310 706
Swap: 767 0 767
目標:ansible是不是需要免秘鑰認證(ssh免秘鑰認證)檩咱? yes
[root@m01 ~]# cat /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.7
還原ansible.cfg配置揭措,重啟服務器
[root@m01 ~]# ansible oldboy -m command -a "free -m"
The authenticity of host '172.16.1.7 (172.16.1.7)' can't be established.
ECDSA key fingerprint is SHA256:qZSBkrmOv7xO/63qOU1uLXkPyNVHdkqvrNAcAmXqNEk.
ECDSA key fingerprint is MD5:23:d0:cb:a9:f4:7c:0b:eb:2d:07:00:e1:a3:12:d8:33.
Are you sure you want to continue connecting (yes/no)? The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.
ECDSA key fingerprint is SHA256:qZSBkrmOv7xO/63qOU1uLXkPyNVHdkqvrNAcAmXqNEk.
ECDSA key fingerprint is MD5:23:d0:cb:a9:f4:7c:0b:eb:2d:07:00:e1:a3:12:d8:33.
Are you sure you want to continue connecting (yes/no)?
解決yes/no不需要輸入問題:
修改ansible.cfg 374行:
ssh_args = -o ControlMaster=auto -o ControlPersist=60s -o StrictHostKeyChecking=no
在執(zhí)行報錯:
[root@m01 ~]# ansible oldboy -m command -a "free -m"
172.16.1.7 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added '172.16.1.7' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).",
"unreachable": true
}
172.16.1.31 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added '172.16.1.31' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
"unreachable": true
}
解決公鑰問題:
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
for ip in 7
do
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip
done
test
ssh 172.16.1.7 "ifconfig eth0"
執(zhí)行ansible命令:
[root@m01 ~]# ansible oldboy -m command -a "free -m"
172.16.1.31 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
"unreachable": true
}
172.16.1.7 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 69 807 7 95 771
Swap: 767 0 767
for ip in 31
do
sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip
done
test
ssh 172.16.1.31 "ifconfig eth0"
[root@m01 ~]# ansible oldboy -m command -a "free -m"
172.16.1.7 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 69 807 7 95 771
Swap: 767 0 767
172.16.1.31 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 71 804 7 96 768
Swap: 767 0 767
驗證:刪除公鑰,是不是不可以了刻蚯?
重啟后绊含,不行,重啟前可以
[root@m01 ~]# ansible oldboy -m command -a "free -m"
172.16.1.7 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Warning: Permanently added '172.16.1.7' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,password).",
"unreachable": true
}
172.16.1.31 | UNREACHABLE! => {
"changed": false,
"msg": "Failed to connect to the host via ssh: Could not create directory '/root/.ssh'.\r\nWarning: Permanently added '172.16.1.31' (ECDSA) to the list of known hosts.\r\nPermission denied (publickey,gssapi-keyex,gssapi-with-mic,password).",
"unreachable": true
}
修改Host增加用戶和密碼:
[root@m01 ~]# cat /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.7
[oldboy_pass]
172.16.1.31 ansible_ssh_user=root ansible_ssh_pass=123456
172.16.1.7 ansible_ssh_user=root ansible_ssh_pass=123456
結果:
[root@m01 ~]# ansible oldboy_pass -m command -a "free -m"
172.16.1.31 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 79 791 7 101 758
Swap: 767 0 767
172.16.1.7 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 972 69 806 7 95 771
Swap: 767 0 767
結論:使用SSH連接:
密碼認證 host里主機后面加密碼 Paramiko模塊 重點:
秘鑰認證:提前發(fā)公鑰炊汹,才能用ansible. SSHPASS工具
http://docs.ansible.com/ansible/latest/intro_inventory.html#list-of-behavioral-inventory-parameters
特殊端口:
[oldboy]
172.16.1.7 ansible_port=52113 ansible_user=root ansible_ssh_pass=123456
172.16.1.8 ansible_port=52113 ansible_user=root ansible_ssh_pass=123456
12.基于SSH秘鑰認證的實踐
一鍵創(chuàng)建及分發(fā)秘鑰:
!/bin/bash
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
for ip in 7 8 41 31
do
sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" 172.16.1.$ip
done
[root@m01 ~]# cat /etc/ansible/hosts
[oldboy]
172.16.1.31
172.16.1.41
172.16.1.7
[root@m01 ~]# ansible oldboy -m command -a "free -m" #和前面sh cmd.sh "free -m"
11.ansible命令參數(shù)
-m MODULE_NAME, 模塊名字躬充,默認command
-a MODULE_ARGS, 模塊參數(shù)
-f FORKS 并發(fā)進程數(shù),默認5個。
-i INVENTORY(default=/etc/ansible/hosts)指定主機列表文件
===================
ansible
ansible-doc
ansible-playbook
ansible-galaxy
===================
12.ansible模塊查看和幫助*****
查找模塊
ansible-doc -l #模塊就Linux命令了充甚。
查看某個模塊的具體參數(shù)幫助
ansible-doc -s command #Linux命令參數(shù)
12.1 command模塊 *****
1)功能說明:
command Executes a command on a remote node
功能說明:執(zhí)行一個命令在遠程節(jié)點上
操作實踐:
ansible oldboy -m command -a "free -m"
ansible oldboy -m command -a "df -h"
ansible oldboy -m command -a "ls /root"
ansible oldboy -m command -a "cat redhat-release"
ansible oldboy -m command -a "cat /etc/redhat-release"
最通用的功能以政。
[root@m01 ~]# ansible oldboy -m command -a "cat /etc/redhat-release"
172.16.1.7 | CHANGED | rc=0 >>
CentOS Linux release 7.6.1810 (Core)
172.16.1.31 | CHANGED | rc=0 >>
CentOS Linux release 7.6.1810 (Core)
172.16.1.41 | CHANGED | rc=0 >>
CentOS Linux release 7.6.1810 (Core)
[root@m01 ~]# cat /server/scripts/cmd.sh
for n in 31 41
do
echo "=====172.16.1.$n======"
ssh 172.16.1.1"
done
[root@m01 ~]# sh /server/scripts/cmd.sh "cat /etc/redhat-release"
=====172.16.1.31======
CentOS Linux release 7.6.1810 (Core)
=====172.16.1.41======
CentOS Linux release 7.6.1810 (Core)
特殊:不支持的東西,例如 > < | &等 $HOME津坑,替代方案用shell模塊
ansible oldboy -m shell -a "ps -ef|grep ssh"
ansible oldboy -m shell -a "echo oldboy >/tmp/a.log"
2)常用參數(shù)說明及實踐
[root@m01 ~]# ansible-doc -s command
- name: Executes a command on a remote node
command:
argv: # Allows the user to provide the command as a list vs. a string. Only the
string or the list form can be provided, not
both. One or the other must be provided.
chdir: # Change into this directory before running the command.
creates: # A filename or (since 2.0) glob pattern. If it already exists, this step
won't be run.
free_form: # (required) The command module takes a free form command to run. There is no
parameter actually named 'free form'. See the
examples!
removes: # A filename or (since 2.0) glob pattern. If it already exists, this step will be run.
stdin: # Set the stdin of the command directly to the specified value.
warn: # If command_warnings are on in ansible.cfg, do not warn about this particular
line if set to `no'.
參數(shù):chdir=/tmp配置相當于cd /tmp
[root@m01 ~]# ansible oldboy -m command -a "pwd chdir=/etc"
ansible oldboy -m shell -a "cd /etc/;pwd"
參數(shù):creates=/etc 相當于條件測試 [ -e /etc ]||pwd 和下面removes相反
[root@m01 ~]# ansible oldboy -m command -a "pwd creates=/etc"
參數(shù):removes=/root 相當于條件測試 [ -e /root ]&&ls /root
ansible oldboy -m command -a "ls /root removes=/root"
ansible oldboy -m shell -a "[ -d /etc ]||pwd"
[root@m01 ~]# ansible oldboy -m command -a "cat /etc/hosts removes=/etc/hosts"
參數(shù):warn=False 忽略警告
[root@m01 ~]# ansible oldboy -m command -a "chmod 000 /etc/hosts warn=False"
更多官方鏈接:http://docs.ansible.com/ansible/latest/command_module.html或ansible-doc -s command
=====================================================================================
項目實踐:
rsync服務器端:31妙蔗、41
rsync客戶端:7、8
實現(xiàn):從7推送文件到31疆瑰,從8推送文件到41.
shell模塊:實現(xiàn)上面
12.2 script模塊功能說明:
功能說明:在遠程節(jié)點上運行本地腳本
官方鏈接:http://docs.ansible.com/ansible/latest/script_module.html
遠端可以沒有腳本,本地有就行:
[root@m01 /server/scripts]# cat setup.sh
pwd
ls /root
for n in {1..100}
do
echo $n >>/tmp/oldboy.log
done
執(zhí)行:
ansible oldboy -m script -a "/server/scripts/setup.sh"
項目實踐作業(yè):
寫好rsync一鍵客戶端配置昙啄,一鍵服務端配置穆役。
12.3 copy模塊功能說明:
copy模塊功能說明:
功能說明:復制文件到遠程主機
官方鏈接:http://docs.ansible.com/ansible/latest/copy_module.html
ansible oldboy -m copy -a "src=/server dest=/ mode=ugo+x group=root owner=root"
ansible oldboy -m copy -a "src=/server/scripts/setup dest=/server/scripts mode=ugo+x group=root owner=root backup=yes"
12.4 shell模塊功能說明:
功能說明:執(zhí)行一個命令在遠程節(jié)點上
官方鏈接:http://docs.ansible.com/ansible/latest/shell_module.html
ansible oldboy -m shell -a "free -m|grep buffer"
遠程執(zhí)行腳本:腳本必須在遠端存在
ansible oldboy -m shell -a "/bin/bash /server/scripts/setup.sh"
項目實踐作業(yè):
1、寫好rsync一鍵客戶端配置梳凛,一鍵服務端配置耿币。
2、寫好nfs一鍵服務端端配置韧拒,一鍵客戶端掛載淹接,并且加到自啟動文件里(/etc/rc.local,/etc/fstab)。
步驟:
1叛溢、遠端命令行非交互實現(xiàn)
echo 123456|passwd --stdin oldboy
2塑悼、所有步驟放在腳本里實現(xiàn)。
3楷掉、管理機上遠程執(zhí)行厢蒜。
ssh /bin/sh /server/scripts/ins.sh
知識----能力-----價值-----金錢
學習方法:
學習能力
解決思路
任何問題有方法。
12.5 file模塊功能說明:
功能說明:設置文件屬性
官方鏈接:http://docs.ansible.com/ansible/latest/file_module.html
================================================================
替代方案:
ansible oldboy -m command -a "chmod 777 /etc/hosts warn=false"
ansible oldboy -m command -a "chmod 644 /etc/hosts warn=false"
ansible oldboy -m command -a "chown oldboy /etc/hosts warn=false"
ansible oldboy -m command -a "chown root /etc/hosts warn=false"
創(chuàng)建目錄:mkdir /tmp/oldboy_dir
ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory"
遞歸設置權限:
ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory mode=644 recurse=yes"
創(chuàng)建文件:touch /tmp/oldboy_file
ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch"
刪除文件:rm -f /tmp/oldboy_file
ansible oldboy -m file -a "dest=/tmp/oldboy_file state=absent"
創(chuàng)建鏈接文件:ln -s /etc/hosts /tmp/link_file
ansible oldboy -m file -a "src=/etc/hosts dest=/tmp/link_file state=link"
ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=000"
ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=ugo=rwx"
12.5 yum模塊功能說明:
功能說明:yum包管理模塊
官方鏈接:http://docs.ansible.com/ansible/latest/yum_module.html
ansible oldboy -m yum -a "name=nginx state=installed"
[root@nfs01 oldboy_dir]# rpm -qa nginx
nginx-1.10.2-1.el6.x86_64
不要用yum卸載烹植,用rpm -e卸載斑鸦。
12.6 service模塊功能說明:
功能說明:啟動停止服務
官方鏈接:http://docs.ansible.com/ansible/latest/service_module.html
相當于
service crond stop|/etc/init.d/crond stop
chkconfig crond off
ansible oldboy -m service -a "name=crond state=stop enabled=no"
相當于/etc/init.d/crond start
chkconfig crond on
ansible oldboy -m service -a "name=crond state=started enabled=yes"
ansible oldboy -m command -a "name=crond state=started enabled=yes"
有選擇才叫有能力。
足球場上草雕,讓拿球隊員有選擇巷屿,就容易進球。
不讓對方有選擇墩虹,就得人盯人嘱巾。
12.7 cron模塊功能說明:
功能說明:管理定時任務條目信息模塊
官方鏈接:http://docs.ansible.com/ansible/latest/cron_module.html
定時任務格式:
- CMD
創(chuàng)建定時任務:
ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"
結果:
Ansible: sync time
00 00 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
添加如下定時任務:
05 03 * * * /bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1
命令如下:
ansible oldboy -m cron -a "name='backup data' minute=05 hour=03 job='/bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1'"
結果:
Ansible: backup data
05 03 * * * /bin/sh /server/scripts/backup.sh /server/scripts/list >/dev/null 2>&1
刪除定時任務:
ansible oldboy -m cron -a "name='backup data' state=absent"
劇本篇
12.8 playbook
把所有ansible命令放在文件里執(zhí)行就是playbook筒愚。
playbook替代方案1:
[root@m01 ~]# cat ansible.sh
ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch"
ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch owner=oldboy group=oldboy mode=ugo=rwx"
ansible oldboy -m yum -a "name=nginx state=installed"
ansible oldboy -m service -a "name=crond state=started enabled=yes"
ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'"
playbook替代方案2:
[root@m01 ~]# cat ~/set.sh
touch /tmp/oldboy_file
chown oldboy.oldboy /tmp/oldboy_file
yum install nginx -y
/etc/init.d/crond start
chkconfig cornd on
echo '#sync time oldboy' >>/var/spool/cron/root
echo '00 00 * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1' >>/var/spool/cron/root
執(zhí)行:
ansible oldboy -m script -a "~/set.sh"
ansible劇本編寫格式說明
ansible劇本遵循PYyaml語法規(guī)則進行編寫禀梳,ymal文件基本編寫規(guī)則如下說明:
規(guī)則一:縮進
yaml使用一個固定的縮進風格表示數(shù)據(jù)層結構關系,需要每個縮進級別由兩個空格組成所森。切記一定不能使用tab鍵進行縮進尖坤。
規(guī)則二:冒號
每個冒號后面一定要有一個空格(以冒號結尾不需要空格稳懒,表示文件路徑的模版可以不需要空格)
規(guī)則三:短橫線
想要表示列表項,使用一個短橫杠加一個空格。多個項使用同樣的縮進級別作為同一個列表的一部分
- name: This command will change the working directory to somedir/ and will only run when /path/to/database doesn't exist.
command: /usr/bin/make_database.sh arg1 arg2
args:
chdir: somedir/
creates: /path/to/database
[root@m01 ~]# cat /etc/ansible/a.yml
- hosts: oldboy
tasks:
- shell: echo hello oldboy linux. >/tmp/a.log
ansible oldboy -m command -a "echo hello oldboy linux."
=========寫成劇本
- hosts: oldboy
task:
- command: echo hello oldboy linux.
=========寫成劇本
ansible oldboy -m command -a "pwd chdir=/etc"
- hosts: oldboy
task:
- command: echo hello oldboy linux.