ansible常用模块

 

 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模块了

 

 ansible常用模块之ping

ping模块用于检查指定节点机器是否连通,用法很简单,不涉及参数,主机如果在线,则回复pong
[root@wang ansible]# ansible all -m ping
ming | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

ansible常用模块之command

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

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

查看受管主机的/tmp目录
[root@wang ansible]# ansible all -a 'ls /tmp'
ming | CHANGED | rc=0 >>
ansible_command_payload_vsnuh1wq
mysql.sock
mysql.sock.lock
vmware-root_870-2731086752
vmware-root_881-4013198953
在受管主机/tmp下创建test文件
[root@wang ansible]# ansible all -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.
ming | CHANGED | rc=0 >>
[root@wang ansible]# ansible all -a 'ls /tmp'
ming | CHANGED | rc=0 >>
ansible_command_payload_p997ylo9
mysql.sock
mysql.sock.lock
test
vmware-root_870-2731086752
vmware-root_881-4013198953

ansible常用模块之raw

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

重定向写入
[root@wang ansible]# ansible all -m raw -a 'echo "hello world" > /tmp/test'
ming | CHANGED | rc=0 >>
Shared connection to ming closed.
[root@wang ansible]# ansible all -a 'cat /tmp/test'
ming | CHANGED | rc=0 >>
hello world
管道符查找
[root@wang ansible]# ansible all -m raw -a 'cat /tmp/test | grep -o hello'
ming | CHANGED | rc=0 >>
hello
Shared connection to ming closed.

ansible常用模块之script

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

在主控机上创建脚本
[root@wang ansible]# mkdir scripts
[root@wang ansible]# ls
ansible.cfg  hosts  inventory  roles  scripts
[root@wang ansible]# vim scripts/ip.sh
#!/bin/bash
ip a > /tmp/ip.txt
执行脚本
[root@wang ansible]# ansible all -m script -a 'scripts/ip.sh'
ming | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to ming closed.\r\n",
    "stderr_lines": [
        "Shared connection to ming closed."
    ],
    "stdout": "",
    "stdout_lines": []
}
验证
[root@wang ansible]# ansible all -a 'cat /tmp/ip.txt'
ming | 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: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:f6:6d:19 brd ff:ff:ff:ff:ff:ff
    inet 192.168.170.21/24 brd 192.168.170.255 scope global noprefixroute ens160
       valid_lft forever preferred_lft forever

ansible常用模块之template

template模块用于生成一个模板,并可将其传输至远程主机上
/下载一个163的yum源文件并开启此源
[root@ansible ~]# cd /etc/yum.repos.d/
[root@ansible yum.repos.d]# curl -o CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@localhost ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost ~]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo

//将设置好的163源传到受控主机
[root@ansible ~]# ansible 172.16.103.129 -m template -a 'src=/etc/yum.repos.d/CentOS7-Base-163.repo dest=/etc/yum.repos.d/163.repo'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "checksum": "60b8868e0599489038710c45025fc11cbccf35f2",
    "dest": "/etc/yum.repos.d/163.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "5a3e688854d9ceccf327b953dab55b21",
    "mode": "0644",
    "owner": "root",
    "size": 1462,
    "src": "/root/.ansible/tmp/ansible-tmp-1536311319.27-78101453778196/source",
    "state": "file",
    "uid": 0
}

//查看受控机上是否有163源
[root@localhost ~]# ls /etc/yum.repos.d/
163.repo
ansible常用模块之yum;也可以使用dnf,操作方法一样,速度更快
yum模块用于在指定节点机器上通过yum管理软件,其支持的参数主要有两个
name:要管理的包名
state:要进行的操作
state常用的值:
latest:安装软件
installed:安装软件
present:安装软件
removed:卸载软件
absent:卸载软件
若想使用yum来管理软件,请确保受控机上的yum源无异常。

查看软件是否安装
[root@wang ansible]# ansible all -a 'rpm -q vsftpd'
[WARNING]: Consider using the yum, dnf or zypper module rather than running
'rpm'.  If you need to use command because yum, dnf or zypper 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.
ming | FAILED | rc=1 >>
未安装软件包 vsftpd non-zero return code
安装软件包
[root@wang ansible]# ansible all -m yum -a 'name=vsftpd state=present'
ming | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: vsftpd-3.0.3-28.el8.x86_64"
    ]
}
查看是否安装成功
[root@wang ansible]# ansible all -a 'rpm -q vsftpd'
[WARNING]: Consider using the yum, dnf or zypper module rather than running
'rpm'.  If you need to use command because yum, dnf or zypper 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.
ming | CHANGED | rc=0 >>
vsftpd-3.0.3-28.el8.x86_64
卸载
[root@wang ansible]# ansible all -m yum -a 'name=vsftpd state=removed'
ming | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Removed: vsftpd-3.0.3-28.el8.x86_64"
    ]
}
[root@wang ansible]# ansible all -a 'rpm -q vsftpd'
[WARNING]: Consider using the yum, dnf or zypper module rather than running
'rpm'.  If you need to use command because yum, dnf or zypper 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.
ming | FAILED | rc=1 >>
未安装软件包 vsftpd non-zero return code

ansible常用模块之copy

copy模块用于复制文件至远程受控机。
[root@wang ansible]# ansible all -m copy -a 'src=/etc/ansible/scripts/ip.sh dest=/scripts/'
ming | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "aa3704839663f784aeb47f2f10e6f4c81e979a6d",
    "dest": "/scripts/ip.sh",
    "gid": 0,
    "group": "root",
    "md5sum": "44504de23423f9d9d9feb75ff1e4c29e",
    "mode": "0644",
    "owner": "root",
    "size": 31,
    "src": "/root/.ansible/tmp/ansible-tmp-1609958745.0061903-2362-86965191801567/source",
    "state": "file",
    "uid": 0
}
[root@wang ansible]# ansible all -m shell -a 'ls /scripts'
ming | CHANGED | rc=0 >>
ip.sh

ansible常用模块之group

group模块用于在受控机上添加或删除组。
//在受控机上添加一个系统组,其gid为306,组名为mysql
[root@wang ansible]# ansible all -m group -a 'name=mysql gid=306 state=present'
ming | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 306,
    "name": "mysql",
    "state": "present",
    "system": false
}
[root@wang ansible]# ansible all -m shell -a 'grep mysql /etc/group'
ming | CHANGED | rc=0 >>
mysql:x:306:
删除受控机上的mysql组
[root@wang ansible]# ansible all -m group -a 'name=mysql state=absent'

ansible常用模块之user

 

user模块用于管理受控机的用户帐号。
//在受控机上添加一个系统用户,用户名为httpd,uid为306,设置其shell为/sbin/nologin,无家目录
[root@wang ansible]# ansible all -m user -a 'name=httpd uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
ming | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": false,
    "group": 305,
    "home": "/home/httpd",
    "name": "httpd",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": true,
    "uid": 306
}
修改uuid

[root@wang ansible]# ansible all -m user -a 'name=httpd uid=366'
删除用户
[root@wang ansible]# ansible all -m user -a 'name=httpd state=absent'

 

ansible常用模块之service

 

service模块用于管理受控机上的服务
查看服务是否启动,未启动状态
[root@wang ansible]# ansible all -m shell -a 'systemctl is-active  vsftpd'
ming | FAILED | rc=3 >>
inactivenon-zero return code
启动服务
[root@wang ansible]# ansible all -m service -a 'name=vsftpd state=started'
[root@wang ansible]# ansible all -m shell -a 'systemctl is-active  vsftpd'
ming | CHANGED | rc=0 >>
active
停止服务
[root@wang ansible]# ansible all -m service -a 'name=vsftpd state=stopped'

 安装lapm

将网络源复制到三台主机

[root@wang ansible]# ansible all -m copy -a 'src=/etc/yum.repos.d/CentOS-Base.repo dest=/etc/yum.repos.d/'

安装apache

安装需要的包

[root@wang ansible]# ansible all -m yum -a 'name=wget,bzip2,gcc,gcc-c++,make,pcre-devel,expat-devel,libxml2-devel,openssl-devel state=present'
解压HTTP依赖包
[root@wang ansible]# ansible ming1 -a 'tar xf httpd-2.4.46.tar.gz'
 [root@wang ansible]# ansible ming1 -a 'tar xf apr-1.7.0.tar.bz2'
[root@wang ansible]# ansible ming1 -a 'tar xf apr-util-1.6.1.tar.gz'
创建apache服务的用户和组,该用户为系统用户,无法登入,没用家目录
[root@wang ansible]# ansible ming1 -m group -a 'name=apache state=present'
[root@wang ansible]# ansible ming1 -m user -a 'name=apache system=yes create_home=no shell=/bin/nologin state=present'
 编译apr包
[root@wang ansible]# ansible ming1 -m shell -a 'cd ~/apr-1.7.0 && ./configure --prefix=/usr/local/apr'
[root@wang ~]# ansible ming1 -m shell -a 'cd ~/apr-1.7.0 && make && make install'
编译apr-util包
[root@wang ~]# ansible ming1 -m shell -a 'cd ~/apr-util-1.6.1 && ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr'
[root@wang ~]# ansible ming1 -m shell -a 'cd ~/apr-util-1.6.1 && make && make install'
编译安装httpd
[root@wang ~]# ansible ming1 -m shell -a 'cd ~/httpd-2.4.46&& ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite 
--with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=prefork'
[root@wang ~]# ansible ming1 -m shell -a 'cd ~/httpd-2.4.46&& make && make install'
设置环境变量
[root@wang ~]# ansible ming1 -m shell -a 'echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh'
[root@wang ~]# ansible ming1 -m shell -a 'source /etc/profile.d/httpd.sh'
ming1 | CHANGED | rc=0 >>  设置软链接 [root@wang ~]# ansible ming1 -m shell -a 'cd /usr/local && ln -s /usr/local/apache/include /usr/include/apache'
ming1 | CHANGED | rc=0 >> 设置帮助文档,在管理机编辑好后复制过去 [root@wang ~]# ansible ming1 -m copy -a 'src=/etc/man_db.conf dest=/etc/man_db.conf'
ming1 | CHANGED =>  启动服务 [root@wang ~]# ansible ming1 -m shell -a '/usr/local/apache/bin/apachectl start'
ming1 | CHANGED | rc=0 >>
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::20c:29ff:fed7:7c67%ens160. Set the 'ServerName' directive globally to suppress this message
[root@wang ~]# ansible ming1 -m shell -a 'ss -antl'
ming1 | CHANGED | rc=0 >>
State    Recv-Q    Send-Q        Local Address:Port        Peer Address:Port   
LISTEN   0         128                 0.0.0.0:22               0.0.0.0:*      
LISTEN   0         128                    [::]:22                  [::]:*      
LISTEN   0         128                       *:80                     *:*      

安装MySQL

 

下载,解压文件
[root@wang ~]# ansible ming2 -m shell -a 'cd ~ && wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz'
[root@wang ~]# ansible ming2 -m shell -a 'cd ~ && tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local'
ming2 | CHANGED | rc=0 >>
创建用户
[root@wang ~]# ansible ming2 -m user -a 'name=mysql system=yes create_home=no shell=/sbin/nologin state=present'

设置软链接
[root@wang ~]# ansible ming2 -m shell -a 'cd /usr/local && ln -s /usr/local/mysql-5.7.31-linux-glibc2.12-x86_64 /usr/local/mysql'
ming2 | CHANGED | rc=0 >>
修改目录的属主和属组
[root@wang ~]# ansible ming2 -m shell -a 'chown -R mysql:mysql /usr/local/mysql'
设置环境变量
[root@wang ~]# ansible ming2 -m shell -a 'echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh‘
ming2 | CHANGED | rc=0 >>
[root@wang ~]# ansible ming2 -m shell -a 'source /etc/profile.d/mysql.sh'
ming2 | CHANGED | rc=0 >>
映射文件
[root@wang ~]# ansible ming2 -m shell -a 'cd /usr/local && ln -s /usr/local/mysql/include /usr/include/mysql'
ming2 | CHANGED | rc=0 >>
设置lib库
[root@wang ~]# ansible ming2 -m shell -a 'echo "/usr/local/mysql/lib" > /etc/ld.so.conf.d/mysql.conf'
ming2 | CHANGED | rc=0 >>
[root@wang ~]# ansible ming2 -m shell -a 'ldconfig'
ming2 | CHANGED | rc=0 >>
配置目录存放组
[root@wang ~]# ansible ming2 -a 'mkdir /opt/data'
[WARNING]: Consider using the file module with state=directory rather than
running 'mkdir'.  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.
ming2 | CHANGED | rc=0 >>
[root@wang ~]# ansible ming2 -a 'chown -R mysql:mysql /opt/data'
[WARNING]: Consider using the file module with owner rather than running
'chown'.  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.
ming2 | CHANGED | rc=0 >>
写配置文件
[root@192 ~]# vim /etc/my.cnf
[root@192 ~]# cat /etc/my.cnf 
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
ansible node3 -m copy -a 'src=/etc/my.cnf  dest=/etc/my.cnf'
配置服务脚本
[root@wang ~]# ansible ming2 -m shell -a 'cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld'
ming2 | CHANGED | rc=0 >>
[root@wang ~]# ansible ming2 -m shell -a 'sed -ri "s#^(basedir=).*#\1/usr/local/mysql#g" /etc/init.d/mysqld'
[WARNING]: Consider using the replace, lineinfile or template module rather
than running 'sed'.  If you need to use command because replace, lineinfile or
template 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.
ming2 | CHANGED | rc=0 >>
[root@wang ~]# ansible ming2 -m shell -a 'sed -ri "s#^(datadir=).*#\1/opt/data#g" /etc/init.d/mysqld'
[WARNING]: Consider using the replace, lineinfile or template module rather
than running 'sed'.  If you need to use command because replace, lineinfile or
template 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.
ming2 | CHANGED | rc=0 >>
启动并修改密码
[root@wang ~]# ansible ming2 -m yum -a 'name=ncurses-compat-libs state=present'
[root@wang ~]# ansible ming -m shell -a 'service mysqld start'
[root@wang ~]# ansible ming -m shell -a '/usr/local/mysql/bin/mysql -uroot -p"k%kXJK>s:55?" --connect-expired-password -e "set password = password(\"990304\")
设置开机自动启动
[root@wang ~]# ansible ming2 -m shell -a 'chkconfig --add mysqld'
[root@wang ~]# ansible ming2 -m shell -a 'chkconfig mysqld on'
ming2 | CHANGED | rc=0 >>

安装php

下载
[root@wang ~]# ansible ming3 -m shell -a 'cd ~ && wget https://www.php.net/distributions/php-8.0.0.tar.bz2'
解压
[root@wang ~]# ansible ming3 -m shell -a 'cd ~ && tar xf php-8.0.0.tar.bz2'
ming3 | CHANGED | rc=0 >>
安装依赖包
[root@wang ~]# ansible ming3 -m yum -a 'name=libxml2,libxml2-devel,openssl,openssl-devel,bzip2,bzip2-devel,libcurl,libcurl-devel,libicu-devel,libjpeg,libjpeg-devel,libpng,libpng-devel,openldap-devel,pcre-devel,freetype,freetype-devel,gmp,gmp-devel,libmcrypt,libmcrypt-devel,readline,readline-devel,libxslt,libxslt-devel,mhash,mhash-devel state=present'
编译安装php
[root@wang ~]# ansible ming3 -m shell -a 'cd php-8.0.0 && ./configure --prefix=/usr/locall/php7 --with-config-file-path=/etc --enable-fpm --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-soap --with-openssl --enable-bcmath --with-iconv --with-bz2 --enable-calendar --with-curl --enable-exif --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --with-gettext --enable-json --enable-mbstring --enable-pdo --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-readline --enable-shmop --enable-simplexml --enable-sockets --enable-zip --enable-mysqlnd-compression-support --with-pear --enable-pcntl --enable-posix'
解析失败,报错
root@wang ~]# ansible ming3 -m shell -a 'cd ~/php-8.0.0 && make -j $(cat /proc/cpuinfo |grep processor|wc -l) && make install'
ming3 | FAILED | rc=2 >>
make: *** 没有指明目标并且找不到 makefile。 停止。non-zero return code

 

 

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

相关推荐