详细的ansible模块

ansible模块

ansible的格式

ansible命令格式:ansible 【主机】 【-m 模块】 【-a args】 #不加模块 默认的是command模块

ansible-doc -l #列出所有安装的模块

ansible-doc -s 模块 #-s列出y模块描述信息和操作动作

command模块


[root@master .ssh]# ansible mysql -m command -a 'date'  #查看mysql的时间
Enter passphrase for key '/root/.ssh/id_rsa': 
20.0.0.15 | CHANGED | rc=0 >>
20201218日 星期五 23:46:17 CST

[root@master .ssh]# ansible all -m command -a 'date'  #查看两个节点的时间
Enter passphrase for key '/root/.ssh/id_rsa': Enter passphrase for key '/root/.ssh/id_rsa': #需要输入密码
20.0.0.14 | CHANGED | rc=0 >>
20201207日 星期一 18:01:01 CST
123^H^H^H^H^H^H^H^H^H^H

Enter passphrase for key '/root/.ssh/id_rsa': #需要输入密码
20.0.0.15 | CHANGED | rc=0 >>
20201218日 星期五 23:48:17 CST

免交互

[root@master .ssh]# ssh-agent bash  #免交互
[root@master .ssh]# ssh-add 
Enter passphrase for /root/.ssh/id_rsa: 
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

[root@master .ssh]# ansible all -m command -a 'date' #不需要在输入密码了
20.0.0.15 | CHANGED | rc=0 >>
20201218日 星期五 23:50:17 CST
20.0.0.14 | CHANGED | rc=0 >>
20201207日 星期一 18:03:27 CST

查看模块后面跟的小模块


[root@master .ssh]# ansible-doc -s command # 看command后面能跟的参数
- name: Execute commands on targets
  command:
      argv:                  # Passes the command as a list rather than a string. Use `argv' to avoid
                               quoting values that would otherwise be
                               interpreted incorrectly (for example
                               "user name"). 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.
      cmd:                   # The command to run.
      creates:               # A filename or (since 2.0) glob pattern. If it already exists, this step
                               *won't* be run.
      free_form:             # The command module takes a free form command to run. There is no actual
                               parameter named 'free form'.
      removes:               # A filename or (since 2.0) glob pattern. If it already exists, this step
                               *will* be run.
      stdin:                 # Set the stdin of the command directly to the specified value.
      stdin_add_newline:     # If set to `yes', append a newline to stdin data.
      strip_empty_ends:      # Strip empty lines from the end of stdout/stderr in result.
      warn:                  # Enable or disable task warnings.

cron模块

查看cron模块信息

ansible-doc -s cron 

每隔一分钟 所做的工作是往mysql节点上opt目录下的helltxt文件中输入一个’啦啦啦’ 工作的名称为’cron_hello’

[root@master .ssh]# ansible mysql -m cron -a'minute="*/1" job="/usr/bin/echo 啦啦啦 >> /opt/hello.txt" name="cron_hello"' #每隔一分钟  所做的工作是往mysql节点上opt目录下的helltxt文件中输入一个'啦啦啦'  工作的名称为'cron_hello'
20.0.0.15 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "cron_hello"
    ]
}
[root@master .ssh]# ansible mysql -a 'crontab -l'
20.0.0.15 | CHANGED | rc=0 >>
#Ansible: cron_hello
*/1 * * * * /usr/bin/echo 啦啦啦 >> /opt/hello.txt

### 在mysql节点是查看 ###
[root@node2 opt]# cat hello.txt 
啦啦啦
啦啦啦
啦啦啦
啦啦啦
啦啦啦

移除 删除指令(添加啦啦啦的指令)

[root@master .ssh]# ansible mysql -m cron -a 'name="cron_hello" state=absent' #移除 删除指令(添加啦啦啦的指令)
20.0.0.15 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": []
}
[root@master .ssh]# ansible mysql -a 'crontab -l' #计划性任务列表中没有计划
20.0.0.15 | CHANGED | rc=0 >>

user模块

用user模块创建一个用户

[root@master .ssh]# ansible all -m user -a 'name="jjj"' #添加用户jjj
20.0.0.15 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1001, 
    "home": "/home/jjj", 
    "name": "jjj", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1001
}
20.0.0.14 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "", 
    "create_home": true, 
    "group": 1001, 
    "home": "/home/jjj", 
    "name": "jjj", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 1001
}

删除node1中的 用户jjj

[root@master .ssh]# ansible webservers -m user -a 'name="jjj" state=absent' #删除node1中的 用户jjj
20.0.0.14 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "force": false, 
    "name": "jjj", 
    "remove": false, 
    "state": "absent"
}

[root@node1 opt]# id jjj
id: jjj: no such user

group模块

创建一个为mysql的组 gid为306,为系统用户


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

把jjj用户定义到 财务组中并且把uid改成1050

[root@master .ssh]# ansible mysql -m user -a 'name=jjj uid=1050 system=yes group=caiwu' #把jjj用户定义到 财务组中并且把uid改成1050
20.0.0.15 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "append": false, 
    "changed": true, 
    "comment": "", 
    "group": 1050, 
    "home": "/home/jjj", 
    "move_home": false, 
    "name": "jjj", 
    "shell": "/bin/bash", 
    "state": "present", 
    "uid": 1050
}
在mysql节点查看
[root@node2 ~]# id jjj
uid=1050(jjj) gid=1050(caiwu)=1050(caiwu)
[root@node2 ~]# 

copy模块

复制mysql节点上/etc/fstab文件到/opt/fstab.bak(备份),新文件属主为root,权限640

[root@master .ssh]# ansible mysql -m copy -a 'src=/etc/fstab dest=/opt/fstab.bak owner=jjj mode=600'
20.0.0.15 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "cf1c11d8227d21a5cf568eb9e17db628bfab200f", 
    "dest": "/opt/fstab.bak", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "7b6c3ed7677cdc76c9c79a09cc5323ad", 
    "mode": "0600", 
    "owner": "jjj", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 635, 
    "src": "/root/.ansible/tmp/ansible-tmp-1607338190.44-56532-141768809784388/source", 
    "state": "file", 
    "uid": 1050
}

[root@node2 ~]# cd /opt/
[root@node2 opt]# ll
总用量 8
-rw-------. 1 jjj  root 635 1219 00:42 fstab.bak
-rw-r--r--. 1 root root 120 1219 00:19 hello.txt
drwxr-xr-x. 2 root root   6 326 2015 rh

[root@master .ssh]# ansible mysql -m copy -a 'content="this is ggg" dest=/opt/ggg.txt'  #拷贝内容为 ’this is ggg‘ 到 opt 目录下
20.0.0.15 | CHANGED => {
    
    
    "ansible_facts": {
    
    
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "fa76b60445fb1955f4f92ea88f0c5f5ca878718f", 
    "dest": "/opt/ggg.txt", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "ae79dfcda670f54053a0e1d164de0884", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "system_u:object_r:usr_t:s0", 
    "size": 11, 
    "src": "/root/.ansible/tmp/ansible-tmp-1607339156.35-56759-61373438582239/source", 
    "state": "file", 
    "uid": 0
}

[root@glt6 opt]# cat ggg.txt 
this is ggg[root@glt6 opt]#

file模块

未更改前的属主  和宿组

[root@node2 ~]# cd /opt/
[root@node2 opt]# ll
总用量 8
-rw-------. 1 jjj  root 635 1219 00:42 fstab.bak

用file模块来修改
[root@master .ssh]# ansible mysql -m file -a 'owner=root group=caiwu mode=666 path=/opt/fstab.bak' #更改文件 把opt下的fstab.bak 的主

在mysql节点上查看
[root@node2 opt]# ll
总用量 12
-rw-rw-rw-. 1 root caiwu  11 1219 01:11 fstab.bak

创建链接型文件

[root@master ~]# ansible mysql -m file -a 'path=/fstab.link src=/opt/fstab.bak state=link' #链接型文件

在mysql节点上查看
[root@node2 opt]# ll /
总用量 30
lrwxrwxrwx.   1 root root    7 1218 22:28 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 1218 22:43 boot
drwxr-xr-x.  20 root root 3300 1218 22:42 dev
drwxr-xr-x. 142 root root 8192 1219 09:06 etc
lrwxrwxrwx.   1 root root   14 1219 09:14 fstab.link -> /opt/fstab.bak

删除创建的链接型文件

[root@master etc]# ansible mysql -m file -a 'path=/opt/fstab.bak state=absent'#删除创建的链接型文件

创建空文件

[root@master etc]# ansible mysql -m file -a 'path=/opt/abc state=touch' 

在mysql节点上查看
[root@node2 opt]# ll
总用量 8
-rw-r--r--. 1 root root   0 1219 09:25 abc

创建一个目录

[root@master ~]# ansible mysql -m file -a 'path=/opt/share state=directory mode=755' #创建一个目录

在mysql节点上查看
[root@node2 opt]# ll
总用量 8
-rw-r--r--. 1 root root   0 1219 09:25 abc
-rw-r--r--. 1 root root  11 1219 00:58 ggg.txt
-rw-r--r--. 1 root root 120 1219 00:19 hello.txt
drwxr-xr-x. 2 root root   6 326 2015 rh
drwxr-xr-x. 2 root root   6 1219 09:28 share

ping模块

[root@master ~]# ansible all -m ping #ping模块

yum模块

远程安装软件

[root@master ~]# ansible mysql -m yum -a 'name=httpd'#远程安装软件
[root@node2 opt]# rpm -q httpd
httpd-2.4.6-67.el7.centos.x86_64
[root@node2 opt]# 
[root@master ~]# ansible mysql -m yum -a 'name=httpd state=absent' #卸载yum源软件包

service模块

启动httpd服务

[root@master ~]# ansible mysql -m service -a 'name=httpd state=started enabled=true' #启动httpd 开机自启
[root@master ~]# ansible mysql -m service -a 'name=firewalld state=started'#开启防火墙
[root@master ~]# ansible mysql -m copy -a 'content="this is kkk" dest=/var/www/html/index.html' #往httpd里写东西

在这里插入图片描述

shell模块

#往ttt.txt中写内容
[root@master ~]# ansible mysql -m shell -a 'echo this is ggg > /opt/ttt.txt'

#到 /usr/local 目录下创建了一个 test.txt 文件 并且往里面写了内容
[root@master ~]# ansible mysql -m shell -a 'chdir=/usr/local echo this is rrr > test.txt'


#在/usr/local目录下创建一个test.txt文件 并往里面写内容

[root@master ~]# ansible mysql -m shell -a 'chdir=/usr/local echo this is rrr > test.txt'

scrpt模块

#在mster编写一个脚本
[root@master opt]# vim share.sh

在这里插入图片描述

#在所有节点上都写入脚本的内容
[root@master opt]# ansible all -m script -a 'share.sh' #在所有节点上都写入脚本的内容
[root@node1 opt]# ll
总用量 4
-rw-r--r--. 1 root root 6 128 04:45 hello.txt
[root@node2 opt]# ll
总用量 4
-rw-r--r--. 1 root root 6 1219 10:31 hello.tx

setup模块

获取mysql组主机的facts信息;facts(事实):主机名称、内核版本、网络接口、IP地址等

ansible mysql -m setup 

猜你喜欢

转载自blog.csdn.net/weixin_50346902/article/details/112600838