自動化運維繫列之Ansible命令應用基礎(模塊的應用)

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

模塊簡介

Ansible可以使用命令行方式進行自動化管理,基本語法如下:

ansible <host-pattern> [-m module_name] [-a args]

<host-pattern> 對哪些主機生效

[-m module_name] 需要使用的模塊

[-a args] 模塊特有的參數,這裏在使用時需加單引號哦!

Ansible的命令行管理工具都是由一系列模塊、參數所支持的,可以在命令行後加上-h或--help獲取幫助。如使用ansible-doc工具可以通過ansible-doc -h查看其幫助信息。

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

ansible自帶了很多模塊,能夠下發執行Ansible的各種管理任務。下面介紹下Ansible常用的一些核心模塊:

  1. command模塊

Ansible管理工具使用-m來指定使用模塊,默認使用command模塊,即不指定-m選項時會默認使用command模塊來執行管理任務。

# 例如:
# ansible webserver -m command -a 'date'
# ansible mysql -a 'date'     #不指定-m 選項時,默認使用command模塊

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

  1. cron模塊

Ansible中的cron模塊用於定義任務計劃。其中有兩種狀態(state):present表示添加(省略時默認使用添加);absent表示移除。

  • 可以先使用-h獲取幫助信息查看cron模塊的幫助

ansible-doc -s cron #查看cron模塊的幫助信息

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

  • 使用cron模塊在webserver組的主機上創建任務計劃
# ansible webserver -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job"'     #添加一個計劃性任務

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

  • 這裏可以查看下我們添加的任務
# ansible webserver -a 'crontab -l'    #查看計劃性任務

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

  • 移除計劃性任務
# ansible webserver -m cron -a 'minute="*/10" job="/bin/echo hello" name="test cron job" state=absent'    #absent表示移除

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

  1. user模塊

Ansible中的user模塊用於創建新用戶和更改、刪除已存在的用戶。其中name選項用來指明創建的用戶名稱。

user模塊是請求的是useradd, userdel, usermod三個指令

  • 創建用戶

ansible webserver -m user -a 'name="zyc"' #創建用戶zyc

ansible webserver -m command -a 'tail /etc/passwd' #查看webserver主機上的用戶列表

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

  • 刪除用戶

ansible webserver -m user -a 'name="zyc" state=absent' #刪除用戶

ansible webserver -m command -a 'tail /etc/passwd' #再次查看用戶列表

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

  1. group模塊

Ansible中的group模塊用於對用戶組進行管理。

  • 創建mysql組,將mysql用戶添加到mysql組。

ansible-doc -s group

ansible mysql -m group -a 'name=mysql gid=306 system=yes' #創建mysql組 將mysql用戶添加進去

ansible mysql -a 'tail /etc/group'

ansible mysql -m user -a 'name=mysql uid=306 system=yes group=mysql'

ansible mysql -a 'tail /etc/passwd'

ansible mysql -a 'id mysql'

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

5.copy模塊

Ansible中的copy模塊用於實現文件複製和批量下發文件。其中src用來定義本地源文件路徑,使用dest定義被管理主機文件路徑,使用content是通過指定信息內容來生成目標文件。

ansible-doc -s copy

ansible webserver -m copy -a 'src=/etc/fstab dest=/opt/fstab.back owner=root mode=640'
#(屬主root 權限640)

ansible webserver -a 'ls -l /opt'

ansible webserver -a 'cat /opt/fstab.back'

ansible webserver -m copy -a 'content="hello heihei!"dest=/opt/fstab.back' #將hello heihei!寫入/opt/fstab.back

ansible webserver -a 'cat /opt/fstab.back'

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

6.file模塊

Ansible中file模塊用來設置文件屬性。(path指定文件路徑,src指定源文件路徑,name或者dest替換創建文件的符號鏈接)

ansible-doc -s file

ansible webserver -m user -a 'name=mysql system=yes'

ansible webserver -m group -a 'name=mysql system=yes'

ansible webserver -m file -a 'owner=mysql group=mysql mode=644 path=/opt/fstab.back' #修改文件的屬主屬組權限等

ansible webserver -m file -a 'path=/opt/fstab.link src=/opt/fstab.back state=link' #設置/opt/fstab.link爲/opt/fstab.back的鏈接文件

ansible webserver -m file -a "path=/opt/fstab.back state=absent" #刪除一個文件

ansible webserver -m file -a "path=/opt/school state=touch" #創建一個文件

7.ping模塊

Ansible中ping模塊用來測試指定主機的連通性

ansible all -m ping #測試所有主機的連通性

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

8.shell模塊

Ansible中的shell模塊可在被管理主機上運行命令,並支持像管道符號等功能的複雜命令。

  • 創建用戶後使用免交互模式給用戶設置密碼。

ansible mysql -m user -a 'name=zyc' #創建用戶

ansible mysql -m shell -a 'echo 123123|passwd --stdin zyc' #給用戶設置密碼

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

9.yum模塊

Ansible中的yum模塊負責在被管理主機上安裝與卸載軟件包,但是需要提前在每個節點配置自己的yum倉庫。

name指定需要安裝的軟件包,需要帶上軟件包的版本號,否則默認安裝最新版

state指定安裝軟件包的狀態,present、latest表示安裝;absent表示卸載

ansible webserver -m yum -a 'name=http' #安裝http軟件

ansible webserver -m yum -a 'name=http state=absent' #卸載http軟件

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

10.service模塊

Ansible中使用service模塊來控制管理服務的運行狀態。

enabled表示是否開機自啓動,取值爲true和false

name定義服務名稱

state指定服務狀態,取值爲:started(開啓)、stoped(停止)、restarted(重啓)

# ansible webserver -m yum -a 'name=http'   #安裝http軟件
# ansible webserver -m service -a 'enabled=true name=httpd state=started'
# ansible webserver -a 'systemctl status httpd.service'

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

11.script模塊

Ansible中的script模塊可以將本地腳本複製到被管理主機上運行。(注意:腳本路徑需要使用相對路徑)

# ansible-doc -s script
# vim abc.sh    #在本地編寫一個abc的腳本
    #!/bin/bash
    echo "hello ansible from script"> /opt/script.txt
# chmod +x abc.sh    #賦予執行權限
# ansible webserver -m script -a 'abc.sh'  #將腳本複製到被管理主機上運行
# ansible webserver -a 'cat /opt/script.txt'    #查看腳本信息

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

12.setup模塊

Ansible中的setup模塊主要收集、查看被管理主機的facts(facts是Ansible採集被管理主機設備信息的一個功能)。

# ansible webserver -m setup  #收集信息

自動化運維繫列之Ansible命令應用基礎(模塊的應用)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章