linux服务-anbsible 常用模块

ansible常用模块使用详解

ansible常用模块有:

  • ping
  • yum
  • template
  • copy
  • user
  • group
  • service
  • raw
  • command
  • shell
  • script

ansible常用模块raw、command、shell的区别:

  • shell模块调用的/bin/sh指令执行
  • command模块不是调用的shell的指令,所以没有bash的环境变量
  • raw很多地方和shell类似,更多的地方建议使用shell和command模块。但是如果是使用老版本python,需要用到raw,又或者是客户端是路由器,因为没有安装python模块,那就需要使用raw模块了

ping模块

[root@ansible ~]# ansible 192.168.248.133 -m ping
192.168.248.133 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

command模块

若不指定模块,则默认使用该模块(不支持管道重定向)

[root@ansible ~]# ansible 192.168.248.133 -m command -a 'ls /root'
192.168.248.133 | CHANGED | rc=0 >>
anaconda-ks.cfg
 
[root@ansible ~]# ansible 192.168.248.133 -m command -a 'echo "123" > /tmp/a'
192.168.248.133 | CHANGED | rc=0 >>
123 > /tmp/a
[root@ansible ~]# ansible 192.168.248.133 -m command -a 'ls /tmp'
192.168.248.133 | CHANGED | rc=0 >>
ansible_command_payload_lovox0nu

row模块

其支持管道符与重定向

[root@ansible ~]# ansible 192.168.248.140 -m raw -a 'echo "123" > /tmp/a'
192.168.248.140 | CHANGED | rc=0 >>
Shared connection to 192.168.248.140 closed.
 
[root@ansible ~]# ansible 192.168.248.140 -m command -a 'ls /tmp'
192.168.248.140 | CHANGED | rc=0 >>
a
ansible_command_payload_oqlgr2mo

shell模块

shell模块用于在受控机上执行受控机上的脚本,亦可直接在受控机上执行命令。
shell模块亦支持管道与重定向。

root@ansible ~]# ansible 192.168.248.133 -m shell -a '/bin/bash /tmp/test.sh'
192.168.248.133 | CHANGED | rc=0 >>
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:75:47:f6 brd ff:ff:ff:ff:ff:ff
    inet 192.168.248.133/24 brd 192.168.248.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe75:47f6/64 scope link
       valid_lft forever preferred_lft forever

scripct模块

脚本模块

[root@ansible scripts]# ansible 192.168.248.133 -m script -a '/root/scripts/test.sh &>/tmp/a'
192.168.248.133 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.248.133 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.248.133 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}
[root@ansible scripts]# ansible 192.168.248.133  -a ' cat /tmp/a'
192.168.248.133 | CHANGED | rc=0 >>
hello world

template模块

#使用selinux作为模板
[root@ansible template]# cp  /etc/selinux/config   .
[root@ansible template]# cat config
 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted
 
#将设置好的模板传输到192.168.248.133上
[root@ansible ~]# ansible 192.168.248.133  -m template -a 'src=/root/template/config dest=/etc/selinux/'
192.168.248.133 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "385caf4e178c9a1dfcdaac71738934c735201480",
    "dest": "/etc/selinux/config",
    "gid": 0,
    "group": "root",
    "md5sum": "293160d55f3a26d5bc687154d028eb47",
    "mode": "0644",
    "owner": "root",
    "size": 547,
    "src": "/root/.ansible/tmp/ansible-tmp-1609988188.5656092-2545-80155753315326/source",
    "state": "file",
    "uid": 0
}
[root@ansible ~]# ansible 192.168.248.133   -a 'cat /etc/selinux/config'
192.168.248.133 | CHANGED | rc=0 >>
 
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

yum 模块

#安装
[root@ansible ~]# ansible 192.168.248.133  -m yum -a 'name=make state=present'
192.168.248.133 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: make-1:4.2.1-10.el8.x86_64"
    ]
}
 
#卸载
[root@ansible ~]# ansible 192.168.248.133  -m yum -a 'name=make  state=absent'
192.168.248.133 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Removed: make-1:4.2.1-10.el8.x86_64"
    ]
}

copy模块

复制文件到受控目标机

[root@ansible scripts]# ansible 192.168.248.133  -m copy -a 'src=/root/scripts/test.sh dest=/tmp/'
192.168.248.133 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "9cd86f975cbb1f70646a73c86a95e7c7f1625e7f",
    "dest": "/tmp/test.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "c708de2c5bbd87d1d29bba99612b184c",
    "mode": "0755",
    "owner": "root",
    "secontext": "unconfined_u:object_r:user_tmp_t:s0",
    "size": 31,
    "src": "/root/.ansible/tmp/ansible-tmp-1609988605.236166-2658-117693293460021/source",
    "state": "file",
    "uid": 0
}
[root@ansible scripts]# ansible 192.168.248.133  -a  ' ls /tmp/'
192.168.248.133 | CHANGED | rc=0 >>
a
ansible_command_payload_s_chwiie
test.sh

service

控制清单机上的服务

[root@ansible ~]# ansible 192.168.248.133 -m service -a 'name=httpd enabled=true state=started'
·192.168.248.133 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started",
    "status": {
······
#查看
[root@php ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2021-01-07 10:05:56 CST; 2min 15s ago
     Docs: man:httpd.service(8)

user

[root@ansible scripts]# ansible 192.168.248.133   -m user -a 'name=test  create_home=no system=yes shell=/sbin/nologin state=present'
192.168.248.133 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": false,
    "group": 990,
    "home": "/home/test",
    "name": "test",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": true,
    "uid": 992
}
[root@ansible scripts]# ansible 192.168.248.133   -a 'grep test /etc/passwd'
192.168.248.133 | CHANGED | rc=0 >>
test:x:992:990::/home/test:/sbin/nologin

posted on 2021-01-07 13:10  fxx013  阅读(128)  评论(0编辑  收藏  举报

导航