ansible-doc 介紹


查看ansible所支持的模塊
 ansible-doc -l   #列出所有ansible支持的模塊,重要,請自行記住
acl                  Sets and retrieves file ACL information.
...
at                   Schedule the execution of a command or scripts via the at co
cron                 Manage cron.d and crontab entries.
command              Executes a command on a remote node                         
copy                 Copies files to remote locations. 
... 
查看指定模塊的參數

ansible-doc -s copy 重要,請自行記住
      src=          #源文件                   
      force=        #是否覆蓋         
      dest=         #目標文件 
ansible all -m copy -a "src=/etc/fstab dest=/tmp"                   
  1: ansible-doc -s cron
  2: - name: Manage cron.d and crontab entries.
  3:   action: cron
  4:       name= 
  5:       hour=
  6:       job=          
  7:       cron_file=           
  8:       reboot=               
  9:       month=             
 10:       state=               
 11:       special_time=        
 12:       user=               
 13:       backup=            
 14:       day=                
 15:       minute=             
 16:       weekday=        
 17: ansible all -m cron -a 'name="custom job" minute=15 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 172.16.0.1"'
 18: ansible all -a 'crontab -l'
ansible的yum使用的

  1: ansible-doc -s yum
  2:       action: yum
  3:       state=               #安裝還是卸載,present安裝,absent卸載      
  4:       disablerepo=         #禁用某個yum源     
  5:       name=                #要安裝的程序包名     
  6:       enablerepo=          #要啟用的yum源    
  7:       list=                #列表,主要是playbooks中使用的
  8:       disable_gpg_check=   #
  9:       conf_file=           #執行yum時使用其他服務器的文件
 10: ansible all -m yum -a "state=present name=corosync"
ansible的服務使用

  1: ansible-doc -s service       #設置某一應用程序開啟自動啟動的
  2: - name: Manage services.
  3:   action: service
  4:       state=                 # 當前的服務是啟動的還是關閉的
  5:       sleep=                 #
  6:       name=                  # 服務名稱
  7:       runlevel=              #在哪些級別下啟動的
  8:       pattern=               #
  9:       enabled=               #開機是否讓其啟動{0|1}
 10:       arguments=             #
 11: ansible all -m service -a "state=stopped name=httpd enabled=yes"
 12: ansible all -a "chkconfig --list httpd"
ansible的playbooks,可把多個需要執行的命令存在劇本中,一次執行

  1: vim test.yaml
  2: 下面的格式很重要,冒號后面一定要有空格,tasks下面一定要有縮進,並且每行對齊
  3: - hosts: all         #執行的主機,前面有橫杠表示可以有多個
  4:  remote_user: root    #使用哪個用戶的身份在各遠程主機上執行命令,可以定義全局的,也可以定義單個任務要使用的用戶
  5:  tasks:              #需要執行的任務,因為有子任務,所以下面的任務需要縮進
  6:    - name: add a group  #第一個執行的任務的名字,類似注釋信息
  7:      group: gid=1001 name=testymal system=no  #具體的任務本身
  8:    - name: excute a command
  9:      command: /bin/date

 10: 執行順序是把一個任務在所有主機上執行一遍,再把第二個任務在所有主機上執行,如果在執行任務的過程中發生錯誤,將回滾所有可回滾的任務

ansible的handler

  1:     - hosts: all
  2:       remote_user: root
  3:       tasks:
  4:         - name: ensure apache latest version
  5:           yum: name=httpd state=laster
  6:         - name: apache configure file
  7:           copy: src=/root/httpd.conf dest=/etc/httpd/conf force=yes
  8:           notify:                          #調用下面的handlers
  9:             - restart httpd
 10:       handlers:                            #定義handlers要執行的動作
 11:           - name: restart httpd
 12:             service: name=httpd state=restarted
ansible的playbooks實現安裝heartbeat
  1: heartbeat.yaml
  2:     - hosts: hbhosts
  3:       remote_user: root
  4:       tasks:
  5:         - name: ensure heartbeat latest version
  6:           yum: name=heartbeat state=present      #安裝最新版本
  7:         - name: authkeys configure file
  8:           copy: src=/root/hb_conf/authkeys dest=/etc/ha.d/authkeys  #復制本地文件到遠程主機
  9:         - name: authkeys mode 600
 10:           file: path=/etc/ha.d/authkeys mode=600 #改文件權限,可以使用ansible-doc -s file查看
 11:           notify:                                #調用下面的handlers
 12:             - restart heartbeat
 13:         - name: ha.cf configure file
 14:           copy: src=/root/hb_conf/ha.cf dest=/etc/ha.d/ha.cf     #復制本地文件到遠程主機
 15:           notify:                                #調用下面的handlers
 16:                - restart heartbeat                  #調用下面的handlers
 17:       handlers:                                  #定義handlers的動作,這個的縮進與tasks一致
 18:           - name: restart heartbeat
 19:             service: name=heartbeat state=restarted

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM