其他分享
首页 > 其他分享> > Ansible的hoc命令行以及常用模块

Ansible的hoc命令行以及常用模块

作者:互联网

ansible的ad hoc命令行格式:

ansible <host-pattern> -i <inventory path> -m <moudle> -a <moudle argument>

<host-pattern> :表示 某个主机组或者某个主机

-i 参数后面跟inventory路径,<inventory path>表示inventory路径

-m 参数后面跟模块,比如 shell、command、copy、ping、yum等等

-a 后面跟的是模块的参数

例: 

[root@bogon lizf]# ansible test -i /home/lizf/host -m shell -a 'ls /tmp/' 
192.168.131.129 | CHANGED | rc=0 >>
ansible_command_payload_dM2M60
systemd-private-7daea4ed89884c8ca5fc9f2ca1bd4766-chronyd.service-Xpd1xn
vmware-root_715-4281843213

ansible基础模块使用:

 ansible-doc -l  列出当前系统中所有的ansible模块

当你想查看某一个模块的用法时,可以用ansible-doc <模块名称>,当然查看到的都是playbook格式的操作。

下面列出一下常用的ansible模块以及用法:

命令模块: command、shell

文件模块:copy、fetch、lineinfile

用户模块:user、group

定时任务模块:cron

安装模块:yum

压缩解压模块:unarchive

服务模块:service

挂载模块:mount

command

 适合使用简单的命令 无法支持"<",">","|",";","&"等符号,如果ansible命令不加-m参数,默认就使用command模块

[root@bogon lizf]# ansible test -i host -m command -a 'hostname'
192.168.131.129 | CHANGED | rc=0 >>
bogon

shell

类似command模块升级版—万能模块,可以写shell命令,可以使用"<",">","|",";","&"等符号特殊符号

[root@bogon lizf]# ansible test -i host -m shell -a 'egrep -v "^#|^$" /etc/ssh/sshd_config'
192.168.131.129 | CHANGED | rc=0 >>
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key
SyslogFacility AUTHPRIV
AuthorizedKeysFile      .ssh/authorized_keys
PasswordAuthentication yes
ChallengeResponseAuthentication no
GSSAPIAuthentication yes
GSSAPICleanupCredentials no
UsePAM yes
X11Forwarding yes
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL LANGUAGE
AcceptEnv XMODIFIERS
Subsystem       sftp    /usr/libexec/openssh/sftp-server

copy

copy模块是常用的文件拷贝模块,后面可以加很多的参数

src  指定需要将本地拷贝的文件路径

dest 需要拷贝到目标主机的路径

mode 设置拷贝到目标主机的权限

group 设置拷贝文件的属组信息

owner 设置拷贝文件的属主用户信息

force  当设置为yes时,如果目标主机有同名的文件,将会覆盖;设置为no时则原文件不会做改变

backup 默认为no,当设置为yes时,如果目标主机有同名的文件时,先备份原文件,然后再覆盖

[root@bogon lizf]# ansible test -i /home/lizf/host -m copy -a 'src=/home/lizf/host dest=/home/ mode=0777 owner=lizf group=lizf backup=yes' 
192.168.131.129 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "ae983bbcb03c06e321b54652af1a0b43a14628d5", 
    "dest": "/home/host", 
    "gid": 1000, 
    "group": "lizf", 
    "md5sum": "a3fb8ee7823ccf7ca7b5af22279f0ce6", 
    "mode": "0777", 
    "owner": "lizf", 
    "secontext": "unconfined_u:object_r:user_home_dir_t:s0", 
    "size": 24, 
    "src": "/root/.ansible/tmp/ansible-tmp-1628262316.65-245400100393655/source", 
    "state": "file", 
    "uid": 1000
}
[root@bogon lizf]# ansible test -i /home/lizf/host -m shell -a 'ls -l /home/'                                                                   
192.168.131.129 | CHANGED | rc=0 >>
总用量 4
-rwxrwxrwx. 1 lizf lizf  24 8月   6 23:05 host
drwx------. 3 lizf lizf 132 8月   5 00:04 lizf

 fetch

抓取文件到ansible主机

src  : 被管理主机中文件的路径

dest :ansible主机要保存的文件路径

[root@bogon lizf]# ansible test -i /home/lizf/host -m fetch -a 'src=/tmp/xiaozhi.txt  dest=/home/lizf/'                
192.168.131.129 | CHANGED => {
    "changed": true, 
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "dest": "/home/lizf/192.168.131.129/tmp/xiaozhi.txt", 
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e", 
    "remote_checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709", 
    "remote_md5sum": null
}
[root@bogon lizf]# tree -n 5 /home/
5 [error opening dir]
/home/
├── ansible.cfg
├── lizf
│   ├── 192.168.131.129
│   │   ├── tmp
│   │   │   └── xiaozhi.txt
│   │   └── var
│   │       └── log
│   │           └── messages
│   └── host
└── xiaozhi

 

标签:hoc,ansible,命令行,host,Ansible,lizf,模块,home,LC
来源: https://www.cnblogs.com/xiaozhi1223/p/15101290.html