ASP站长网nsible可以使用命令行方式进行自动化管理,基本语法如下:
 
ansible 主机名 -m 模块名称 -a 模块特有参数
 
ansible的命令行管理工具都是由一系列模块、参数所支持的,可以在命令后面加上-h或--help获取帮助。如使用ansible-doc -h或者ansible-doc --help查看其帮助信息
 
ansible-doc是用来查看模块帮助信息的工具,最主要的选项 -l用来列出可使用的模块, -s用来列出某个模块的描述信息和使用示例。
 
以下是我列出yum模块的描述信息和操作动作:
 
[root@promote ~]# ansible-doc -s yum
- name: Manages packages with the `yum' package manager
  yum:
      allow_downgrade:      # Specify if the named package and version is
                              allowed to
                              downgrade a maybe
                              already installed
                              higher version of
                              that package.
                              Note that setting
                              allow_downgrade=T
                              rue can make this
                              module behave in
                              a non-idempotent
                              way.
 
Ansible自带了很多模块,能够下发执行Ansible的各种管理任务。下面我列出一些较为常用的模块。
1 command模块
ansible管理工具使用-m选项来指定使用模块,默认使用command模块,即-m选项省略时会运行次模块,用于在被管理主机上运行命令
 
[root@promote ~]# ansible-doc -s command
- name: Executes a command on a remote node
  command:
      argv:                  # Allows the user to provide the command as a list
                              vs. a string.
                              Only the string
                              or the list form
                              can be provided,
                              not both.  One or
                              the other must be
                              provided.
      chdir:                # Change into this directory before running the
                              command.
      creates:              # A filename or (since 2.0) glob pattern. If it
                              already exists,
                              this step *won't*
                              be run.
 
ansible-doc -l    #列出所有已安装的模块 注:按q退出
ansible-doc -s yum    #-s列出yum模块描述信息和操作动作
ansible 192.168.199.130 -m command -a 'date'    #指定IP执行date
ansible web -m command -a 'date'    #指定分类执行date
ansible all -m command -a 'date'    #所有hosts主机执行date
ansible all -a 'ls /'    #如果不加-m模块,则默认运行command模块
 
下面我在ansible服务器上执行‘date’命令来查看被管理主机的时间:
 
[root@promote ~]# ansible all -a 'date'
192.168.199.131 | CHANGED | rc=0 >>
2018年 10月 22日 星期一 22:35:53 CST
 
192.168.199.130 | CHANGED | rc=0 >>
2018年 10月 22日 星期一 22:35:53 CST
 
2 cron 模块
Ansible中的cron模块用于定义计划任务。其中两种状态(state):present表示添加(省略状态时默认使用),absent表示移除
 
[root@promote ~]# ansible-doc -s cron              #查看cron模块信息
- name: Manage cron.d and crontab entries
  cron:
      backup:                # If set, create a backup of the crontab before it
                              is modified. The
                              location of the
                              backup is
                              returned in the
                              `backup_file'
                              variable by this
                              module.
......
 
添加任务计划:
 
[root@promote ~]# ansible web -m cron -a 'minute="*/1" job="/usr/bin/echo hehe" name="test hehe"'
192.168.199.130 | SUCCESS => {
    "changed": false,
    "envs": [],
    "jobs": [
        "test hehe"
    ]
}
[root@promote ~]# ansible web -a 'crontab -l'            #查看web主机的计划性任务
192.168.199.130 | CHANGED | rc=0 >>
#Ansible: test hehe
*/1 * * * * /usr/bin/echo hehe

dawei

【声明】:九江站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。