四、ansible模块-命令类型模块

64836人浏览 / 1431人评论

一、ping模块

[root@m01-ansible ansible]# ansible client -m ping
172.168.2.6 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "ping": "pong"
}
二、command命令模块

[root@m01-ansible ansible]# ansible client -m command -a "ip a s eth0"
172.168.2.6 | CHANGED | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:e8:d9:ad brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.3/24 brd 10.0.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fee8:d9ad/64 scope link 
       valid_lft forever preferred_lft forever

[root@m01-ansible ansible]# ansible client -m command -a "hostname -I"
172.168.2.6 | CHANGED | rc=0 >>
10.0.0.3 172.168.2.6 
 

三、chdir

ansible client -m command -a "chdir=/tmp touch oldboy.txt"

chdir --- 在执行命令前切换目录

creates --- 指定目录如果存在, 跳过后续操作过程

removes --- 指定目录如果不存在, 跳过后续操作过程

 

四、shell

ansible client -m shell -a "touch oldboy.txt"

执行脚本:

第一个历程: 编写脚本

第二个历程: 将脚本传输被管理主机上

第三个历程: 进行脚本文件授权操作

第四个历程: 运行ansible命令

ansible 172.16.1.41 -m shell -a "/server/scripts/yum.sh"

 

五、script --- 远程执行脚本文件

ansible 172.16.1.41 -m script -a "/server/scripts/yum.sh"

 

六、copy

copy --- 批量分发文件信息/ 批量备份文件信息

批量分发文件:

ansible all -m copy -a "src=/oldboy/oldbaby.txt dest=/tmp mode=666 owner=oldboy group=oldboy"

批量备份源文件:

ansible all -m copy -a "src=/oldboy/oldbaby.txt dest=/tmp mode=666 owner=oldboy group=oldboy backup=yes"

批量还原文件信息:

ansible all -m copy -a "src=/tmp/oldbaby.txt.bak dest=/tmp/oldbaby.txt mode=666 owner=oldboy group=oldboy remote_src=yes"

 

批量创建文件, 文件中生成指定信息

ansible all -m copy -a "content=rsync_backup:oldboy123 dest=/etc/rsync.password mode=600"

批量分发文件会自动创建目录信息

ansible all -m copy -a "src=/oldboy/oldbaby.txt dest=/oldbaby/oldboy01/oldboy02/oldboy03/ mode=600"

 

七、file 

file --- 修改文件属性信息

修改文件权限属性信息

ansible all -m file -a "path=/tmp/oldbaby.txt mode=644 owner=root group=root"

批量创建目录信息

ansible all -m file -a "path=/oldboy50/ state=directory"

批量创建文件信息

ansible all -m file -a "path=/oldboy50/ state=touch"

批量创建硬链接文件

ansible all -m file -a "src=/oldboy50/oldboy.txt path=/oldboy50/oldboy_hard.txt state=hard"

 

批量创建软链接文件

ansible all -m file -a "src=/oldboy50/oldboy.txt path=/oldboy50/oldboy_soft.txt state=link"

批量删除文件信息

ansible all -m file -a "path=/oldboy50/ state=absent"

 

八、fetch

fetch --- 批量拉取数据模块

批量拉取文件数据信息

ansible all -m fetch -a "src=/tmp/ dest=/oldgirl"

 

九、yum

yum --- 批量安装软件

ansible all -m yum -a "name=iftop state=installed"

 

十、service

service --- 批量管理服务信息

ansible all -m service -a "name=crond state=started enabled=yes"

 

十一、cron

cron --- 批量设置定时任务

* * * * * 执行任务信息

minute hour day month weekday job='ntpdate ntp1.aliyun.com &>/dev/null'

 

批量设置定时任务:

ansible all -m cron -a "name='date sync01' minute=0 hour=2 job='ntpdate ntp1.aliyun.com &>/dev/null'"

批量删除定时任务:

ansible all -m cron -a "name='date sync01' state=absent"

批量注释定时任务:

ansible all -m cron -a "name='date sync01' minute=0 hour=2 job='ntpdate ntp1.aliyun.com &>/dev/null' disabled=yes"

 

十二、补充

补充: ansible帮助信息查看方法

ansible-doc -l --- 列出模块信息

ansible-doc -s 模块名 --- 列出模块参数信息

ansible-doc 模块名 --- 列出模块详细信息

绿色: 查询操作|操作没有执行

黄色: 操作被执行了

紫色: 警告建议信息

红色: 报错

蓝色: 显示执行过程

4) 用户模块 user group

user --- 批量创建用户

批量创建用户信息

ansible all -m user -a "name=alex"

ansible all -m user -a "name=alex01 uid=4000"

ansible all -m user -a "name=alex02 uid=4001 group=alex"

ansible all -m user -a "name=alex03 shell=/sbin/nologin create_home=no"

 

批量删除用户信息

ansible all -m user -a "name=alex state=absent"

ansible 172.16.1.41 -m user -a "name=alex01 state=absent remove=yes"

批量创建用户设置密码

ansible 172.16.1.41 -m user -a 'name=alex01 password=$6$01$snUJALRNL0on5KrtUPpluyfigSNzXxOxDgcdY5djxcmw45C/tKvB./53OhOXLbTqg/D8XadSbcpYqgJTzJ2bH0'

如何创建密文密码:

方法一:

[root@oldboy-m01 ~]# ansible all -i localhost, -m debug -a "msg={{ 'oldboy123' | password_hash('sha512', '01') }}"

localhost | SUCCESS => {

"msg": "$6$01$snUJALRNL0on5KrtUPpluyfigSNzXxOxDgcdY5djxcmw45C/tKvB./53OhOXLbTqg/D8XadSbcpYqgJTzJ2bH0"

}

 

方法二:

yum install -y python-pip

pip install passlib

优化pip源方法

pypi

配置方法

在文件

~/.pip/pip.conf

中添加或修改:

[global]

index-url = https://mirrors.aliyun.com/pypi/simple/

[install]

trusted-host=mirrors.aliyun.com

[root@oldboy-m01 ~]# python -c "from passlib.hash import sha512_crypt; import getpass; print(sha512_crypt.using(rounds=5000).hash(getpass.getpass()))"

Password:

$6$YEBaHethHNl2oEUU$ejYMZcDtlHhgWBMuKevFSf7CpGvqVbBD2qMwB4lrdJ3IW2awZafxnDJtvCuEEnbT1ftNbQ46IHS8IcVw1kmO30

 

5) 系统挂载模块 mount

批量挂载操作:

ansible 172.16.1.41 -m mount -a "src=172.16.1.31:/data/blog path=/mnt fstype=nfs state=mounted"

批量卸载操作:

ansible 172.16.1.41 -m mount -a "src=172.16.1.31:/data/blog path=/mnt fstype=nfs state=absent"

present 在fstab文件中设置自动挂载操作 永久挂载操作

mounted 在fstab文件中设置自动挂载操作 永久挂载操作 ******

在命令行界面进行挂载操作 临时挂载操作

 

absent 将fstab文件中设置信息删除 永久卸载操作 ******

在命令行界面进行卸载操作(尝试删除挂载点目录) 临时卸载操作

unmounted 在命令行界面进行卸载操作 临时卸载操作

09. ansible剧本编写功能:

可以将多个模块功能进行整合, 一键化部署服务

PYyaml -- 语法规范???

1) 缩进规范 需要输入两个空格

hosts: 主机信息

tasks:

name:

2) 字典规范 冒号输入规范: 冒号后面需要加一个空格

hosts: 172.16.1.31

tasks:

name: xxx

3) 列表规范 短横线规范: 短横线后面需要加空格

- hosts: 172.16.1.31

tasks:

- copy: xxx

- yum: xxx

- hosts: 172.16.1.41

tasks:

shell:

一键化部署rsync服务剧本:

第一步: 定义主机清单:

[rsync_server]

172.16.1.41

[rsync_server:vars]

ansible_user=root

ansible_password=654321

ansible_port=22

[rsync_client]

172.16.1.31

172.16.1.7

[rsync_client:vars]

ansible_user=root

ansible_password=654321

ansible_port=22

第二个步骤: rsync软件部署过程

rsync服务端部署过程:

1) 安装rsync软件

ansible rsync_server -m yum -a "name=rsync state=installed"

2) 编写rsync配置文件

在管理先编写一个rsyncd.conf文件, 将文件进行分发

ansible rsync_server -m copy -a "src=/etc/ansible/conf/rsyncd.conf dest=/etc/ backup=yes"

3) 创建rsync虚拟用户

ansible rsync_server -m user -a "name=rsync shell=/sbin/nologin create_home=no"

4) 创建备份目录,并进行授权

ansible rsync_server -m file -a "path=/backup state=directory owner=rsync group=rsync"

5) 创建密码文件,并授权

ansible rsync_server -m copy -a "content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600"

6) 启动rsync服务

ansible rsync_server -m service -a "name=rsyncd state=started enabled=yes"

rsync客户端部署过程:

1) 安装rsync

ansible rsync_client -m yum -a "name=rsync state=installed"

2) 创建密码文件, 并授权

ansible rsync_clientr -m copy -a "content='oldboy123' dest=/etc/rsync.password mode=600"

vim /etc/ansible/playbook/rsync_auto.yaml

- hosts: rsync_server

tasks:

- name: 01:install rsync

yum: name=rsync state=installed

- name: 02:push rsync conf file

copy: src=/etc/ansible/conf/rsyncd.conf dest=/etc/ backup=yes

- name: 03:create user rsync

user: name=rsync shell=/sbin/nologin create_home=no

- name: 04:create backup dir

file: path=/backup state=directory owner=rsync group=rsync

- name: 05:create password file

copy: content='rsync_backup:oldboy123' dest=/etc/rsync.password mode=600

- name: 06:boot rsync server

service: name=rsyncd state=started enabled=yes

- hosts: rsync_client

tasks:

- name: 01:install rsync

yum: name=rsync state=installed

- name: 02:create password file

copy: content='oldboy123' dest=/etc/rsync.password mode=600

第三个历程: 剧本执行过程:

语法检查:

ansible-playbook --syntax-check 剧本信息

模拟执行:

ansible-playbook -C 剧本信息

真正执行:

ansible-playbook 剧本信息

全部评论

2020-01-06 20:52
angelina
2020-01-06 20:52
angelina
2020-01-06 20:52
a ;ping -c 3 314162f1-2951-4ca4-8917-7c63038a1bc9.scan.heibaidiguo.cn
2020-01-06 20:52
"|ping -c 3 314162f1-2951-4ca4-8917-7c63038a1bc9.scan.heibaidiguo.cn|"
2020-01-06 20:52
a
2020-01-06 20:52
a ||ping -c 3 314162f1-2951-4ca4-8917-7c63038a1bc9.scan.heibaidiguo.cn
2020-01-06 20:52
a ||ping -n 3 314162f1-2951-4ca4-8917-7c63038a1bc9.scan.heibaidiguo.cn
2020-01-06 20:52
';ping -n 3 314162f1-2951-4ca4-8917-7c63038a1bc9.scan.heibaidiguo.cn;'
2020-01-06 20:52
a |ping -c 3 314162f1-2951-4ca4-8917-7c63038a1bc9.scan.heibaidiguo.cn
2020-01-06 20:52
"|ping -n 3 314162f1-2951-4ca4-8917-7c63038a1bc9.scan.heibaidiguo.cn|"