1,查看使用幫助
ansible-doc -l ? ? ?? =============>相當(dāng)于Linux里面的命令
查模塊的參數(shù)
ansible-doc -s ==========>>接模塊名查看模塊參數(shù)信息
===================>查模塊信息就相當(dāng)于查L(zhǎng)inux里面的命令,模塊就是ansible里的命令
2,command模塊知識(shí)
=======================================================
參數(shù) ? chdir=/tmp? 配置相當(dāng)于cd /tmp
[root@BOSS ~]# ansible oldboy -m command -a "pwd chdir=/etc"
172.16.1.31 | CHANGED | rc=0 >>
/etc
172.16.1.41 | CHANGED | rc=0 >>
/etc
=======================================================
參數(shù) ? creates=/etc? 相當(dāng)于條件測(cè)試
[root@BOSS ~]# ansible oldboy -m command -a "pwd creates=/etc"
172.16.1.31 | SUCCESS | rc=0 >>
skipped, since /etc exists
172.16.1.41 | SUCCESS | rc=0 >>
skipped, since /etc exists
==============================================================
參數(shù) ? removes=/root ? 相當(dāng)于條件測(cè)試
[root@BOSS ~]# ansible oldboy -m command -a "ls /root removes=/etc"
172.16.1.41 | CHANGED | rc=0 >>
(date +%F %T).tar.gz
(date +%F).tar.gz
===================================>>省略若干
參數(shù) ? warn=False ? 忽略警告
[root@BOSS ~]# ansible oldboy -m command -a "chmod 000 /etc/hosts"
[WARNING]: Consider using the file module with mode rather than running 'chmod'.? If you need to use command because file is insufficient you can add 'warn: false' to
this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
172.16.1.41 | CHANGED | rc=0 >>
172.16.1.31 | CHANGED | rc=0 >> ?? ======================>>會(huì)出現(xiàn)警告
[root@BOSS ~]# ansible oldboy -m command -a "chmod 000 /etc/hosts warn=False"
172.16.1.41 | CHANGED | rc=0 >>
172.16.1.31 | CHANGED | rc=0 >>===============>>>不出現(xiàn)警告
[root@BOSS ~]# ansible oldboy -m command -a "ls -la /etc/hosts"
172.16.1.41 | CHANGED | rc=0 >>
----------. 1 root root 332 Apr 12 15:08 /etc/hosts
172.16.1.31 | CHANGED | rc=0 >>
----------. 1 root root 332 Apr 12 15:08 /etc/hosts ? ? ========>>>記得更改回來,不要太皮
3,shell模塊功能介紹
shell? 執(zhí)行一個(gè)命令在遠(yuǎn)程節(jié)點(diǎn)上
=================================================================
[root@BOSS ~]# ansible oldboy -m shell -a "echo oldboy >/tmp/a.txt"
172.16.1.31 | CHANGED | rc=0 >>
172.16.1.41 | CHANGED | rc=0 >>
[root@BOSS ~]# ansible oldboy -m shell -a "cat /tmp/a.txt"
172.16.1.41 | CHANGED | rc=0 >>
oldboy
172.16.1.31 | CHANGED | rc=0 >>
oldboy
========================================>>>實(shí)踐增加文本
[root@BOSS ~]# ansible oldboy -m shell -a "sh -x /server/scripts/bak.sh"
172.16.1.41 | FAILED | rc=127 >>
sh: /server/scripts/bak.sh: No such file or directorynon-zero return code
172.16.1.31 | CHANGED | rc=0 >>
++ date +%F
+ /bin/cp /etc/hosts /opt/hosts_2019-04-24
===========================================>>>遠(yuǎn)程執(zhí)行腳本條件是必須存在
[root@NFS /server/scripts]# scp bak.sh 172.16.1.41:/server/scripts
------------------------------------------------------->>>使用scp拷貝
[root@BOSS ~]# ansible oldboy -m shell -a "sh -x /server/scripts/bak.sh"
172.16.1.31 | CHANGED | rc=0 >>
++ date +%F
+ /bin/cp /etc/hosts /opt/hosts_2019-04-24
172.16.1.41 | CHANGED | rc=0 >>
++ date +%F
+ /bin/cp /etc/hosts /opt/hosts_2019-04-24
=============================================>>>在執(zhí)行就成功了
===============================================================
4,copy模塊功能說明
復(fù)制文件到遠(yuǎn)程主機(jī)
常見參數(shù)說明
src ?? 拷貝什么東西的原路徑
dest ? 往哪兒拷貝
owner ? 屬組
group ? 屬組
mode ? 權(quán)限
backup ? 需不需要操作前備份
content ? ?
force ??
-----------------------------------------------------------------------------------------------------------
實(shí)踐:
[root@BOSS ~]# ansible oldboy -m copy -a "src=/etc/hosts dest=/opt mode=ugo+x"
172.16.1.31 | CHANGED => {
? ? "changed": true,
? ? "checksum": "e52c528913b5c22d388cc2a18f6943641c8442c3",?
------------------------------------------------------>>>省略若干
172.16.1.41 | CHANGED => {
? ? "changed": true,
? ? "checksum": "e52c528913b5c22d388cc2a18f6943641c8442c3",?
--------------------------------------------------------->>>省略若干
查看:
[root@BOSS ~]# ansible oldboy -m shell -a "ls -ld /opt/hosts"
172.16.1.31 | CHANGED | rc=0 >>
-r-x--x--x 1 root root 332 Apr 24 10:34 /opt/hosts
172.16.1.41 | CHANGED | rc=0 >>
-r-x--x--x 1 root root 332 Apr 24 10:34 /opt/hosts
---------------------------------------------------------------------------->>>拷貝成功
5递沪,script模塊功能說明
遠(yuǎn)程節(jié)點(diǎn)上運(yùn)行本地腳本
實(shí)踐:
[root@BOSS ~]# vim ff.sh
? 1 echo 666 ------------------------->>>創(chuàng)建腳本
執(zhí)行腳本:
[root@BOSS ~]# ansible oldboy -m script -a "ff.sh"
172.16.1.31 | CHANGED => {
--------------------------------->省略若干
"stdout_lines": [
? ? ? ? "666"
172.16.1.41 | CHANGED => {
----------------------------------->省略若干
"stdout_lines": [
? ? ? ? "666"
======================================>>>執(zhí)行成功
6勇蝙,file模塊功能說明
主要用于創(chuàng)建文件或目錄數(shù)據(jù)沫勿,以及多存在的文件或目錄權(quán)限屬性進(jìn)行修改管理
src ? ? 源
path ? 路徑
owner ? 屬主
group ? ? 屬主
mode ?? 權(quán)限
state ?? 狀態(tài)
-------------------------------------------------------------------------------------------------------------------------
實(shí)踐:
創(chuàng)建目錄:
[root@BOSS ~]# ansible oldboy -m file -a "dest=/tmp/oldboy_dir state=directory"?
查看:
[root@BOSS ~]# ansible oldboy -m command -a "ls -l /tmp/"
172.16.1.41 | CHANGED | rc=0 >>
drwxr-xr-x 2 root dengli 6 Apr 25 00:27 oldboy_dir
172.16.1.31 | CHANGED | rc=0 >>
drwxr-xr-x 2 root dengli 6 Apr 25 00:27 oldboy_dir
------------------------------------------------------------------------------------->>>創(chuàng)建成功
用command實(shí)現(xiàn):
[root@BOSS ~]# ansible oldboy -m command -a "mkdir -p /tmp/oldboy_dir1 warn=false"
172.16.1.31 | CHANGED | rc=0 >>
172.16.1.41 | CHANGED | rc=0 >>
查看:
[root@BOSS ~]# ansible oldboy -m command -a "ls -l /tmp/"
172.16.1.41 | CHANGED | rc=0 >>
drwxr-xr-x 2 root dengli 6 Apr 25 00:35 oldboy_dir1
172.16.1.31 | CHANGED | rc=0 >>
drwxr-xr-x 2 root dengli 6 Apr 25 00:35 oldboy_dir1
--------------------------------------------------------------------------------->>>實(shí)現(xiàn)成功
創(chuàng)建文件:
[root@BOSS ~]# ansible oldboy -m file -a "dest=/tmp/oldboy_file state=touch"?
-------------------------------------------------------------------------------------------------------------------
刪除文件:
[root@BOSS ~]# ansible oldboy -m file -a "dest=/tmp/oldboy_file state=absent"
-----------------------------------------------------------------------------------------------------------------------
創(chuàng)建鏈接文件:
[root@BOSS ~]# ansible oldboy -m file -a "src=/etc/hosts dest=/tmp/link_file state=link"
-----------------------------------------------------------------------------------------------------------------------
7,yum模塊
示例:
[root@BOSS ~]# ansible oldboy -m yum -a "name=inotify-tools state=installed"
------------------------------------------------------------>>>安裝
-------------------------------------------------------------------------------------------------------------------------
8味混,systemd模塊
9产雹,cron模塊
參數(shù)
minute ? 運(yùn)行job分鐘信息
hour ? ? 運(yùn)行job小時(shí)時(shí)間信息
day ? ? 運(yùn)行job日期時(shí)間信息
month ? 運(yùn)行job月份時(shí)間信息
weekday ? 運(yùn)行job周期時(shí)間信息
job ? ? 具體的任務(wù)
name ? ? 名字,描述信息
state ? ?? 狀態(tài)
disabled ? ? ?? 靜止
---------------------------------------------------------------------------------------------------------------------
創(chuàng)建定時(shí)任務(wù):
[root@BOSS ~]# ansible oldboy -m cron -a "name='sync time' minute=00 hour=00 job='/usr/sbin/ntpdata time.nist.gov >/dev/null 2>&1'"
------------------------------------------------------------------>>>命令行執(zhí)行成功
查看:
[root@BOSS ~]# ansible oldboy -m command -a "crontab -l"
172.16.1.41 | CHANGED | rc=0 >>
#Ansible: sync time
00 00 * * * /usr/sbin/ntpdata time.nist.gov >/dev/null 2>&1
172.16.1.31 | CHANGED | rc=0 >>
#Ansible: sync time
00 00 * * * /usr/sbin/ntpdata time.nist.gov >/dev/null 2>&1
----------------------------------------------------------------------------------------------->>>成功