首页 > 其他分享 >Ansible常用模块

Ansible常用模块

时间:2022-10-24 03:33:06浏览次数:46  
标签:常用 root CHANGED 222.137 192.168 Ansible 模块 ansible

Ansible常用模块


目录

Ansible常用模块详解

ansible常用模块有:

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

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

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

ansible常用模块之ping

//将受控主机加入ansible清单
[root@ansible ~]# cd /etc/ansible/
[root@ansible ansible]# ls
ansible.cfg  hosts  roles
[root@ansible ansible]# touch inventory
[root@ansible ansible]# ls
ansible.cfg  hosts  inventory  roles
[root@ansible ansible]# vim ansible.cfg 
#inventory      = /etc/ansible/hosts    //取消注释并修改为下面这样
inventory       = /etc/ansible/inventory
[root@ansible ansible]# vim inventory 
[root@ansible ansible]# cat inventory 
[web]
192.168.222.137
[root@ansible ansible]# ansible all -m ping
192.168.222.137 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

ansible常用模块之command

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

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

//查看受控主机的/tmp目录内容
[root@ansible ansible]# ansible 192.168.222.137 -a 'ls /tmp'
192.168.222.137 | CHANGED | rc=0 >>
a
ansible_command_payload_pgp9m_c8
systemd-private-ddc74647b1614d9f97693ed56992353e-nginx.service-LcMflb
vmware-root_901-3988228452
vmware-root_903-3979774182
//在受控主机的/tmp目录下新建一个文件test
[root@ansible ansible]# ansible 192.168.222.137 -a 'touch /tmp/test'
[WARNING]: Consider using the file module with state=touch rather than running 'touch'.  If you need to
use command because file is insufficient you can add 'warn: false' to this command task or set
'command_warnings=False' in ansible.cfg to get rid of this message.
192.168.222.137 | CHANGED | rc=0 >>

[root@ansible ansible]# ansible 192.168.222.137 -a 'ls /tmp'
192.168.222.137 | CHANGED | rc=0 >>
a
ansible_command_payload_l91ypv4n
systemd-private-ddc74647b1614d9f97693ed56992353e-nginx.service-LcMflb
test
vmware-root_901-3988228452
vmware-root_903-3979774182
//command模块不支持管道符,不支持重定向
[root@ansible ansible]# ansible 192.168.222.137 -a "echo 'hello world' > /tmp/test"
192.168.222.137 | CHANGED | rc=0 >>
hello world > /tmp/test
[root@ansible ansible]# ansible 192.168.222.137 -a 'cat /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>

[root@ansible ansible]# ansible 192.168.222.137 -a 'ps -ef|grep vsftpd'
192.168.222.137 | 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@ansible ansible]# ansible 192.168.222.137 -m raw -a 'echo "hello world" > /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>
Shared connection to 192.168.222.137 closed.

[root@ansible ansible]# ansible 192.168.222.137 -a 'cat /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>
hello world
[root@ansible ansible]# ansible 192.168.222.137 -m raw -a 'cat /tmp/test|grep -Eo hello'
192.168.222.137 | CHANGED | rc=0 >>
hello
Shared connection to 192.168.222.137 closed.

ansible常用模块之shell

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

//查看受控机上的脚本
[root@nginx ~]# mkdir /scripts/
[root@nginx ~]# cd /scripts/
[root@nginx scripts]# vim test.sh
[root@nginx scripts]# cat test.sh 
#!/bin/bash
my_arry=$(seq 1 10)
for i in ${my_arry[@]};do
    echo $i
done
[root@nginx scripts]# chmod +x test.sh 
[root@nginx scripts]# cd
[root@nginx ~]# ll /scripts/
total 4
-rwxr-xr-x 1 root root 76 Oct 24 02:33 test.sh
//使用shell模块在主控机上执行受控机上的脚本
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>

[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'cat  /tmp/test'
192.168.222.137 | CHANGED | rc=0 >>
1
2
3
4
5
6
7
8
9
10

ansible常用模块之script

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

[root@ansible ansible]# mkdir -p /scripts
[root@ansible ansible]# vim /scripts/test.sh
[root@ansible ansible]# cat /scripts/test.sh 
#!/bin/bash
my_arry=$(seq 1 10)
for i in ${my_arry[@]};do
    echo $i
done

[root@ansible ansible]# ansible 192.168.222.137 -m script -a '/scripts/test.sh'
192.168.222.137 | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.222.137 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 192.168.222.137 closed."
    ],
    "stdout": "1\r\n2\r\n3\r\n4\r\n5\r\n6\r\n7\r\n8\r\n9\r\n10\r\n",
    "stdout_lines": [
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10"
    ]
}

ansible常用模块之template

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

//将设置好的阿里源传到受控主机
[root@ansible ~]# cd /etc/yum.repos.d/
[root@ansible yum.repos.d]# rm -rf *
[root@ansible yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo     
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   3574      0 --:--:-- --:--:-- --:--:--  3574
[root@ansible yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@ansible yum.repos.d]# cd
[root@ansible ~]# ansible nginx  -m template -a 'src=/etc/yum.repos.d/CentOS-Base.repo  dest=/etc/yum.repos.d/CentOS-Base.repo'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "8bbf30b2d80c3b97292ca7b32f33ef494269a5b8",
    "dest": "/etc/yum.repos.d/CentOS-Base.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "ed031c350da2532e6a8d09a4d9b05278",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 1653,
    "src": "/root/.ansible/tmp/ansible-tmp-1666511143.7368824-130351-128775339422969/source",
    "state": "file",
    "uid": 0
}
//查看受控主机上面
[root@nginx ~]# ls /etc/yum.repos.d/
CentOS-Base.repo

ansible常用模块之yum

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

  1. name:要管理的包名

  2. state:要进行的操作
    state常用的值:

  3. latest:安装软件

  4. installed:安装软件

  5. present:安装软件

  6. removed:卸载软件

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

//在受控机上查询看vsftpd软件是否安装
[root@nginx ~]# rpm -qa|grep vsftpd
[root@nginx ~]# 
//在ansible主机上使用yum模块在受控机上安装vsftpd
[root@ansible ansible]# ansible 192.168.222.137 -m yum -a 'name=vsftpd state=present'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: vsftpd-3.0.3-34.el8.x86_64"
    ]
}
//查看受控机上是否安装了vsftpd
[root@nginx ~]# rpm -qa|grep vsftpd
vsftpd-3.0.3-34.el8.x86_64

ansible常用模块之copy

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

[root@ansible ansible]# ls xbz
ssh  ssh.sh
[root@ansible ansible]# ansible 192.168.222.137 -m copy -a 'src=/etc/ansible/xbz/ssh.sh dest=/xbz/'192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "da39a3ee5e6b4b0d3255bfef95601890afd80709",
    "dest": "/xbz/ssh.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "d41d8cd98f00b204e9800998ecf8427e",
    "mode": "0644",
    "owner": "root",
    "size": 0,
    "src": "/root/.ansible/tmp/ansible-tmp-1666551783.8760731-1363195-260276005042864/source",
    "state": "file",
    "uid": 0
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'ls /xbz/'
192.168.222.137 | CHANGED | rc=0 >>
ssh.sh

ansible常用模块之group

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

//在受控机上添加一个系统组,其gid为306,组名为mysql
[root@ansible ansible]# ansible 192.168.222.137 -m group -a 'name=mysql gid=306 state=present'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 306,
    "name": "mysql",
    "state": "present",
    "system": false
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/group'
192.168.222.137 | CHANGED | rc=0 >>
mysql:x:306:
//删除受控机上的mysql组
[root@ansible ansible]# ansible 192.168.222.137 -m group -a 'name=mysql state=absent'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "mysql",
    "state": "absent"
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/group'
192.168.222.137 | FAILED | rc=1 >>
non-zero return code

ansible常用模块之user

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

//在受控机上添加一个系统用户,用户名为mysql,uid为306,设置其shell为/sbin/nologin,无家目录
[root@ansible ansible]# ansible 192.168.222.137 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": false,
    "group": 306,
    "home": "/home/mysql",
    "name": "mysql",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": true,
    "uid": 306
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/passwd'
192.168.222.137 | CHANGED | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'ls /home'
192.168.222.137 | CHANGED | rc=0 >>
nginx
//修改mysql用户的uid为366
[root@ansible ansible]# ansible 192.168.222.137 -m user -a 'name=mysql uid=366'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "append": false,
    "changed": true,
    "comment": "",
    "group": 306,
    "home": "/home/mysql",
    "move_home": false,
    "name": "mysql",
    "shell": "/sbin/nologin",
    "state": "present",
    "uid": 366
}
[root@ansible ansible]# ansible 192.168.222.137  -m shell -a 'grep mysql /etc/passwd'
192.168.222.137 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin
//删除受控机上的mysql用户
[root@ansible ansible]# ansible 192.168.222.137  -m shell -a 'grep mysql /etc/passwd'
192.168.222.137 | CHANGED | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin
[root@ansible ansible]# ansible 192.168.222.137 -m user -a 'name=mysql state=absent'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "mysql",
    "remove": false,
    "state": "absent"
}
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'grep mysql /etc/passwd'
192.168.222.137 | FAILED | rc=1 >>
non-zero return code

ansible常用模块之service

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

//查看受控机上的vsftpd服务是否启动
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-active vsftpd'
192.168.222.137 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible ansible]# ansible 192.168.222.137 -m service -a 'name=vsftpd state=started'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "vsftpd",
    "state": "started",
    "status": {
        "ActiveState": "inactive",
//查看受控机上的vsftpd服务是否启动
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-active vsftpd'
192.168.222.137 | CHANGED | rc=0 >>
active
//查看受控机上的vsftpd服务是否开机自动启动
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-enabled vsftpd'
192.168.222.137 | FAILED | rc=1 >>
disablednon-zero return code
//设置受控机上的vsftpd服务开机自动启动
[root@ansible ansible]# ansible 192.168.222.137 -m service -a 'name=vsftpd enabled=yes'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
//查看受控机上的vsftpd服务是否开机自动启动
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-enabled vsftpd'
192.168.222.137 | CHANGED | rc=0 >>
enabled
//停止受控机上的vsftpd服务
[root@ansible ansible]# ansible 192.168.222.137 -m service -a 'name=vsftpd state=stopped'
192.168.222.137 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'systemctl is-active vsftpd'
192.168.222.137 | FAILED | rc=3 >>
inactivenon-zero return code
[root@ansible ansible]# ansible 192.168.222.137 -m shell -a 'ss -antl'
192.168.222.137 | CHANGED | rc=0 >>
State  Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0      128          0.0.0.0:80        0.0.0.0:*          
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*          
LISTEN 0      128             [::]:22           [::]:*          

标签:常用,root,CHANGED,222.137,192.168,Ansible,模块,ansible
From: https://www.cnblogs.com/tushanbu/p/16820238.html

相关文章

  • Ansible部署LNMP
    Ansible部署LNMP目录Ansible部署LNMP基础准备工作管理nginx受管主机部署nginx服务管理mysql受管主机部署mysql服务管理php受管主机部署php服务环境介绍:系统ip主......
  • Ansible常用模块的介绍与使用
    Ansible常用模块的介绍与使用ansible常用模块有:pingyumtemplatecopyusergroupservicerawcommandshellscriptansible常用模块raw、command、shell的区别:sh......
  • ansible常用模块的介绍与使用
    ansible常用模块的介绍与使用目录ansible常用模块的介绍与使用ansible常用模块之pingansible常用模块之commandansible常用模块之rawansible常用模块之shellansible常用模......
  • ansible配置基础
    配置文件详解[root@ansible~]#vim/etc/ansible/ansible.cfg[defaults]默认配置#somebasicdefaultvalues...#inventory=/etc/ansible/hosts#主机列表配置文......
  • 常用的内置对象
    常用内置对象一览Math对象向下取整Math.floorvarnum=3.14;varresult=Math.floor(num);console.log(result);//30-1的随机数Math.randomvarre......
  • Ansible常用模块
    目录Ansible常用模块1.Ansible常用模块详解2.ping3.command4.raw5.shell6.script7.template8.yum9.copy10.group11.user12.service13.file使用Ansible模块部......
  • centos7 防火墙及开放端口的常用命令
    开放centos系统tcp端口firewall-cmd--zone=public--add-port=待开放的端口号/tcp--permanent让端口生效firewall-cmd--reload查看防火墙所有开放的端口fi......
  • 异常处理与python内置模块
    异常常见类型syntaxError语法错误NameError当你引用了变量、模块、类、函数或代码中没有定义的其他名称时,将引发NameErrorIndexError当你尝试从序列(如列表或......
  • MILIANKE-CZ01-7010/7020400-核心模块硬件手册(ZYNQ)
    1产品概述    自2017年MLK-CZ01-7010/7020-400(MZ7XCORE400)系列开发平台发布以来,该系列开发平台和核心模块经过多次迭代升级,在工业自动化、水利电力控制设备、医疗图......
  • 2.9 复制文件和文件夹 shutil模块 shutil.copy shutil.copytree
    #复制文件:shutil.copy(要复制的文件,要复制文件的位置)#复制文件夹:shutil.copytree(要复制的文件夹,要复制文件夹的位置)-----------------------------------------......