【无标题】

1、ansible模块

文件模块:
copy:将本地文件复制到受控主机
file:设置文件的权限和其他属性
lineinfile:确保特定行是否在文件中,也就是说修改文件内容
synchronize:使用rsync同步内容

软件包模块
package:使用操作系统本机的自动检测软件包管理器管理软件包
yum:使用yum软件包管理器管理软件包
apt:使用apt软件包管理器管理软件包
dnf:使用dnf软件包管理器管理软件包
pip:从PyPI管理Python软件包

系统模块
firewalld:使用firewalld管理任意端口和服务
reboot:重新启动计算机
service:管理服务
user:添加、删除和管理用户账户

Net Tools模块
get_url:通过http、https或者ftp下载文件
nmcli:管理网络
uri:与WEB服务交互

常用模块

案例1:user
临时命令使用user模块来确保newbie用户存在于node1.example.com上,并且其UID为4000
[galaxy@server ~]$ ansible server1 -m user -a ‘name=newbie uid=4000 state=present’

案例2:shell
临时命令使用shell模块来删除node1.example.com节点中的用户newbie
ansible server1 -m shell -a ‘userdel -r newbie’

案例3:copy
ansible webserver -m copy -a ‘src=/etc/fstab dest=/var/tmp/fstab’

ansible webserver -m copy -a ‘src=/etc/fstab dest=/var/tmp/fstab group=chenyu owner=chenyu’

案例4:template模块—template模块用法和copy模块用法基本一致,它主要用于复制配置文件
ansible all -m template -a 'src=/usr/share/doc/httpd/httpd-vhosts.conf dest=/etc/httpd/conf.d/httpd-vhosts.conf group=root owner=root mode=0644 ’

案例5:file
修改文件的权限属性和context值
ansible webserver -m file -a ‘path=/var/tmp/fstab mode=g+w mode=o+w group=galaxy owner=galaxy setype=samba_share_t’

案例6:lineinfile
把abc开头的一行换成 bbbbb
ansible webserver -m lineinfile -a ‘dest=/tmp/cy regexp=abc line=bbbbb’

在某一行前面插入一行新数据—insertbefore
ansible webserver -m lineinfile -a ‘dest=/tmp/cy insertbefore=“aa(.*)” line=chenyu’

在某一行后面插入一行新数据—insertafter
ansible webserver -m lineinfile -a ‘dest=/tmp/cy insertafter=“aaaa(.*)” line=bbbb’

删除某一行
ansible webserver -m lineinfile -a ‘dest=/tmp/cy regexp=“aaa(.*)” state=absent’

案例7:yum_repository模块-----配置yum仓库
ansible webserver -m yum_repository -a ‘file=server name=baseos description=rhel8 baseurl=file:///mnt/BaseOS enabled=yes gpgcheck=no’

案例8:yum模块----yum安装与卸载
state:present、installed、latest安装
absent、removed卸载
ansible all -m yum -a ‘name=httpd state=installed’ ----------------安装

ansible all -m yum -a ‘name=httpd state=removed’ ----------------卸载

案例9:service模块

重启httpd服务并设置下次启动生效
ansible all -m service -a ‘name=httpd state=started enabled=yes’

案例10:firewalld模块
允许http流量的传入
ansible all -m firewalld -a ‘service=http permanent=yes state=enabled immediate=yes’

案例11:replace模块
replace模块可以根据我们指定的正则表达式替换文件中的字符串,文件中所有被匹配的字符串都会被替换
参数:
path参数:2.3版本之前只能用dest、destfile、name指定操作文件,2.4版本中仍然可以用这些参数名,也可以用path
regexp参数:必须参数,指定一个python正则表达式,文件中与正则匹配的字符串将会被替换
replace参数:指定最终要替换成的字符串
backup参数:是否在修改文件之前对文件进行备份,最好设置为yes。

将/tmp/cy文件中的“abc”替换成“yyy”
ansible all -m replace -a ‘path=/tmp/cy regexp=“abc” replace=“yyy”’

将/tmp/cy文件中的“yyy”替换成“iii”,且把替换前的/tmp/cy文件备份
ansible all -m replace -a ‘path=/tmp/cy regexp=“yyy” replace=“iii” backup=yes’

案例12:parted模块
新建扩展分区
ansible node1 -m parted -a ‘device=/dev/sda number=4 part_type=extended part_start=46GiB part_end=49.8GiB state=present’

新建逻辑分区ansible node1 -m parted -a ‘device=/dev/sda number=5 part_type=logical part_start=46.1GiB part_end=48.2GiB state=present’

案例13:filesystem—文件系统
ansible node1 -m filesystem -a ‘fstype=xfs dev=/dev/sda5’

案例14:mount—挂载
新建挂载点/common
ansible node1 -m file -a ‘path=/common state=directory’

查看/dev/sda5的UUID
ansible node1 -m shell -a ‘blkid /dev/sda5’

将分区/dev/sda5挂载到/common目录
ansible node1 -m mount -a ‘path=/common src=“UUID=d162b8b9-2326-4ee4-a559-80861461c4f0” fstype=xfs state=mounted’

卸载
ansible node1 -m mount -a ‘path=/common src=“UUID=d162b8b9-2326-4ee4-a559-80861461c4f0” fstype=xfs state=absent’

案例15:lvg—新建卷组
ansible node1 -m lvg -a ‘vg=vg0 pesize=16M pvs=/dev/sda5’

案例16:lvol—新建逻辑卷
ansible node1 -m lvol -a ‘lv=lv0 size=1000M vg=vg0’

在线扩容逻辑卷
ansible node1 -m lvol -a ‘lv=lv0 size=1600M vg=vg0 resizefs=yes’

案例17:sefcontext—修改context值
ansible node1 -m file -a ‘path=/share state=directory’

修改context值
ansible node1 -m sefcontext -a ‘target=“/share(/.*)?” setype=samba_share_t state=present’
应用新的selinux 文件的context值
ansible node1 -m command -a ‘restorecon -irv /share’

案例18:debug
用户输出自定义的信息,类似于echo、print等输出命令。ansible中的debug主要用于输出变量值、表达式值,以及用于when条件判断时。使用方式非常简单

案例19:cron—计划任务模块

ansible node1 -m cron -a ‘name=“shuchu” job=“/bin/echo I AM RHCE” user=root minute=0 hour=14 state=present’

案例20:get_url

语法:ansible node1 -m get_url -a ‘url=需要下载的文件 dest=存放的位置’

部署web服务器

部署yum仓库
[student@ansible ansible]$ ansible node1 -m yum_repository -a 'file=ansible name=guan description=guan1 baseurl=file:///mnt/BaseOS enabled=yes gpgcheck=no'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "repo": "guan",
    "state": "present"
}
[student@ansible ansible]$ ansible node1 -m yum_repository -a 'file=ansible name=gbj description=gbj1 baseurl=file:///mnt/AppStream enabled=yes gpgcheck=no'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "repo": "gbj",
    "state": "present"
}

挂载光盘
[student@ansible ansible]$ ansible node1 -m mount -a 'src=/dev/cdrom path=/mnt fstype=iso9660 state=mounted'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dump": "0",
    "fstab": "/etc/fstab",
    "fstype": "iso9660",
    "name": "/mnt",
    "opts": "defaults",
    "passno": "0",
    "src": "/dev/cdrom"
}
安装httpd
[student@ansible ansible]$ ansible node1 -m yum -a 'name=httpd state=present'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "",
    "rc": 0,
    "results": [
        "Installed: centos-logos-httpd-85.8-2.el8.noarch",
        "Installed: httpd-tools-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64",
        "Installed: apr-1.6.3-12.el8.x86_64",
        "Installed: mod_http2-1.15.7-5.module_el8.6.0+1111+ce6f4ceb.x86_64",
        "Installed: apr-util-1.6.1-6.el8.x86_64",
        "Installed: httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64",
        "Installed: apr-util-bdb-1.6.1-6.el8.x86_64",
        "Installed: apr-util-openssl-1.6.1-6.el8.x86_64",
        "Installed: httpd-filesystem-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.noarch"
    ]
}
[root@node1 ~]# rpm -q httpd
httpd-2.4.37-47.module_el8.6.0+1111+ce6f4ceb.1.x86_64

重启httpd服务并设置下次启动生效
[student@ansible ansible]$ ansible node1 -m service -a 'name=httpd state=started enabled=yes'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "enabled": true,
    "name": "httpd",
    "state": "started",
    "status": {

[root@node1 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor prese>
   Active: active (running) since Mon 2022-10-24 21:10:59 CST; 54s ago
     Docs: man:httpd.service(8)
 Main PID: 36907 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 11070)
   Memory: 22.2M
   CGroup: /system.slice/httpd.service
           ├─36907 /usr/sbin/httpd -DFOREGROUND
           ├─36908 /usr/sbin/httpd -DFOREGROUND
           ├─36909 /usr/sbin/httpd -DFOREGROUND
           ├─36910 /usr/sbin/httpd -DFOREGROUND
           └─36911 /usr/sbin/httpd -DFOREGROUND

Oct 24 21:10:58 node1.example.com systemd[1]: Starting The Apache HTTP Server...
Oct 24 21:10:59 node1.example.com systemd[1]: Started The Apache HTTP Server.
Oct 24 21:10:59 node1.example.com httpd[36907]: Server configured, listening on
讲/var/www/html目录做一个软链接,到/www
[student@ansible ansible]$ ansible node1 -m file -a 'src=/var/www/html/ dest=/www state=link'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "dest": "/www",
    "gid": 0,
    "group": "root",
    "mode": "0777",
    "owner": "root",
    "secontext": "unconfined_u:object_r:root_t:s0",
    "size": 14,
    "src": "/var/www/html/",
    "state": "link",
    "uid": 0
}
[root@node1 ~]# ll -d /www
lrwxrwxrwx. 1 root root 14 Oct 24 21:13 /www -> /var/www/html/

在/www中新建index.html,内容为my name is chenyu
[student@ansible ansible]$ ansible node1 -m shell -a 'echo "my name is guanbingjie" > /www/index.html'
node1 | CHANGED | rc=0 >>

[student@ansible ansible]$ ansible node1 -m shell -a 'cat /www/index.html'
node1 | CHANGED | rc=0 >>
my name is guanbingjie
实现在ansible中能够使用http://node1访问到该网页内容
[student@ansible ansible]$  ansible node1 -m firewalld -a 'service=http permanent=yes state=enabled immediate=yes'
node1 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": true,
    "msg": "Permanent and Non-Permanent(immediate) operation, Changed service http to enabled"
}
[student@ansible ansible]$ curl http://node1
my name is guanbingjie
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值