ansible常用模块使用详解

ansible常用模块使用详解

    • ansible常用模块使用详解
      • 1.ansible常用模块之ping
      • 2. ansible常用模块之command
      • 3. ansible常用模块之raw
      • 4.ansible常用模块之shell
      • 5.ansible常用模块之script
      • 6. ansible常用模块之template
      • 7. ansible常用模块之yum
      • 8. ansible常用模块之copy
      • 9. ansible常用模块之group
      • 10. ansible常用模块之user
      • 11. ansible常用模块之service
      • 12. ansible常用模块之lineinfile
      • 13. ansible常用模块之replace
      • 14. ansible常用模块之file
      • 15. ansible常用模块之fetch
      • 16. ansible常用模块之yum_repository
      • 17. ansible常用模块之parted
      • 18. ansible常用模块之filesystem
      • 19. ansible常用模块之lvg
      • 19. ansible常用模块之notify
      • 19. ansible常用模块之unarchive

ansible常用模块使用详解

1.ansible常用模块之ping

[root@hm1 ~]# ansible all -m ping
192.168.70.138 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

2. ansible常用模块之command

[root@hm1 ~]# ansible 192.168.70.138 -m command -a 'mkdir 123'
[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.
192.168.70.138 | CHANGED | rc=0 >>
[root@hm1 ~]# ansible 192.168.70.138 -m command -a 'ls '
192.168.70.138 | CHANGED | rc=0 >>
123
公共
模板
视频
图片
文档
下载
音乐
桌面
anaconda-ks.cfg
initial-setup-ks.cfg

[root@hm2 ~]# ls
123   模板  图片  下载  桌面             initial-setup-ks.cfg
公共  视频  文档  音乐  anaconda-ks.cfg
支持linux的全部命令
command模块不支持管道符,不支持重定向

3. ansible常用模块之raw

//支持重定向
[root@ansible ~]# ansible 172.16.103.129 -m raw -a 'echo "hello world" > /tmp/test'
172.16.103.129 | SUCCESS | rc=0 >>
Shared connection to 172.16.103.129 closed.


[root@ansible ~]# ansible 172.16.103.129 -a 'cat /tmp/test'
172.16.103.129 | SUCCESS | rc=0 >>
hello world


//支持管道符
[root@ansible ~]# ansible 172.16.103.129 -m raw -a 'cat /tmp/test|grep -Eo hello'
172.16.103.129 | SUCCESS | rc=0 >>
hello
Shared connection to 172.16.103.129 closed.

4.ansible常用模块之shell

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

//查看受控机上的脚本
[root@localhost ~]# ll /scripts/
总用量 4
-rwxr-xr-x 1 root root 52 97 22:49 test.sh


//使用shell模块在受控机上执行受控机上的脚本
[root@ansible ~]# ansible 172.16.103.129 -m shell -a '/bin/bash /scripts/test.sh &> /tmp/test'
172.16.103.129 | SUCCESS | rc=0 >>


[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'cat /tmp/test'
172.16.103.129 | SUCCESS | rc=0 >>
1
2
3
4
5
6
7
8
9
10

5.ansible常用模块之script

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

[root@hm1 ~]# ll /etc/ansible/scripts/
总用量 4
-rw-r--r--. 1 root root 61 9月   8 18:59 a.sh
[root@hm1 ~]# ansible 172.16.103.129 -m script -a '/etc/ansible/scripts/a.sh &>/tmp/a'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "rc": 0,
    "stderr": "Shared connection to 172.16.103.129 closed.\r\n",
    "stderr_lines": [
        "Shared connection to 172.16.103.129 closed."
    ],
    "stdout": "",
    "stdout_lines": []
}


//查看受控机上的/tmp/a文件内容
[root@hm1 ~]# ansible 172.16.103.129 -m shell -a 'cat /tmp/a'
172.16.103.129 | SUCCESS | rc=0 >>
root:x:0:0:root:/root:/bin/bash
....此处省略N行
jerry:x:1000:1000::/home/jerry:/bin/bash

//由此可见确是在受控机上执行了主控机上的脚本,且输出记录到了受控机上。因为此处 \
//的jerry用户是在受控机上才有的用户

6. ansible常用模块之template

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

[root@hm1 ~]# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-SIG-ansible-29.repo  redhat.repo
[root@hm1 ~]# ansible 192.168.70.138 -m template -a 'src=/etc/yum.repos.d/redhat.repo dest=/etc/yum.repos.d/'
192.168.70.138 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "b5dcc866d5312968c494ec06ddd01527163c9f26",
    "dest": "/etc/yum.repos.d/redhat.repo",
    "gid": 0,
    "group": "root",
    "md5sum": "c07c46acc942d53433ccafe16a631548",
    "mode": "0644",
    "owner": "root",
    "secontext": "system_u:object_r:system_conf_t:s0",
    "size": 358,
    "src": "/root/.ansible/tmp/ansible-tmp-1653742555.0658815-317993-54476399021047/source",
    "state": "file",
    "uid": 0
}
[root@hm1 ~]# ansible 192.168.70.138 -m command -a 'ls /etc/yum.repos.d/'
192.168.70.138 | CHANGED | rc=0 >>
CentOS-Base.repo
epel-modular.repo
epel.repo
epel-testing-modular.repo
epel-testing.repo
redhat.repo

##查找客户机,名和IP地址
[root@node1 httpd]# vim templeate.yml 
---
- hosts: webs
  tasks:
    - name: test
      debug:
        msg: >   //列出打印
          The default ipaddress of {{ ansible_facts["fqdn"] }} is
          {{ ansible_facts["default_ipv4"]["address"] }}.
[root@node1 httpd]# ansible-playbook templeate.yml 

PLAY [webs] *******************************************************************************************

TASK [Gathering Facts] ********************************************************************************
ok: [web01.example.com]
ok: [192.168.70.139]

TASK [test] *******************************************************************************************
ok: [web01.example.com] => {
    "msg": "The default ipaddress of 192.168.70.138 is 192.168.70.138.\n"
}
ok: [192.168.70.139] => {
    "msg": "The default ipaddress of hm3 is 192.168.70.139.\n"
}

PLAY RECAP ********************************************************************************************
192.168.70.139             : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
web01.example.com          : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
#可以取消老方法事实命名注入方法,
[root@node1 httpd]# vim ansible.cfg
# inject_facts_as_vars = True //取消注释改成
 inject_facts_as_vars = False  //这样

7. ansible常用模块之yum

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

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

latest:升级软件
present:安装软件*
absent:卸载软件 *
若想使用yum来管理软件,请确保受控机上的yum源无异常。

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


//在ansible主机上使用yum模块在受控机上安装vsftpd
[root@hm1 ~]# ansible 172.16.103.129 -m yum -a 'name=vsftpd state=present'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "msg": "warning: /var/cache/yum/x86_64/7Server/base/packages/vsftpd-3.0.2-22.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY\nImporting GPG key 0xF4A80EB5:\n Userid     : \"CentOS-7 Key (CentOS 7 Official Signing Key) \"\n Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5\n From       : http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7\n",
    "rc": 0,
    "results": [
        "Loaded plugins: product-id, search-disabled-repos, subscription-manager\nThis system is not registered with an entitlement server. You can use subscription-manager to register.\nResolving Dependencies\n--> Running transaction check\n---> Package vsftpd.x86_64 0:3.0.2-22.el7 will be installed\n--> Finished Dependency Resolution\n\nDependencies Resolved\n\n================================================================================\n Package          Arch             Version                 Repository      Size\n================================================================================\nInstalling:\n vsftpd           x86_64           3.0.2-22.el7            base           169 k\n\nTransaction Summary\n================================================================================\nInstall  1 Package\n\nTotal download size: 169 k\nInstalled size: 348 k\nDownloading packages:\nPublic key for vsftpd-3.0.2-22.el7.x86_64.rpm is not installed\nRetrieving key from http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7\nRunning transaction check\nRunning transaction test\nTransaction test succeeded\nRunning transaction\n  Installing : vsftpd-3.0.2-22.el7.x86_64                                   1/1 \n  Verifying  : vsftpd-3.0.2-22.el7.x86_64                                   1/1 \n\nInstalled:\n  vsftpd.x86_64 0:3.0.2-22.el7                                                  \n\nComplete!\n"
    ]
}


//查看受控机上是否安装了vsftpd
[root@hm2 ~]# rpm -qa|grep vsftpd
vsftpd-3.0.2-22.el7.x86_64

8. ansible常用模块之copy

copy模块用于复制文件至远程受控机。
//有一个非常重要的模块 backup: yes 有同名的文件会自动备份
[root@node1 httpd]# ansible web01.example.com -m copy -a "content='hello world' dest=qzl.txt"
web01.example.com | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed",
    "dest": "./qzl.txt",
    "gid": 0,
[root@192 ~]# cat qzl.txt 
hello world[root@192 ~]# 
//copy默认是没有回车的,就会出现这样情况
[root@node1 httpd]# ansible web01.example.com -m copy -a "content='hello world\nhello world\n' dest=qzl.txt"
web01.example.com | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "checksum": "885bdc6c34afe89c4cb474b1cdebc29a5f7a7e40",
    "dest": "./qzl.txt",
    "gid": 0,
    "group": "root",
    "md5sum": "cdb22973649bc8bb322a144796b42163",
    "mode": "0644",
    "owner": "root",
    "size": 24,
    "src": "/root/.ansible/tmp/ansible-tmp-1658243290.1498291-159894-83334016474317/source",
    "state": "file",
    "uid": 0
}
[root@192 ~]# cat qzl.txt 
hello world
hello world


9. ansible常用模块之group

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

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


//删除受控机上的mysql组
[root@ansible ~]# ansible 172.16.103.129 -m group -a 'name=mysql state=absent'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "name": "mysql",
    "state": "absent"
}
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'grep mysql /etc/group'
172.16.103.129 | FAILED | rc=1 >>
non-zero return code

10. ansible常用模块之user

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

//在受控机上添加一个系统用户,用户名为mysql,uid为306,设置其shell为/sbin/nologin,无家目录
[root@ansible ~]# ansible 172.16.103.129 -m user -a 'name=mysql uid=306 system=yes create_home=no shell=/sbin/nologin state=present'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "comment": "",
    "create_home": false,
    "group": 306,
    "home": "/home/mysql",
    "name": "mysql",
    "shell": "/sbin/nologin",
    "state": "present",
    "system": true,
    "uid": 306
}
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'grep mysql /etc/passwd'
172.16.103.129 | SUCCESS | rc=0 >>
mysql:x:306:306::/home/mysql:/sbin/nologin

[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'ls /home'
172.16.103.129 | SUCCESS | rc=0 >>
jerry


//修改mysql用户的uid为366
[root@ansible ~]# ansible 172.16.103.129 -m user -a 'name=mysql uid=366'
172.16.103.129 | SUCCESS => {
    "append": false,
    "changed": true,
    "comment": "",
    "group": 306,
    "home": "/home/mysql",
    "move_home": false,
    "name": "mysql",
    "shell": "/sbin/nologin",
    "state": "present",
    "uid": 366
}
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'grep mysql /etc/passwd'
172.16.103.129 | SUCCESS | rc=0 >>
mysql:x:366:306::/home/mysql:/sbin/nologin


//删除受控机上的mysql用户
[root@ansible ~]# ansible 172.16.103.129 -m user -a 'name=mysql state=absent'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "force": false,
    "name": "mysql",
    "remove": false,
    "state": "absent"
}
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'grep mysql /etc/passwd'
172.16.103.129 | FAILED | rc=1 >>
non-zero return code
##设置可以登录的加密用户
[root@node1 httpd]# ansible web01.example.com -m user -a "name=jjj password={{'abc'|password_hash('sha512')}}"
//sha512:标准算法  ,  加密的格式
web01.example.com | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "comment": "",
    "create_home": true,
    "group": 1001,
    "home": "/home/jjj",
    "name": "jjj",
    "password": "NOT_LOGGING_PASSWORD",
    "shell": "/bin/bash",
    "state": "present",
    "system": false,
    "uid": 1001
}
[root@192 ~]# id jjj
uid=1001(jjj) gid=1001(jjj)=1001(jjj)
[root@192 ~]# grep jjj /etc/shadow
jjj:$6$U8Q2UEP0mD5ATjfu$oHKV9MsCwWGD/jUr28MuYnDYpp7UODCwFdUwUKI3v3WUFTMurgD8wUqrnE/mUYaSV9EVTxEK73.PkDq951DQ/1:19193:0:99999:7:::

11. ansible常用模块之service

service模块用于管理受控机上的服务。
started   开启服务
stopped   关闭服务
restarted  重启服务
enabled=yes 或 no 设置开机自启或关闭
//查看受控机上的vsftpd服务是否启动
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'systemctl is-active vsftpd'
172.16.103.129 | FAILED | rc=3 >>
unknownnon-zero return code

//启动受控机上的vsftpd服务
[root@ansible ~]# ansible 172.16.103.129 -m service -a 'name=vsftpd state=started'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "name": "vsftpd",
    "state": "started",
    "status": {
        "ActiveEnterTimestampMonotonic": "0",
        ....此处省略N行
}

//查看受控机上的vsftpd服务是否启动
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'systemctl is-active vsftpd'
172.16.103.129 | SUCCESS | rc=0 >>
active


//查看受控机上的vsftpd服务是否开机自动启动
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'systemctl is-enabled vsftpd'
172.16.103.129 | FAILED | rc=1 >>
disablednon-zero return code

//设置受控机上的vsftpd服务开机自动启动
[root@ansible ~]# ansible 172.16.103.129 -m service -a 'name=vsftpd enabled=yes'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "enabled": true,
    "name": "vsftpd",
    "status": {
        "ActiveEnterTimestamp": "六 2018-09-08 00:02:39 EDT",
        ....此处省略N行
}

//查看受控机上的vsftpd服务是否开机自动启动
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'systemctl is-enabled vsftpd'
172.16.103.129 | SUCCESS | rc=0 >>
enabled


//停止受控机上的vsftpd服务
[root@ansible ~]# ansible 172.16.103.129 -m service -a 'name=vsftpd state=stopped'
172.16.103.129 | SUCCESS => {
    "changed": true,
    "name": "vsftpd",
    "state": "stopped",
    "status": {
        "ActiveEnterTimestamp": "六 2018-09-08 00:02:39 EDT",
        ....此处省略N行
}
[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'systemctl is-active vsftpd'
172.16.103.129 | FAILED | rc=3 >>
inactivenon-zero return code

[root@ansible ~]# ansible 172.16.103.129 -m shell -a 'ss -antl'
172.16.103.129 | SUCCESS | rc=0 >>
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port
LISTEN     0      128          *:22                       *:*
LISTEN     0      100    127.0.0.1:25                       *:*
LISTEN     0      128         :::22                      :::*
LISTEN     0      100        ::1:25                      :::*

12. ansible常用模块之lineinfile

lineinfile是替换单个文件整行内容

//在jjyy的下一行添加qzl
[root@localhost ~]# cat 1
jjyy aksdfjsdff
hello world
[root@hm1 mysql]# ansible all -m lineinfile -a "path=/root/1 line='qzl' insertafter='jjyy'"
web01.example.com | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line added"
}
[root@localhost ~]# cat 1
jjyy aksdfjsdff
qzl
hello world
//替换qzl,为ni hao
[root@localhost ~]# cat 1
jjyy aksdfjsdff
qzl
hello world

[root@hm1 mysql]# ansible all -m lineinfile -a "path=/root/1 regexp='qzl' line='ni hao'"
web01.example.com | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "backup": "",
    "changed": true,
    "msg": "line replaced"
}
[root@localhost ~]# cat 1
jjyy aksdfjsdff
ni hao
hello world

13. ansible常用模块之replace

replace模块替换关键字

[root@localhost ~]# cat 1
jjyy aksdfjsdff
qzl
hello world
[root@hm1 mysql]# ansible all -m replace -a "path=/root/1 \
> regexp=qzl replace=jjjjjjjjjjjjjjjj "
web01.example.com | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "1 replacements made"
}

[root@localhost ~]# cat 1
jjyy aksdfjsdff
jjjjjjjjjjjjjjjj aksdf;laskdf;
hello world

14. ansible常用模块之file

[root@node1 httpd]# ansible web01.example.com -m file -a "path=/root/qzl.txt state=touch"
web01.example.com | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/root/qzl.txt",
    "gid": 0,
    "group": "root",
    "mode": "0644",
    "owner": "root",
    "size": 0,
    "state": "file",
    "uid": 0
}
//新建一个文件
[root@node1 httpd]# 
[root@192 ~]# ls
公共  模板  视频  图片  文档  下载  音乐  桌面  anaconda-ks.cfg  initial-setup-ks.cfg  qzl.txt

[root@node1 httpd]# ansible web01.example.com -m file -a "path=/root/ll state=directory owner=sshd group=adm mode=777"
web01.example.com | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "gid": 4,
    "group": "adm",
    "mode": "0777",
    "owner": "sshd",
    "path": "/root/ll",
    "size": 6,
    "state": "directory",
    "uid": 74
}
[root@192 ~]# ls ~
公共  视频  文档  音乐  anaconda-ks.cfg       ll 
[root@192 ~]# ll -d ll
drwxrwxrwx 2 sshd adm 6 719 22:44 ll

//新建一个目录,改了权限
[root@node1 httpd]# ansible web01.example.com -m file -a "path=/root/ll state=absent"web01.example.com | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "path": "/root/ll",
    "state": "absent"
}
//删除目录

[root@node1 httpd]# ansible web01.example.com -m file -a "src=/root/qzl path=/root/lll state=link"
web01.example.com | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/root/lll",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "size": 9,
    "src": "/root/qzl",
    "state": "link",
    "uid": 0
}
[root@192 ~]# ll -d lll
lrwxrwxrwx 1 root root 9 719 22:54 lll -> /root/qzl
//设置个软链接

15. ansible常用模块之fetch

#将客户端的文件拷贝到控制端
[root@node1 httpd]# ansible web01.example.com -m fetch -a "src=/root/qzl.txt dest=/root/~"
web01.example.com | CHANGED => {
    "changed": true,
    "checksum": "885bdc6c34afe89c4cb474b1cdebc29a5f7a7e40",
    "dest": "/root/~/web01.example.com/root/qzl.txt",
    "md5sum": "cdb22973649bc8bb322a144796b42163",
    "remote_checksum": "885bdc6c34afe89c4cb474b1cdebc29a5f7a7e40",
    "remote_md5sum": null
}
[root@node1 ~]# ls
'~'     模板   图片   下载   桌面              fhdsfds   index.html             mysql
 公共   视频   文档   音乐   anaconda-ks.cfg   httpd     initial-setup-ks.cfg   pass
[root@node1 ~]# cd '~'
[root@node1 ~]# ls
web01.example.com
[root@node1 ~]# cd web01.example.com/
[root@node1 web01.example.com]# ls
root
[root@node1 web01.example.com]# cd root
[root@node1 root]# ls
qzl.txt
[root@node1 root]# cat qzl.txt 
hello world
hello world
//如果不指定文件,就会自己创建和客户端同名的文件目录

16. ansible常用模块之yum_repository

创建或配置yum源

17. ansible常用模块之parted

磁盘分区模块

18. ansible常用模块之filesystem

分区的格式化

19. ansible常用模块之lvg

[root@node2 ~]# parted /dev/sdb mklabel gpt
信息: You may need to update /etc/fstab.

[root@node2 ~]# parted /dev/sdb mkpart primary 50% 100%
信息: You may need to update /etc/fstab.

[root@node2 ~]# lsblk                                                     
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda             8:0    0   60G  0 disk 
├─sda1          8:1    0    1G  0 part /boot
└─sda2          8:2    0   59G  0 part 
  ├─rhel-root 253:0    0   37G  0 lvm  /
  ├─rhel-swap 253:1    0    4G  0 lvm  [SWAP]
  └─rhel-home 253:2    0 18.1G  0 lvm  /home
sdb             8:16   0   20G  0 disk 
└─sdb1          8:17   0   10G  0 part 
sr0            11:0    1  7.9G  0 rom  /run/media/root/RHEL-8-2-0-BaseOS-x86_64
[root@node2 ~]# parted /dev/sdb mkpart primary 1 50% 
信息: You may need to update /etc/fstab.

[root@node2 ~]# lsblk
NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda             8:0    0   60G  0 disk 
├─sda1          8:1    0    1G  0 part /boot
└─sda2          8:2    0   59G  0 part 
  ├─rhel-root 253:0    0   37G  0 lvm  /
  ├─rhel-swap 253:1    0    4G  0 lvm  [SWAP]
  └─rhel-home 253:2    0 18.1G  0 lvm  /home
sdb             8:16   0   20G  0 disk 
├─sdb1          8:17   0   10G  0 part 
└─sdb2          8:18   0   10G  0 part 
sr0            11:0    1  7.9G  0 rom  /run/media/root/RHEL-8-2-0-BaseOS-x86_64
//准备工作创建俩个分区
[root@node1 httpd]# ansible web01.example.com -m yum -a "name=lvm2"
web01.example.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "msg": "Nothing to do",
    "rc": 0,
    "results": []
}
//目标主机是要有lvm2的软件包
[root@node1 httpd]# ansible web01.example.com -m lvg -a "vg=myvg pvs=/dev/sdb1"
//目表主机创建一个卷组
[root@node1 httpd]# ansible web01.example.com -m lvg -a "vg=myvg pvs=/dev/sdb1,/dev/sdb2"
//扩容,把俩个分区加给卷组

19. ansible常用模块之notify

触发器notify,触发项handlers
---
- hosts: all
  tasks:
  - name: kkkk
    lineinfile:
      path: /etc/httpd/conf/httpd.conf
      regexp: "Listen 82"
      line: "Listen 80"
      state: present
    notify:
      - restart apache  //以上进行模块更改触发

  handlers:    //要和tasks 同一位置
    - name: restart apache
      service:
        name: httpd
        state: restarted   //触发这个
//处理程序是在所有任务完成之后才会执行,handlers任务是按他的顺序来

19. ansible常用模块之unarchive

解压包

你可能感兴趣的:(linux,运维,服务器)