天天看點

ansible常用子產品介紹

file子產品

file子產品包含如下選項:

    force:需要在兩種情況下強制建立軟連結,一種是源檔案不存在但之後會建立的情況下;另一種是目标軟連結已存在,需要先取消之前的軟鍊,然後建立新的軟鍊,有兩個選項:yes|no

    group:定義檔案/目錄的屬組

    mode:定義檔案/目錄的權限

    owner:定義檔案/目錄的屬主

    path:必選項,定義檔案/目錄的路徑

    recurse:遞歸的設定檔案的屬性,隻對目錄有效

    src:要被連結的源檔案的路徑,隻應用于state=link的情況

    dest:被連結到的路徑,隻應用于state=link的情況

    state:

            directory:如果目錄不存在,建立目錄

            file:即使檔案不存在,也不會被建立

            link:建立軟連結

            hard:建立硬連結

            touch:如果檔案不存在,則會建立一個新的檔案,如果檔案或目錄已存在,則更新其最後修改時間

            absent:删除目錄、檔案或者取消連結檔案

1.建立file1檔案

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m file -a 'path=/tmp/file1 state=touch'

2.删除file1 檔案

ansible常用語句

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m file -a 'path=/tmp/file1 state=absent'

3.建立d1目錄

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m file -a 'path=/tmp/d1 state=directory owner=root group=root mode=777'

copy子產品

copy子產品包含如下選項:

    backup:在覆寫之前将原檔案備份,備份檔案包含時間資訊。有兩個選項:yes|no

    content:用于替代"src",可以直接設定指定檔案的值

    dest:必選項。要将源檔案複制到的遠端主機的絕對路徑,如果源檔案是一個目錄,那麼該路徑也必須是個目錄

    directory_mode:遞歸的設定目錄的權限,預設為系統預設權限

    force:如果目标主機包含該檔案,但内容不同,如果設定為yes,則強制覆寫,如果為no,則隻有當目标主機的目标位置不存在該檔案時,才複制。預設為yes

    others:所有的file子產品裡的選項都可以在這裡使用

    src:要複制到遠端主機的檔案在本地的位址,可以是絕對路徑,也可以是相對路徑。如果路徑是一個目錄,它将遞歸複制。在這種情況下,如果路徑使用"/"來結尾,則隻複制目錄裡的内容,如果沒有使用"/"來結尾,則包含目錄在内的整個内容全部複制,類似于rsync。

1.拷貝yum.log檔案到root下的file2

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m  copy -a 'src=/tmp/yum.log dest=/root/file2 mode=755 owner=root group=root'

2.cretes子產品執行的時候  會做一個判斷 前面的參數存在   後面的指令才不會繼續執行下去

[root@xuegod100 opt]# ansible -i hosts tomcatserver -a 'creates=/root/file2 ls /home'

command子產品

command子產品包含如下選項:

    creates:一個檔案名,當該檔案存在,則該指令不執行

    free_form:要執行的Linux指令

    chdir:在執行指令之前,先切換到該指定的目錄

    removes:一個檔案名,當該檔案不存在,則該選項不執行

    executable:切換shell來執行指令,該執行路徑必須是一個絕對路徑

1.檢視硬碟使用情況 裡面可以使用一些指令

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m command -a 'df -hT'

2.檢視檔案内容

[root@xuegod100 opt]# ansible -i hotst  tomcatserver -m command -a 'cat /var/www/helloword'

chdir子產品

.chdir  進入到指定的目錄然後進行操作

進入到tmp 使用者下面将passwd 檔案壓縮成 bbb.tar 檔案

[root@xuegod100 opt]# ansible -i hosts tomcatserver -a 'chdir=/tmp/ tar zcf bbb.tar.gz passwd'

shell 子產品

1.shell 子產品使用

與command子產品使用相似 但是shell支援管道

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m shell -a 'ps -ef | grep httpd'

2.raw子產品 與上面shell 相似

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m raw -a 'ps -ef | grep httpd'

server子產品

該子產品包含如下選項:

    arguments:給指令行提供一些選項

    enabled:是否開機啟動  yes|no

    name:必選項,服務名稱

    pattern:定義一個模式,如果通過status指令來檢視服務的狀态時,沒有響應,就會通過ps指令在程序中根據該模式進行查找,如果比對到,則認為該服務依然在運作

    runlevel:運作級别

    sleep:如果執行了restarted,在則stop和start之間沉睡幾秒鐘

    state:對目前服務執行啟動,停止、重新開機、重新加載等操作(started,stopped,restarted,reloaded)

1.啟動httpd服務 并且開機啟動

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m service  -a 'name=httpd state=started enabled=yes'

2.關閉httpd服務

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m service  -a 'name=httpd state=stop'

3.重新開機httpd服務 并且沉睡3秒鐘

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m service  -a 'name=httpd state=restarted sleep=3'

4.重新開機network 隻重新開機eth0 網卡

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m service  -a 'name=network state=restarted args=eth0'

corn子產品

包含如下選項:

    backup:對遠端主機上的原任務計劃内容修改之前做備份

    cron_file:如果指定該選項,則用該檔案替換遠端主機上的cron.d目錄下的使用者的任務計劃

    day:日(1-31,*,*/2,……)

    hour:小時(0-23,*,*/2,……)

    minute:分鐘(0-59,*,*/2,……)

    month:月(1-12,*,*/2,……)

    weekday:周(0-7,*,……)

    job:要執行的任務,依賴于state=present

    name:該任務的描述

    special_time:指定什麼時候執行,參數:reboot,yearly,annually,monthly,weekly,daily,hourly

    state:确認該任務計劃是建立還是删除

    user:以哪個使用者的身份執行

1.root使用者下 重新開機系統計劃任務   每天的兩點進行重新開機系統

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m cron -a 'name="reboot system" hour=2 user=root job="/sbin/reboot"'

2.删除計劃任務 使用state=sbsent

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m cron -a 'name="reboot system" hour=2 user=root job="/sbin/reboot" state=absent'

3.每三分鐘檢查一下home下的檔案

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m command -a "crontab -l"

4.special_time指定什麼時候執行

5.删除計劃任務(name 指定計劃任務名稱)

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m cron -a 'name="list dir"  state=absent'

yum 子產品

選項:

    config_file:yum的配置檔案

    disable_gpg_check:關閉gpg_check

    disablerepo:不啟用某個源

    enablerepo:啟用某個源

    list

    name:要進行操作的軟體包的名字,也可以傳遞一個url或者一個本地的rpm包的路徑

    state:狀态(present,absent,latest)

1.安裝mysql服務

[root@xuegod100 opt]# ansible -i hosts  tomcatserver  -m yum -a 'name=mysql state=installed'

2.解除安裝mysql服務

[root@xuegod100 opt]# ansible -i hosts  tomcatserver  -m yum -a 'name=mysql state=absent'

 user子產品

home:指定使用者的家目錄,需要與createhome配合使用

groups:指定使用者的屬組

uid:指定用的uid

password:指定使用者的密碼

name:指定使用者名

createhome:是否建立家目錄 yes|no

system:是否為系統使用者

remove:當state=absent時,remove=yes則表示連同家目錄一起删除,等價于userdel -r

state:是建立還是删除

shell:指定使用者的shell環境

1.建立使用者

[root@xuegod100 opt]# ansible -i hosts  tomcatserver  -m  command -a 'useradd -s /sbin/nologine -M user1'

2.使用user子產品建立子產品

createhome  是否建立家目錄 指定home 指定密碼  指定使用者 指定shell  狀态建立

[root@xuegod100 opt]# ansible -i hosts  tomcatserver  -m  user -a 'createhome=yes home=/home/user2 password=123456 name=user2 shell=/bin/bash state=present'

3.删除使用者

[root@xuegod100 opt]# ansible -i hosts  tomcatserver  -m  user -a 'remove=yes name=user2 state=absent'

synchronize子產品(同步檔案)

使用rsync同步檔案

archive #鏡像rsync存檔标志,支援遞歸、連結、perms、times、owner、group flags和- d,選項都為yes 預設該項為開啟。

owner、group、-D選項都為yes ,預設該項為開啟

checksum: 跳過檢測sum值,預設關閉

compress:是否開啟壓縮

copy_links:複制連結檔案,預設為no ,注意後面還有一個links參數

delete: 删除不存在的檔案,預設no

dest:目錄路徑

dest_port:預設目錄主機上的端口 ,預設是22,走的ssh協定

dirs:傳速目錄不進行遞歸,預設為no,即進行目錄遞歸

rsync_opts:rsync參數部分

set_remote_user:主要用于/etc/ansible/hosts中定義或預設使用的使用者與rsync使用的使用者不同的情況

mode: push或pull 子產品,push模的話,一般用于從本機向遠端主機上傳檔案,pull 模式用于從遠端主機上取檔案

1.同步檔案,将、tmp下的helloword檔案複制到、var、www 的目錄裡面 src 指定原位址  dest 目标位址

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m synchronize -a 'src=/tmp/helloword dest=/var/www/'

3.同步目錄

同步/var/log/下面所有的日志    mode 指定模式 rsync_path 指定遠端主機的sync指令 sync_opts 指定額外的rsync選項

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m synchronize -a 'src=/var/log/ dest=/var/www/ mode=push  rsync_path=/usr/bin/rsync rsync_opts="-avz,--exclude=.git"'

filesystem子產品

設定檔案的屬性

在塊裝置上建立檔案系統

1.建立一個4M 的檔案系統

[root@xuegod100 opt]# ansible -i hosts tomcatserver  -a 'dd if=/dev/zero of=/disk.img bs=4k count=1024'

2.把disk.img作為loop裝置進而可以進行分區格式化

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m command -a 'losetup /dev/loop0 /disk.img'

3.格式化硬碟分區

-F額外的參數 對照mkfs /dev/disk.img -F 強制格式化

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m filesystem -a 'dev=/disk.img fstype=ext4 opts=-F'

mount挂載子產品

    dump

    fstype:必選項,挂載檔案的類型

    name:必選項,挂載點

    opts:傳遞給mount指令的參數

    passno

    src:必選項,要挂載的檔案

    state:必選項

            present:隻處理fstab中的配置

            absent:删除挂載點

            mounted:自動建立挂載點并挂載之

            umounted:解除安裝

1.挂載裝置/dev/loop0 到aaa  state=mounted 挂載并且建立挂載點

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m mount -a 'fstype=ext4 name=/aaa src=/dev/loop0 state=mounted'

2.檢視硬碟挂載情況

3.在挂載的裝置上面建立檔案測試

[root@xuegod100 opt]# ansible -i hosts tomcatserver -m file -a 'path=/aaa/test state=touch'

本文轉自 Innocence_caosm 51CTO部落格,原文連結:http://blog.51cto.com/innocence/1961205,如需轉載請自行聯系原作者