ansible常用模块

ansible常用模块使用详解

ansible常用模块有:

  • ping
  • 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@cb1 ~]# ansible all -m ping
cb2 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

ansible常用模块之command

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

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

 

[root@cb1 ~]# ansible cb2 -a 'ls /tmp' //查看一下cb2的tmp
cb2 | CHANGED | rc=0 >>
ansible_command_payload_dukn8k88
hsperfdata_root
systemd-private-c72fa6840c54495eb4d10d8aa0d288a6-chronyd.service-qKrtN2
systemd-private-c72fa6840c54495eb4d10d8aa0d288a6-mariadb.service-UZSZCg
vmware-root_887-4013330030
vmware-root_890-2722107963
vmware-root_892-2722239036
vmware-root_893-3988097506
vmware-root_898-2722239165
vmware-root_903-3979774182
[root@cb1 ~]# ansible cb2 -a 'touch /tmp/a1'  // 在cb2的tmp下面touch一个a1
[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.
cb2 | CHANGED | rc=0 >>

[root@cb1 ~]# ansible cb2 -a 'ls /tmp'
cb2 | CHANGED | rc=0 >>
a1
ansible_command_payload_76y9n_pi
hsperfdata_root
systemd-private-c72fa6840c54495eb4d10d8aa0d288a6-chronyd.service-qKrtN2
systemd-private-c72fa6840c54495eb4d10d8aa0d288a6-mariadb.service-UZSZCg
vmware-root_887-4013330030
vmware-root_890-2722107963
vmware-root_892-2722239036
vmware-root_893-3988097506
vmware-root_898-2722239165
vmware-root_903-3979774182
[root@cb1 ~]# 
command模块不支持管道符,不支持重定向

 ansible常用模块之raw

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

[root@cb1 ~]# ansible cb2 -m raw -a 'echo "redhat" > /tmp/a1'
cb2 | CHANGED | rc=0 >>
Shared connection to cb2 closed.

[root@cb1 ~]# ansible cb2 -a 'cat /tmp/a1'
cb2 | CHANGED | rc=0 >>
redhat
支持重定向

ansible常用模块之shell

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

[root@cb2 scripts]# vim test.sh 

#!/bin/bash
echo 'hello,boss!'
[root@cb1 ~]# ansible cb2 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
cb2 | CHANGED | rc=0 >>

[root@cb1 ~]# ansible cb2 -m shell -a 'cat /tmp/test'
cb2 | CHANGED | rc=0 >>
hello,boss!
[root@cb1 ~]#

ansible常用模块之script

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

 

ansible常用模块之template

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

[root@cb1 ~]# cd /etc/yum.repos.d/
[root@cb1 yum.repos.d]# curl -o CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1572  100  1572    0     0    723      0  0:00:02  0:00:02 --:--:--   723
[root@cb1 yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@cb1 yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@cb1 yum.repos.d]# 
//下载一个163的yum源文件并开启此源
[root@cb1 yum.repos.d]# ansible cb2 -m template -a 'src=/etc/yum.repos.d/CentOS7-Base-163.repo dest=/etc/yum.repos.d/163.repo'
cb2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "60b8868e0599489038710c45025fc11cbccf35f2",
    "dest": "/etc/yum.repos.d/163.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "5a3e688854d9ceccf327b953dab55b21",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 1462,
    "src": "/root/.ansible/tmp/ansible-tmp-1609952271.907525-25686-212126671573994/source",
    "state": "file",
    "uid": 0
}
//将设置好的163源传到受控主机
[root@cb2 ~]# ls /etc/yum.repos.d/
163.repo  redhat.repo  xx.repo
[root@cb2 ~]# 
//查看受控机上是否有163源

ansible常用模块之yum

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

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

state常用的值:

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

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

//在受控机上查询看vsftpd软件是否安装
[root@cb2 ~]# rpm -qa|grep vsftpd

[root@cb2 ~]#
//在cb1主机上使用yum模块在受控机上安装vsftpd

[root@cb1 ~]# ansible cb2 -m copy -a 'src=/etc/ansible/scripts/a.sh dest=/scripts/'
cb2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "3de43701e7f743c199714e48d2d9207cff9242c7",
"dest": "/scripts/a.sh",
"gid": 0,
"group": "root",
"md5sum": "7e642f5e23e33215b90decdc29b1d997",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:default_t:s0",
"size": 32,
"src": "/root/.ansible/tmp/ansible-tmp-1609953238.7101834-25901-24558157836919/source",
"state": "file",
"uid": 0
}
[root@cb1 ~]# ansible cb2 -m shell -a 'ls /scripts/'
cb2 | CHANGED | rc=0 >>
a.sh
test.sh
[root@cb1 ~]#

 

ansible常用模块之group

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

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

ansible常用模块之user

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

//在受控机上添加一个系统用户,用户名为tom,uid为306,设置其shell为/sbin/nologin,无家目录
[root@cb1 ~]# ansible cb2 -m user -a 'name=tom uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
cb2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": false,
    "group": 305,
    "home": "/home/tom",
    "name": "tom",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": true,
    "uid": 306
}
//修改mysql用户的uid为366
[root@cb1 ~]# ansible cb2 -m shell -a 'grep tom /etc/passwd'
cb2 | CHANGED | rc=0 >>
tom:x:306:305::/home/tom:/sbin/nologin
[root@cb1 ~]# ansible cb2 -m shell -a 'ls /home'
cb2 | CHANGED | rc=0 >>

[root@cb1 ~]# ansible cb2 -m user -a 'name=tom uid=366'
cb2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "append": false,
    "changed": true,
    "comment": "",
    "group": 305,
    "home": "/home/tom",
    "move_home": false,
    "name": "tom",
    "shell": "/sbin/nologin",
    "state": "present",
    "uid": 366
}
[root@cb1 ~]# ansible cb2 -m shell -a 'grep tom /etc/passwd'
cb2 | CHANGED | rc=0 >>
tom:x:366:305::/home/tom:/sbin/nologin
//删除受控机上的mysql用户
[root@cb1 ~]# ansible cb2 -m user -a 'name=tom state=absent'
cb2 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "force": false,
    "name": "tom",
    "remove": false,
    "state": "absent"
}
[root@cb1 ~]# ansible cb2 -m shell -a 'grep tom /etc/passwd'
cb2 | FAILED | rc=1 >>
non-zero return code

ansible常用模块之service

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

 

弄4台主机,其中一台装ansible,其余三台分别部署nginx、mysql、php,实现lnmp架构。请合理分配主机资源,所有主机均给500M内存即可,若资源富裕多给些亦可。

主控机ip:192.168.122.134   cb1

受控机ip:192.168.122.130   cb2   httpd

                192.168.122.135  cb3   mysql

                192.168.122.136  cb4   php

// 设置免密
[root@cb1 ~]# vim /etc/hosts

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.122.134 cb1
192.168.122.130 cb2
192.168.122.135 cb3
192.168.122.136 cb4
[root@cb1 ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
        (if you think this is a mistake, you may want to use -f option)

[root@cb1 ~]# ssh-copy-id [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
        (if you think this is a mistake, you may want to use -f option)

[root@cb1 ~]# vim /etc/ansible/inventory 

[dbs]
cb1

[webservers]
cb2
cb3
cb4

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐