linux上ansidle常用模块的使用(二)

注释:接上一章

  • 磁盘管理模块
  • 用户管理模块
  • 防火墙管理模块
29.11 使用lvol模块管理逻辑卷
卷组创建好之后就要创建逻辑卷了,管理逻辑卷的模块是Ivol,lvol模块常见的参数包含
以下几个。
(1)vg:用于指定在哪个卷组上划分逻辑卷。
(2)lv:用于指定逻辑卷的名称。
(3)size:用于指定逻辑卷的大小。
(4)state:此参数的值如下。present :用于创建逻辑卷。absent :用于删除逻辑卷。
练习1:在up的卷组 vg0上,创建大小为1G、名称为lv0的逻辑卷。
先判断up上是否存在逻辑卷,命令如下。
[root@pp ~]# ansible up -m shell -a "lvs"
up | CHANGED | rc=0 >>
                                                 
[root@pp ~]# 
可以看到,不存在任何逻辑卷。下面开始创建逻辑卷,命令如下。
[root@pp ~]# ansible up -m lvol -a "vg=vg0 lv=lv0 size=1G"
up | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": ""
}
[root@pp ~]# 
验证,命令如下
[root@pp ~]# ansible up -m shell -a "lvs"
\up | CHANGED | rc=0 >>                                                 
  lv0  vg0  -wi-a-----   1.00g                                                    
[root@pp ~]# 
可以看到,此逻辑卷已经创建出来了。
练习2:在 up上删除逻辑卷/dev/vg0/lv0,命令如下。
[root@pp ~]# ansible up -m lvol -a "vg=vg0 lv=lvo state=absent force=yes"
up | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": ""
}
[root@pp ~]# 
29.12 使用firewalld模块管理防火墙
在ansible中可以通过firewalld模块对防火墙进行管理,firewalld模块常见的参数包括以
下几个。
(1)service:开放哪个服务。
(2)port:开放哪个端口,用法为port=80/tcp。
(3)permanent=yes:设置永久生效,不存在默认值。
(4)immediate=yes:设置当前生效,默认为不生效。 (5)state:此参数的值如下。
①enabled:用于创建规则。
②disabled:用于删除规则。
(6)rich_rule:富规则。
练习1:在up上开放服务http,命令如下。
[root@pp ~]# ansible up -m firewalld -a "service=http immediate=yes permanent=yes state=enabled"
up | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "Permanent and Non-Permanent(immediate) operation"
}
[root@pp ~]# 
验证,命令如下。
[root@pp ~]# ansible up -m shell -a "firewall-cmd --list-all"
up | CHANGED | rc=0 >>
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens160
  sources: 
  services: cockpit dhcpv6-client http ssh
  ports: 
  protocols: 
  forward: no
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
[root@pp ~]# 
练习2:在up上配置防火墙,允许tcp端口808通过,命令如下。
[root@pp ~]# ansible up -m firewalld -a "port=808/tcp immediate=yes permanent=yes state=enabled"
up | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Permanent and Non-Permanent(immediate) operation, Changed port 808/tcp to enabled"
}
[root@pp ~]# 
练习3:在up上配置防火墙,删除开放的端口808和服务http,命令如下。
[root@pp ~]# ansible up -m firewalld -a "port=808/tcp immediate=yes permanent=yes state=disabled"
up | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Permanent and Non-Permanent(immediate) operation, Changed port 808/tcp to disabled"
}
[root@pp ~]# 
29.13 替换模块replace
平时写shell脚本时,要替换文件的内容,可以直接使用vim或sed命令来进行替换操作。
在 ansible中也有相关的替换模块:replace和 lineinfile,这里先讲replace模块的使用。
replace模块常见的参数包括以下几个。
(1)path:指明编辑的文件。
(2)regexp:正则表达式,指定要替换哪块内容。
(3)replace:替换后的字符。
练习1:把up 上 /opt/aa.txt中开头为aa那行的内容替换为xx=666。
在server2的lopt目录中创建aa.txt,内容如下。
[root@up opt]# cat aa.txt 
aa=111
bb=222
[root@up opt]# 
在ansible主机上执行replace模块,命令如下。
[root@pp ~]# ansible up -m replace -a "path=/opt/aa.txt regexp=^aa replace=xx=666"
up | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "1 replacements made"
}
[root@pp ~]# 
这里的意思是把server2 上/opt/aa.txt这个文件中行开头是aa的字符替换成xx=666。记
住,这里只是对regexp表示的字符进行替换,替换之后的内容如下。
[root@up opt]# cat aa.txt 
xx=666=111
bb=222
[root@up opt]# 
29.14 替换模块lineinfile
lineinfile模块的用法与replace基本一致,也是用于替换的,常见的参数包括以下几个。
(1)path:指明编辑的文件。
(2)regexp:正则表达式。
(3)line:替换后的字符。
练习:把server2 上/opt/bb.txt中开头为aa=111那行的内容替换为xx=666。
在server2上创建文件/opt/bb.txt,内容如下。
[root@up opt]# cat aa.txt 
xx=666=111
bb=222
在ansible主机上执行lineinfile模块,命令如下。
[root@pp ~]# ansible up -m lineinfile -a "path=/opt/bb.txt regexp=^aa line=xx=666"
up | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
[root@pp ~]# 
这里的意思是把path所指定的文件/opt/bb.txt,regexp后面跟的^aa,即以aa开头的行
(需要注意的是,这里和 replace模块有区别),替换成xx=666,运行结果如下。
[root@up opt]# cat bb.txt 
xx=666
bb=222
[root@up opt]# 
总结:replace是对字符进行替换,lineinfile是对行进行替换,如果replace想对行进行替
换,在regexp后面必须写上正则表达式来表示一整行内容。
29.15 打印模块debug
debug模块一般用于打印提示信息,类似于shell 中的echo命令,其他语言如Python等
中的print,其常见的参数包括以下几个。
(1)msg:后面跟具体内容。
(2)var:后面跟变量。
注意
var 和 msg 不可以同时使用。
练习:在server2上打印“111”,命令如下。
[root@pp ~]# ansible up -m debug -a "msg='hello' anible"
29.16 使用script 模块在远端执行脚本
如果在本地写了一个脚本,想在所有被管理节点上执行,没有必要事先把脚本分发到被管
理机器上,使用script模块即可快速实现。
先写一个简单的脚本test1.sh 用于显示主机名,内容如下。
[root@pp ~]# cat test1.sh 
#!/bin/bash
hostname
[root@pp ~]# 
[root@pp ~]# chmod +x test1.sh 
下面在db主机组上执行,命令如下。
[root@pp ~]# ansible db -m script -a "./test1.sh"
up | CHANGED => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to up closed.\r\n",
    "stderr_lines": [
        "Shared connection to up closed."
    ],
    "stdout": "up\r\n",
    "stdout_lines": [
        "up"
    ]
}
[root@pp ~]# 

这样本地脚本直接在所有被管理主机上执行了。

29.17 使用group模块对组进行管理
如果对系统的组进行管理,那么可以使用group模块。group模块常见的参数包括以下几
个。
(1)name:指定组名。
(2)state:此参数的值如下。
①present:用于创建组。
②absent:用于删除组。
下面在up上创建组 group1。
先判断up上是否存在group1,命令如下。
[root@up ~]# grep group1 /etc/group
[root@up ~]# 
没有任何输出,说明server2上是没有group1这个组的。下面创建组group1,命令如
下。
[root@pp ~]# ansible up -m group -a "name=group1 state=present"
up | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 1002,
    "name": "group1",
    "state": "present",
    "system": false
}
[root@pp ~]# 
然后切换到up上进行验证,命令如下。
[root@up ~]# grep group1 /etc/group
group1:x:1002:
[root@up ~]# 
删除这个组,命令如下。
[root@pp ~]# ansible up -m group -a "name=group1 state=absent"
up | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "name": "group1",
    "state": "absent"
}
[root@pp ~]#
29.18 使用user模块对用户进行管理
对用户的管理可以使用user模块,对于user模块来说,常见的参数包括以下几个。
(1)name:指定用户名。
(2)comment:指定注释信息。
(3)group:指定用户的主组。
(4)groups:指定用户的附属组。
(5)password:指定密码,但是必须对密码进行加密。
(6)state:此参数的值如下。
①present:用于创建用户。
②absent:用于删除用户。
下面创建一个用户lisi,命令如下。
[root@pp ~]# ansible up -m user -a "name=list group=root password={{'haha001' | password_hash('sha512')}} state=present"
up | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 0,
    "home": "/home/list",
    "name": "list",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1002
}
[root@pp ~]# 
这里password=({'haha001'| password hash('sha512')}}的意思是,用password hash
函数调用sha512这个哈希算法对字符串 haha001进行加密。
到server2 上验证,因为root用su命令切换到任何用户都不需要密码,所以这里先切换到
lduan用户,然后再切换到lisi用户,测试密码是不是正确。
[blab@up ~]$ su - list
密码:
[list@up ~]$ 
可以看到,用户的密码是haha001。
下面把lisi用户删除,命令如下。
[root@pp ~]# ansible up -m user -a "name=list state=absent remove=yes"
这里remove=yes的意思类似于userdel 中的-r选项,删除用户的同时把家目录也删除。
29.19 使用get url模块下载文件
如果想从服务器上下载到被管理机器上,需要使用到get_url模块。get_url模块常见的参
数包括以下几个。
(1)url:指定文件的URL 连接。
(2)dest:指定存储在哪里。
例如,现在要把ftp://ftp.rhce.cc/auto/web.tar.gz下载到server2的/opt目录中,命令如
下。
[root@pp ~]# ansible up -m get_url -a "url=ftp://ftp.rhce.cc/auto/web.tar.gz dest=/opt/"
up | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum_dest": null,
    "checksum_src": "0453d8c9e9ce9bda838b659b901b74a2e709fb15",
    "dest": "/opt/web.tar.gz",
    "elapsed": 5,
    "gid": 0,
    "group": "root",
    "md5sum": "41c019324f7b8160ce6944102a755de8",
    "mode": "0644",
    "msg": "OK (1406 bytes)",
    "owner": "root",
    "secontext": "system_u:object_r:usr_t:s0",
    "size": 1406,
    "src": "/home/blab/.ansible/tmp/ansible-tmp-1702950653.9560287-82670-130978094325115/tmppx0w93_4",
    "state": "file",
    "status_code": null,
    "uid": 0,
    "url": "ftp://ftp.rhce.cc/auto/web.tar.gz"
}
[root@pp ~]# 
然后到up上验证,命令如下。
[root@up ~]# ls /opt/
aa.txt  bb.txt  web.tar.gz
[root@up ~]# 
29.20 使用setup模块获取被管理主机信息
如果想获取被管理主机的系统信息,可以使用setup模块。下面获取up上的信息,命
令如下。
[root@pp ~]# ansible up -m setup
up | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "192.168.248.23",
            "192.168.122.1"
        ],
        "ansible_all_ipv6_addresses": [
            "fe80::20c:29ff:fe20:dada"
        ],
        "ansible_apparmor": {
setup中所获取的变量叫作fact变量,这里都是以key:value的格式输出,大致结构如下。
1 键1:值
2 键2:{
3 子键a: 值a
4 子键b: 值b
5 ...
6 }
如果想获取“键1”的值,可以通过参数“filter=键”或“filter=键.子键”来过滤。例如,
要获取up所在机器BIOS的版本,可以通过键值ansible_bios_version来获取,命令如
下。
[root@pp ~]# ansible up -m setup -a "filter=ansible_bios_version"
up | SUCCESS => {
    "ansible_facts": {
        "ansible_bios_version": "6.00",
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
[root@pp ~]# 
如果想获取up上ipv4的所有信息,可以通过键值ansible default ipv4来获取,命令如
下。
[root@pp ~]# ansible up -m setup -a "filter=ansible_default_ipv4"
up | SUCCESS => {
    "ansible_facts": {
        "ansible_default_ipv4": {
            "address": "192.168.248.23",
            "alias": "ens160",
            "broadcast": "192.168.248.255",
            "gateway": "192.168.248.2",
            "interface": "ens160",
            "macaddress": "00:0c:29:20:da:da",
            "mtu": 1500,
            "netmask": "255.255.255.0",
            "network": "192.168.248.0",
            "type": "ether"
        },
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
[root@pp ~]# 
如果仅仅想获取IP地址信息,其他网络信息不需要,可以通过 ansible_default_ipv4的子
键来获取,命令如下。
[root@pp ~]# ansible up -m setup -a "filter=ansible_default_ipv4.address"
up | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false
}
[root@pp ~]# 
不过在命令行中如果filter含有子键,结果是不会显示的,所以上面的命令没有看到IP.不
过如果把这个键写入playbook,是会显示值的,关于 playbook后面会讲。
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值