ansible安装及模块管理(最全操作,可跟做!)(代码片段)

author author     2023-04-22     497

关键词:

一、Ansible安装
环境准备

管理端:CentOS 7-2 192.168.18.147
被管理端1:CentOS 7-3 192.168.18.128
被管理端2:CentOS 7-4 192.168.18.148

管理端:
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# yum install epel-release -y
[root@localhost ~]# yum install ansible -y
[root@localhost ~]# ansible --version
ansible 2.9.2
[root@localhost ~]# yum install tree -y
[root@localhost ~]# tree /etc/ansible/
/etc/ansible/
├── ansible.cfg         #配置文件
├── hosts
└── roles

1 directory, 2 files
`配置主机清单`
[root@localhost ~]# vim  /etc/ansible/hosts
#在24行下插入以下内容
[webserver]
192.168.18.128
[mysql]
192.168.18.148

`生成密钥对`
[root@localhost ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):        #直接回车
Created directory ‘/root/.ssh‘.
Enter passphrase (empty for no passphrase):     #输入密码:abc123
Enter same passphrase again:                    #再次输入密码:abc123
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:5RvIVqbI9hscNK1Y4YivNnnUEgQeNfNm/WJcBXr8jWc root@localhost.localdomain
The key‘s randomart image is:
+---[RSA 2048]----+
|    oo= .   ...  |
|   . + * + o .   |
|    o o O B +    |
|     o @ @ + . o |
|      O S * . o E|
|     = = o +   o |
|    = . + .      |
|   . o   o       |
|        .        |
+----[SHA256]-----+

`密钥对位置`
[root@localhost ~]# ls -la
总用量 56
......
drwx------.  2 root root   38 1月  22 17:34 .ssh
......此处省略多行
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
id_rsa(私钥)  id_rsa.pub(公钥)

`把密钥推给被管理端1`
[root@localhost .ssh]# ssh-copy-id root@192.168.18.128
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host ‘192.168.18.128 (192.168.18.128)‘ can‘t be established.
ECDSA key fingerprint is SHA256:mTT+FEtzAu4X3D5srZlz93S3gye8MzbqVZFDzfJd4Gk.
ECDSA key fingerprint is MD5:fa:5a:88:23:49:60:9b:b8:7e:4b:14:4b:3f:cd:96:a0.
Are you sure you want to continue connecting (yes/no)? yes  #确认链接
/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: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.18.128‘s password:     #输入相对应被管理端的root密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh ‘root@192.168.18.128‘"
and check to make sure that only the key(s) you wanted were added.
`把密钥推给被管理端2`
[root@localhost .ssh]# ssh-copy-id root@192.168.18.148
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host ‘192.168.18.148 (192.168.18.148)‘ can‘t be established.
ECDSA key fingerprint is SHA256:mTT+FEtzAu4X3D5srZlz93S3gye8MzbqVZFDzfJd4Gk.
ECDSA key fingerprint is MD5:fa:5a:88:23:49:60:9b:b8:7e:4b:14:4b:3f:cd:96:a0.
Are you sure you want to continue connecting (yes/no)? yes  #确认链接
/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: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.18.148‘s password:     #输入相对应被管理端的root密码

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh ‘root@192.168.18.148‘"
and check to make sure that only the key(s) you wanted were added.

验证密钥是否推送成功:

被管理端1:192.168.18.128
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
authorized_keys
#此时密钥推送成功
被管理端2:
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
authorized_keys
#此时密钥推送成功

二、Ansible命令行模块

1、command模块

`使用IP地址查看被管理端1的时间`
[root@localhost .ssh]# ansible 192.168.18.128 -m command -a ‘date‘
Enter passphrase for key ‘/root/.ssh/id_rsa‘:       #输入密钥密码abc123
192.168.18.128 | CHANGED | rc=0 >>
2020年 02月 02日 星期日 15:53:20 CST
`使用别名查看被管理端2的时间`
[root@localhost .ssh]# ansible mysql -m command -a ‘date‘
Enter passphrase for key ‘/root/.ssh/id_rsa‘:       #输入密钥密码abc123
192.168.18.148 | CHANGED | rc=0 >>
2020年 02月 02日 星期日 15:55:13 CST

`为避免总是输入密码的麻烦,我们可以执行免交互代理`
[root@localhost .ssh]# ssh-agent bash
[root@localhost .ssh]# ssh-add
Enter passphrase for /root/.ssh/id_rsa:             #输入密钥密码abc123   
Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)

[root@localhost .ssh]# ansible webserver -m command -a ‘date‘
192.168.18.128 | CHANGED | rc=0 >>
2020年 02月 02日 星期日 16:01:40 CST
#此时可以免交互直接显示时间

`所有hosts主机执行date命令`
[root@localhost .ssh]# ansible all -a ‘date‘
192.168.18.128 | CHANGED | rc=0 >>
2020年 02月 02日 星期日 16:21:08 CST

192.168.18.148 | CHANGED | rc=0 >>
2020年 02月 02日 星期日 16:21:08 CST

2、cron模块

两种状态(state):present表示添加(可以省略),absent表示移除

[root@localhost .ssh]# ansible mysql -m cron -a ‘minute="*/1" job="/usr/bin/echo hello"  name="test hello"‘
192.168.18.148 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "envs": [],
    "jobs": [
        "test hello"
    ]

[root@localhost .ssh]# ansible mysql -a ‘crontab -l‘
192.168.18.148 | CHANGED | rc=0 >>
#Ansible: test hello
*/1 * * * * /usr/bin/echo hello
此时我们可以进入被管理端2进行验证:
[root@localhost .ssh]# crontab -l
#Ansible: test hello
*/1 * * * * /usr/bin/echo hello
您在 /var/spool/mail/root 中有新邮件

[root@localhost .ssh]# vim /var/spool/mail/root
From root@localhost.localdomain  Sun Feb  2 16:40:02 2020
Return-Path: <root@localhost.localdomain>
X-Original-To: root
Delivered-To: root@localhost.localdomain
Received: by localhost.localdomain (Postfix, from userid 0)
        id 2255A319AE4E; Sun,  2 Feb 2020 16:40:02 +0800 (CST)
From: "(Cron Daemon)" <root@localhost.localdomain>
To: root@localhost.localdomain
Subject: Cron <root@localhost> /usr/bin/echo hello
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=19>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=zh_CN.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20200202084002.2255A319AE4E@localhost.localdomain>
Date: Sun,  2 Feb 2020 16:40:02 +0800 (CST)

hello
#以下省略多行,每分钟生成一个
此时移除计划性任务,使用absent:
[root@localhost .ssh]# ansible mysql -m cron -a ‘name="test hello" state=absent‘
192.168.18.148 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "envs": [],
    "jobs": []


`此时再回到被管理端2中会发现计划任务消失`
[root@localhost ~]# crontab -l

3、user模块

user模块是请求的是useradd, userdel, usermod三个指令

`创建用户test01`
[root@localhost ~]# ansible all -m user -a ‘name=test01‘
192.168.18.128 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/test01",
    "name": "test01",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001

192.168.18.148 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/test01",
    "name": "test01",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001

`此时回到被管理端1中验证添加用户`
[root@localhost ~]# id test01
uid=1001(test01) gid=1001(test01) 组=1001(test01)
`此时回到被管理端2中验证添加用户`
[root@localhost ~]# id test01
uid=1001(test01) gid=1001(test01) 组=1001(test01)
#此时两台被管理端test01用户均添加成功

`删除webserver端中的test01用户`
[root@localhost ~]# ansible webserver -m user -a ‘name=test01 state=absent‘
192.168.18.128 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "force": false,
    "name": "test01",
    "remove": false,
    "state": "absent"

`此时回到被管理端1:webserver中验证用户情况`
[root@localhost ~]# id test01
id: test01: no such user
#此时显示找不到,说明已被删除

3、group模块

group模块请求的是groupadd, groupdel, groupmod 三个指令

`创建mysql组`
[root@localhost ~]# ansible mysql -m group -a ‘name=mysql gid=306 system=yes‘
192.168.18.148 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "gid": 306,
    "name": "mysql",
    "state": "present",
    "system": true

`远程查看被管理端2:mysql中是否有mysql组`
[root@localhost ~]# ansible mysql -a ‘tail /etc/group‘
192.168.18.148 | CHANGED | rc=0 >>
postfix:x:89:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
tcpdump:x:72:
zhou:x:1000:
dhcpd:x:177:
named:x:25:
test01:x:1001:
mysql:x:306:        #此时有mysql组,同时gid号为306

`创建新用户test02并添加到mysql组`
[root@localhost ~]# ansible mysql -m user -a ‘name=test02 uid=306 group=mysql system=yes‘
192.168.18.148 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 306,
    "home": "/home/test02",
    "name": "test02",
    "shell": "/bin/bash",
    "state": "present",
    "system": true,
    "uid": 306

`远程查看被管理端2:mysql中是否在mysql组是否有新创建的用户test02`
[root@localhost ~]# ansible mysql -a ‘id test02‘
192.168.18.148 | CHANGED | rc=0 >>
uid=306(test02) gid=306(mysql) 组=306(mysql)

4、copy模块

`远程把被管理端2:mysql中的etc目录下fstab自动挂载文件,复制到opt目录下并且取名为fstab.bk,属组为root,权限为644`
[root@localhost ~]# ansible mysql -m copy -a ‘src=/etc/fstab dest=/opt/fstab.bk owner=root mode=644‘
192.168.18.148 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "checksum": "100f3bbf6644926857bbec2a40ab2f70bf1c060b",
    "dest": "/opt/fstab.bk",
    "gid": 0,
    "group": "root",
    "md5sum": "f57167de0e8f6f2963771a72af8a2840",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:usr_t:s0",
    "size": 595,
    "src": "/root/.ansible/tmp/ansible-tmp-1580693038.81-171191249824445/source",
    "state": "file",
    "uid": 0

`远程查看被管理端2:mysql的opt目录下是否在mysql组是否有fstab.bk文件`
[root@localhost ~]# ansible mysql -a ‘ls -l /opt‘
192.168.18.148 | CHANGED | rc=0 >>
总用量 4
-rw-r--r--. 1 root root 595 2月   3 09:24 fstab.bk
drwxr-xr-x. 2 root root   6 3月  26 2015 rh

`指定内容this is test,重定向生成新文件test.txt在opt目录下`
[root@localhost ~]# ansible mysql -m copy -a ‘content="this is test" dest=/opt/test.txt‘
192.168.18.148 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "checksum": "b6794b2000d94d348203d0279c2e7322b922cb16",
    "dest": "/opt/test.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "8c6d115258631625b625486f81b09532",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:usr_t:s0",
    "size": 12,
    "src": "/root/.ansible/tmp/ansible-tmp-1580693472.89-123279558248268/source",
    "state": "file",
    "uid": 0

`远程查看被管理端2:mysql的opt目录下的test.txt文件中内容是否为this is test`
[root@localhost ~]# ansible mysql -a ‘cat /opt/test.txt‘
192.168.18.148 | CHANGED | rc=0 >>
this is test

5、file模块

`路径opt下的文件test.txt,用户为test02,组指定为mysql,权限为666`
[root@localhost ~]# ansible mysql -m file -a ‘path=/opt/test.txt owner=test02 group=mysql mode=666‘
192.168.18.148 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "gid": 306,
    "group": "mysql",
    "mode": "0666",
    "owner": "test02",
    "path": "/opt/test.txt",
    "secontext": "system_u:object_r:usr_t:s0",
    "size": 12,
    "state": "file",
    "uid": 306

`此时回到被管理端2:mysql中opt目录下的test.txt文件的详细情况`
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls -l
总用量 8
-rw-r--r--. 1 root   root  595 2月   3 09:24 fstab.bk
drwxr-xr-x. 2 root   root    6 3月  26 2015 rh
-rw-rw-rw-. 1 test02 mysql  12 2月   3 09:31 test.txt
#此时test.txt文件属主为test02,属组为mysql,权限为666

`设置/opt/test.txt.link为/opt/test.txt的链接文件`
[root@localhost ~]# ansible mysql -m file -a ‘src=/opt/test.txt path=/opt/test.txt.link state=link‘
192.168.18.148 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "dest": "/opt/test.txt.link",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:usr_t:s0",
    "size": 13,
    "src": "/opt/test.txt",
    "state": "link",
    "uid": 0

`此时回到被管理端2:mysql中opt目录下查看此链接文件`
[root@localhost opt]# ls -l
总用量 8
-rw-r--r--. 1 root   root  595 2月   3 09:24 fstab.bk
drwxr-xr-x. 2 root   root    6 3月  26 2015 rh
-rw-rw-rw-. 1 test02 mysql  12 2月   3 09:31 test.txt
lrwxrwxrwx. 1 root   root   13 2月   3 09:59 test.txt.link -> /opt/test.txt  #链接性的文件

`创建一个空文件`
[root@localhost ~]# ansible mysql -m file -a ‘path=/opt/abc.txt state=touch‘
192.168.18.148 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "dest": "/opt/abc.txt",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "secontext": "unconfined_u:object_r:usr_t:s0",
    "size": 0,
    "state": "file",
    "uid": 0

`此时回到被管理端2:mysql中opt目录下abc.txt文件的详细情况`
[root@localhost opt]# ls                #此时有abc.txt文件
abc.txt  fstab.bk  rh  test.txt  test.txt.link
[root@localhost opt]# cat abc.txt       #因为是空文件,所以没有内容

`删除创建的abc.txt空文件`
[root@localhost ~]# ansible mysql -m file -a ‘path=/opt/abc.txt state=absent‘
192.168.18.148 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "path": "/opt/abc.txt",
    "state": "absent"

`此时回到被管理端2:mysql中opt目录下是否有abc.txt文件`
[root@localhost opt]# ls
fstab.bk  rh  test.txt  test.txt.link

6、ping模块

`测试两台被管理端是否在线`
[root@localhost ~]# ansible all -m ping
192.168.18.148 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": false,
    "ping": "pong"

192.168.18.128 | SUCCESS => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": false,
    "ping": "pong"

7、yum模块

`在被管理端1:webserver中安装httpd服务`
[root@localhost ~]# ansible webserver -m yum -a ‘name=httpd‘
192.168.18.128 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "changes": 
        "installed": [
            "httpd"
        ]
    ,
    "msg": "",
    "rc": 0,
    "results": [
        "Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirror.bit.edu.cn
 * updates: mirror.bit.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-5.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package            Arch          Version                     Repository   Size
================================================================================
Installing:
 httpd              x86_64        2.4.6-90.el7.centos         base        2.7 M
Installing for dependencies:
 apr                x86_64        1.4.8-5.el7                 base        103 k
 apr-util           x86_64        1.5.2-6.el7                 base         92 k
 httpd-tools        x86_64        2.4.6-90.el7.centos         base         91 k
 mailcap            noarch        2.1.41-2.el7                base         31 k

Transaction Summary
================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 3.0 M
Installed size: 10 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                              1.0 MB/s | 3.0 MB  00:03     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-5.el7.x86_64                                       1/5 
  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/5 
  Installing : httpd-tools-2.4.6-90.el7.centos.x86_64                       3/5 
  Installing : mailcap-2.1.41-2.el7.noarch                                  4/5 
  Installing : httpd-2.4.6-90.el7.centos.x86_64                             5/5 
  Verifying  : apr-1.4.8-5.el7.x86_64                                       1/5 
  Verifying  : mailcap-2.1.41-2.el7.noarch                                  2/5 
  Verifying  : httpd-tools-2.4.6-90.el7.centos.x86_64                       3/5 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  4/5 
  Verifying  : httpd-2.4.6-90.el7.centos.x86_64                             5/5 

Installed:
  httpd.x86_64 0:2.4.6-90.el7.centos                                            

Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7                     apr-util.x86_64 0:1.5.2-6.el7    
  httpd-tools.x86_64 0:2.4.6-90.el7.centos     mailcap.noarch 0:2.1.41-2.el7    

Complete!
"
    ]

#安装完成后会显示过程
`此时可以返回被管理端1:webserver中查看httpd服务是否安装成功`
[root@localhost ~]# rpm -q httpd
httpd-2.4.6-90.el7.centos.x86_64        #此时服务安装完成

`可以用以下命令移除服务`
[root@localhost ~]# ansible webserver -m yum -a ‘name=httpd state=absent‘

8、service模块

`启动被管理端2中的httpd服务`
[root@localhost ~]# ansible webserver -m service -a ‘enabled=true name=httpd state=started‘
192.168.18.128 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started",
......此处省略多行
`此时到被管理端1:webserver中查看httpd服务的状态`
[root@localhost ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 一 2020-02-03 10:24:28 CST; 2min 25s ago
#此时状态显示为running运行

9、shell模块

`创建一个用户,为用户生成免交互密码`
[root@localhost ~]# ansible webserver -m user -a ‘name=jarry‘       #创建新用户jarry
192.168.18.128 | CHANGED => 
    "ansible_facts": 
        "discovered_interpreter_python": "/usr/bin/python"
    ,
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/jarry",
    "name": "jarry",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001

[root@localhost ~]# ansible webserver -m shell -a ‘echo abc123 | passwd --stdin jarry‘
192.168.18.128 | CHANGED | rc=0 >>
更改用户 jarry 的密码 。
passwd:所有的身份验证令牌已经成功更新。
#生成jarry用户的登录密码为abc123

10、script模块

`首先在管理端编写脚本`
[root@localhost ~]# cd /opt/
[root@localhost opt]# vim test.sh
#!/bin/bash
echo "this is test script" > /opt/script.txt
chmod 666 /opt/script.txt
[root@localhost opt]# ls
rh  test.sh
[root@localhost opt]# chmod +x test.sh      #给予执行权限

`对所有被管理端执行test.sh脚本`
[root@localhost opt]# ansible all -m script -a ‘test.sh‘
192.168.18.128 | CHANGED => 
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.18.128 closed.
",
    "stderr_lines": [
        "Shared connection to 192.168.18.128 closed."
    ],
    "stdout": "",
    "stdout_lines": []

192.168.18.148 | CHANGED => 
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 192.168.18.148 closed.
",
    "stderr_lines": [
        "Shared connection to 192.168.18.148 closed."
    ],
    "stdout": "",
    "stdout_lines": []

`验证两台被管理端的opt目录下是否有script.txt文件`
#被管理端1:
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
rh  script.txt
[root@localhost opt]# cat script.txt
this is test script
#被管理端2:
[root@localhost opt]# ls
fstab.bk  rh  script.txt  test.txt  test.txt.link
[root@localhost opt]# cat script.txt
this is test script

11、setup模块

`列出被管理端2:mysql的所有主机信息`
[root@localhost opt]# ansible mysql -m setup
192.168.18.148 | SUCCESS => 
    "ansible_facts": 
        "ansible_all_ipv4_addresses": [
            "192.168.122.1",
            "192.168.18.148"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::1cb1:b734:7f72:576f",
            "fe80::578f:4368:6a2c:80d7",
            "fe80::6a0c:e6a0:7978:3543"
        ],
        "ansible_apparmor": 
            "status": "disabled"
        ,
        "ansible_architecture": "x86_64",
        "ansible_bios_date": "07/29/2019",
        "ansible_bios_version": "6.00",
        "ansible_cmdline": 
            "BOOT_IMAGE": "/vmlinuz-3.10.0-693.el7.x86_64",
            "LANG": "zh_CN.UTF-8",
            "crashkernel": "auto",
            "quiet": true,
            "rhgb": true,
            "ro": true,
            "root": "UUID=32c169ff-9bf7-4d89-a2f1-a99a7e59d4f2"
        ,
        "ansible_date_time": 
            "date": "2020-02-03",
            "day": "03",
            "epoch": "1580698171",
            "hour": "10",
            "iso8601": "2020-02-03T02:49:31Z",
            "iso8601_basic": "20200203T104931948449",
            "iso8601_basic_short": "20200203T104931",
            "iso8601_micro": "2020-02-03T02:49:31.948682Z",
            "minute": "49",
            "month": "02",
            "second": "31",
            "time": "10:49:31",
            "tz": "CST",
            "tz_offset": "+0800",
            "weekday": "星期一",
            "weekday_number": "1",
            "weeknumber": "05",
            "year": "2020"
        ,
        "ansible_default_ipv4": 
            "address": "192.168.18.148",
            "alias": "ens33",
            "broadcast": "192.168.18.255",
            "gateway": "192.168.18.2",
            "interface": "ens33",
            "macaddress": "00:0c:29:79:45:8e",
            "mtu": 1500,
            "netmask": "255.255.255.0",
            "network": "192.168.18.0",
            "type": "ether"
        ,
......以下省略多行

ansible安装及模块的管理(代码片段)

简介1、ansible安装部署过程特别简单,学习曲线很平坦;2、不需要单独安装客户端,知识利用现有的SSHD服务(协议)即可。3、基于Python开发,集合了众多运维工具(puppet、chef、func、fabric)的优点,实现了批量系统配置、批量... 查看详情

ansible的安装及常用模块

简介Ansible基于Python语言实现,由Paramiko和PyYAML两个关键模块构建。Ansible特点:1、部署简单,只需在主控端部署Ansible环境,被控端无需做任何操作。2、默认使用SSH(SecureShell)协议对设备进行管理。3、主从集中化管理。4、配置... 查看详情

自动化运维工具ansible——安装及模块管理(代码片段)

ansible简介?ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、func、fabric)的优点,实现了批量系统配置、批量程序部署、批量运行命令等功能。?ansible是基于模块工作的,本身没有批量... 查看详情

ansible架构介绍及部署(代码片段)

Ansible架构模式Ansible是由控制机和被管理机组成:控制机是用来安装Ansible工具软件和执行指令的服务器,被管理机是指运行业务的服务器,由控制机通过SSH进行管理。Ansible管理方式Ansible是一个模型驱动管理器,支持多节点发布... 查看详情

ansible安装及基础模块介绍(代码片段)

ansible介绍Ansible基于Python开发,集合了众多优秀运维工具的优点,实现了批量运行命令部署程序、配置系统等功能。默认通过SSH协议进行远程命令执行或下发配置,无需部署任何客户端代理软件,从而使得自动化环境部署变得更加简单... 查看详情

一:ansible介绍及安装(代码片段)

一:Ansible基本概述1什么是ansibleAnsible是一个自动化统一配置管理工具,自动化主要体现在Ansible集成了丰富模块以及功能组件,可以通过一个命令完成一系列的操作,进而能减少重复性的工作和维护成本,可以提高工作效率。2同... 查看详情

ansible安装及部分模块初步使用(代码片段)

Ansible安装及初步使用Ansible概述由于互联网的快速发展导致产品更新换代速度逐渐加快,运维人员每天都要进行大量的维护操作,仍旧按照传统的方式进行维护工作会使得工作效率低下。这时,部署自动化运维就可以尽可能安全... 查看详情

ansible的安装配置及常用模块介绍(代码片段)

Ansible的安装、配置及常用模块介绍 ansible安装方式1、ansible安装常用两种方式,yum安装和pip程序安装这里提供二种安装方式,任选一种即可:1、使用yum安装yuminstallepel-release-yyuminstallansible–y2、使用pip(python的包管理模块)安... 查看详情

ansible自动化运维详解ansible的安装部署参数使用清单管理配置文件参数及用户级ansible操作环境构建(代码片段)

文章目录ansible自动化运维详解(一)ansible的安装部署、参数使用、清单管理、配置文件参数及用户级ansible操作环境构建一、ansible的安装部署1.1、ansible简介1.2、实验环境1.3、安装部署ansible二、ansible的基本信息和参数使... 查看详情

ansible安装及原理

介绍两种安装方式yum和编译1)yum安装yuminstallansible-y 2)编译安装 ... ansible原理和架构主要包含6个组件:1)Ansible核心2)Inventory主机清单3)Modules模块4)Playbook剧本5)Plugins插件6)连接插件 1.1)ansible提供了两种方... 查看详情

自动化运维之ansible安装及基本模块应用

ansible简介Ansible可以同时管理Redhat系的Linux,Debian系的Linux,以及Windows主机。管理节点只在执行脚本时与远程主机连接,没有特别的同步机制,所以断电等异常一般不会影响ansbile。2.ansible是新出现的自动化运维工具,基于Python开... 查看详情

ansible概述及各模块应用(代码片段)

ansible概述及各模块应用ansible概述ansible特点ansible工作原理==ansible工具优点==ansible环境部署ansible命令模块command模块cron模块user模块group模块copy模块file模块ping模块service模块模块shell模块script模块yum模块setup模块ansible概... 查看详情

ansible的部署和命令模板

一、ansible的概述1、ansible简介Ansible是一款为类Unix系统开发的自由开源的配置和自动化工具。它用Python写成,类似于saltstack和Puppet,但是有一个不同和优点是我们不需要在节点中安装任何客户端。它使用SSH来和节点进行通信。Ansi... 查看详情

ansible安装与模块管理(代码片段)

Ansible简介Ansible可以同时管理Redhat系的Linux,Debian系的Linux,以及Windows主机。管理节点只在执行脚本时与远程主机连接,没有特别的同步机制,所以断电等异常一般不会影响ansbile。Ansible是新出现的自动化运维工具,基于Python开发... 查看详情

ansible使用一(ansible的安装及ansible常用模块的使用)(代码片段)

1、ansible概述        Ansible是一款基于Python开发的自动化运维工具,它不需要安装客户端,使用SSH进行通信,同时可支持系统账号认证或秘钥认证,也支持windows客户端。Ansible主要组成部分:  ... 查看详情

ansible的入门及常见模块总结实战(代码片段)

Ansible帮助获取:?ansible-doc-s模块名##查看指定模块的帮助信息?ansible-doc-l##查看支持的所有模块常用模块:模块名用途file文件管理,创建文件、目录或链接文件copy/synchronize用于传输文件或目录,对于copy传输慢建议采用synchronize(r... 查看详情

ansible架构原理及工作流程(代码片段)

一、ansible介绍ansible是一种自动化运维工具,基于paramiko模块开发,用于批量执行任务和发布工作,被广泛用于日常运维工作当中.二、ansible架构架构图:ansible核心模块介绍:coremodels:ansible自带的模块,file,shell,copy等custommodels:ansible自... 查看详情

ansible的使用和安装

参考技术A一、ansible入门1.介绍Ansible是一种IT自动化工具。它可以配置系统,部署软件以及协调更高级的IT任务,例如持续部署,滚动更新。Ansible适用于管理企业IT基础设施,从具有少数主机的小规模到数千个实例的企业环境。Ans... 查看详情