Ansible常用模块使用详解

Ansible常用模块使用详解

Ansible常用模块有:

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

Ansible常用模块rawcommandshell的区别:

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

ansible常用模块之ping

ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong

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

ansible常用模块之lineinfile

lineinfile模块用于确保特定行是否在文件中。

//将yqh2中selinux设置为enforcing
[root@yqh1 ~]# ansible yqh2 -m lineinfile -a 'path=/etc/selinux/config regexp="^SELINUX=" line=SELINUX=enforcing'
yqh2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": false,
    "msg": ""
}

[root@yqh2 ~]# getenforce 
Enforcing

ansible常用模块之command

command模块用于在远程主机上执行命令,ansible默认就是使用command模块。

command模块有一个缺陷就是不能使用管道符和重定向功能。

[root@yqh1 ~]# ansible yqh2 -m command -a 'getenforce'
yqh2 | CHANGED | rc=0 >>
Enforcing

//不加-m默认是command模块
[root@yqh1 ~]# ansible yqh2 -a 'getenforce'
yqh2 | CHANGED | rc=0 >>
Enforcing

//command模块不支持管道符,不支持重定向
[root@yqh1 ~]# ansible yqh2 -a 'ps -ef|grep vsftpd'
yqh2 | FAILED | rc=1 >>
error: unsupported SysV option

Usage:
 ps [options]

 Try 'ps --help <simple|list|output|threads|misc|all>'
  or 'ps --help <s|l|o|t|m|a>'
 for additional help text.

For more details see ps(1).non-zero return code

ansible常用模块之raw

raw模块用于在远程主机上执行命令,其支持管道符与重定向

//支持重定向
[root@yqh1 ~]# ansible yqh2 -m raw -a 'echo "hello world" > /root/abc'
yqh2 | CHANGED | rc=0 >>
Shared connection to yqh2 closed.

[root@yqh2 ~]# pwd
/root
[root@yqh2 ~]# cat abc
hello world

//支持管道符
[root@yqh1 ~]# ansible yqh2 -m raw -a 'ps -ef|grep vsftpd'
yqh2 | CHANGED | rc=0 >>
root        2364    2326  0 18:31 pts/1    00:00:00 bash -c ps -ef|grep vsftpd
root        2382    2364  0 18:31 pts/1    00:00:00 grep vsftpd
Shared connection to yqh2 closed.

ansible常用模块之shell

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

//支持重定向
[root@yqh1 ~]# ansible yqh2 -m shell -a 'echo "hello world" > /root/bcd'
yqh2 | CHANGED | rc=0 >>

[root@yqh2 ~]# pwd
/root
[root@yqh2 ~]# cat bcd
hello world

//支持管道符
[root@yqh1 ~]# ansible yqh2 -m shell -a 'ps -ef|grep vsftpd'
yqh2 | CHANGED | rc=0 >>
root        2482    2481  0 18:33 pts/1    00:00:00 /bin/sh -c ps -ef|grep vsftpd
root        2484    2482  0 18:33 pts/1    00:00:00 grep vsftpd

ansible常用模块之script

script模块用于在受控机上执行主控机上的脚本

//写一个查看ip的脚本
[root@yqh1 ~]# mkdir scripts
[root@yqh1 ~]# vim /root/scripts/ip.sh
#!/bin/bash

ip a > /tmp/ip.txt

//让yqh2执行脚本
[root@yqh1 ~]# ansible yqh2 -m script -a '/root/scripts/ip.sh'
yqh2 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to yqh2 closed.\r\n",
    "stderr_lines": [
        "Shared connection to yqh2 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}

//验证
[root@yqh2 ~]# cat /tmp/ip.txt 
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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:bc:a7:b5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.2/24 brd 192.168.100.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:febc:a7b5/64 scope link 
       valid_lft forever preferred_lft forever

ansible常用模块之template

template模块用于生成一个模板,并可将其传输至远程主机上。

//下载一个阿里云的yum源文件并开启此源
[root@yqh1 ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
[root@yqh1 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@yqh1 ~]# sed -i 's|$releasever|8|' /etc/yum.repos.d/CentOS-Base.repo

//将设置好的阿里云源传到yqh2
[root@yqh1 ~]# ansible yqh2 -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/CentOS-Base.repo'
yqh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "e2c5e733b29668ef82633e043e094108e934d4d3",
    "dest": "/etc/yum.repos.d/CentOS-Base.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "021e9bb5a28116f6b3fe608ddb806ebc",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 1683,
    "src": "/root/.ansible/tmp/ansible-tmp-1609929998.1968453-5009-242589038739465/source",
    "state": "file",
    "uid": 0
}

//查看yqh2上是否有阿里云源
[root@yqh2 ~]# ls /etc/yum.repos.d/
CentOS-Base.repo

ansible常用模块之yum

yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个

  • name:要管理的包名
  • state:要进行的操作

state常用的值:

  • latest:安装软件
  • installed:安装软件
  • present:安装软件
  • removed:卸载软件
  • absent:卸载软件

若想使用yum来管理软件,请确保受控机上的yum源无异常。

//在yqh1上使用yum模块在yqh2上安装vsftpd
[root@yqh1 ~]# ansible yqh2 -m yum -a 'name=vsftpd state=present'
yqh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: vsftpd-3.0.3-32.el8.x86_64"
    ]
}

//查看yqh2上是否安装了vsftpd
[root@yqh2 ~]# rpm -qa|grep vsftpd
vsftpd-3.0.3-32.el8.x86_64

ansible常用模块之copy

copy模块用于复制文件至远程受控机。

//将创建的abc文件复制到yqh2上的/tmp/
[root@yqh1 ~]# echo "hello world" > abc
[root@yqh1 ~]# ansible yqh2 -m copy -a 'src=/root/abc dest=/tmp/abc'
yqh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "22596363b3de40b06f981fb85d82312e8c0ed511",
    "dest": "/tmp/abc",
    "gid": 0,
    "group": "root",
    "md5sum": "6f5902ac237024bdd0c176cb93063dc4",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:admin_home_t:s0",
    "size": 12,
    "src": "/root/.ansible/tmp/ansible-tmp-1609930360.463924-5094-234697478100730/source",
    "state": "file",
    "uid": 0
}

//验证
[root@yqh2 tmp]# cat /tmp/abc 
hello world

ansible常用模块之group

group模块用于在受控机上添加或删除组。

//在yqh2上创建yuqinghao组
[root@yqh1 ~]# ansible all -m group -a 'name=yuqinghao state=present'
yqh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 1001,
    "name": "yuqinghao",
    "state": "present",
    "system": false
}

//验证
[root@yqh2 ~]# grep yuqinghao /etc/group
yuqinghao:x:1001:

ansible常用模块之user

user模块用于管理受控机的用户帐号。

//在yqh2上创建一个用户,用户名为jerry,uid为2000,设置其shell为/sbin/nologin,无家目录
[root@yqh1 ~]# ansible yqh2 -m user -a 'name=jerry uid=2000 system=true shell=/sbin/nologin create_home=false state=present'
yqh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": false,
    "group": 991,
    "home": "/home/jerry",
    "name": "jerry",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": true,
    "uid": 2000
}

[root@yqh2 ~]# id jerry
uid=2000(jerry) gid=991(jerry) groups=991(jerry)

ansible常用模块之service

service模块用于管理受控机上的服务。

//将yqh2上的httpd服务启动,并设置为开启自启
[root@yqh1 ~]# ansible yqh2 -m service -a 'name=httpd enabled=true state=started'
yqh2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started",
    "status": {
······

//验证
[root@yqh2 ~]# 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-06 19:00:45 CST; 2min 15s ago
     Docs: man:httpd.service(8)
 Main PID: 13104 (httpd)
   Status: "Running, listening on: port 80"
······
posted @ 2021-01-06 21:20  Serein)  阅读(964)  评论(0编辑  收藏  举报