访次: AmazingCounters.com 次

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
复制代码

 

posted @   IT老登  阅读(2553)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
访次: AmazingCounters.com 次
点击右上角即可分享
微信分享提示