ansible3

1.cron模块
定时模块

[root@bastion ansible]# ansible prod -m shell -a 'crontab -l'
172.25.250.11 | FAILED | rc=1 >>
no crontab for rootnon-zero return code
172.25.250.12 | FAILED | rc=1 >>
no crontab for rootnon-zero return code

受控主机没有crontab任务

[root@bastion ansible]# ansible prod -m cron -a "job=date name=qihao minute=*/2"

两分钟执行一次date命令

[root@bastion ansible]# ansible prod -m shell -a 'crontab -l'
172.25.250.11 | CHANGED | rc=0 >>
#Ansible: qihao
*/2 * * * * date
172.25.250.12 | CHANGED | rc=0 >>
#Ansible: qihao
*/2 * * * * date

再次查看就会有crontab命令生成

[root@bastion ansible]# ansible prod -m cron -a "job=date name=qihao minute=*/2 disabled=yes"

关闭crontab任务

[root@bastion ansible]# ansible prod -m cron -a "job=date name=qihao minute=*/2 disabled=no"

开启crontab任务

[root@bastion ansible]# ansible prod -m cron -a "job=date name=qihao minute=*/2 state=absent"

删除任务
2.hostname模块
更改主机名

[root@bastion ansible]# ansible prod -m shell -a 'hostname'

查看主机名

[root@bastion ansible]# ansible 172.25.250.12 -m hostname -a 'name=nihaoya'

把主机名修改为你好呀

[root@bastion ansible]# ansible prod -m shell -a 'hostname'
172.25.250.12 | CHANGED | rc=0 >>
nihaoya
172.25.250.11 | CHANGED | rc=0 >>
serverb.exam.com

修改成功
3.yum_repository模块

[root@bastion ansible]# ansible prod -m yum_repository -a "name=AppStream baseurl=http://172.25.250.250/rhel8.2/AppStream description=linshi gpgcheck=no file=linshi"

description就是name
file=后面不要加repo的后缀,因为会自动生成。file就是此文件的名字。

[root@bastion ansible]# ansible prod -m yum_repository -a "name=BaseOS baseurl=http://172.25.250.250/rhel8.2/BaseOS description=linshi gpgcheck=no file=linshi"

添加了新的内容

[root@bastion ansible]# ansible prod -m yum_repository -a "name=BaseOS baseurl=http://172.25.250.250/rhel8.2/BaseOS description=linshi gpgcheck=no file=linshi enabled=0"

关闭内容

[root@bastion ansible]# ansible prod -m yum_repository -a "name=BaseOS baseurl=http://172.25.250.250/rhel8.2/BaseOS description=linshi gpgcheck=no file=linshi enabled=1 state=absent"

删除内容
4.dnf模块

[root@bastion ansible]# ansible prod  -m  dnf -a 'name=vsftpd state=latest'

安装vsftpd,latest表示最新版,如果已经安装了但是不是最新版也会更新

[root@bastion ansible]# ansible prod  -m  dnf -a 'name="vsftpd,httpd,dhcp-server" state=latest disable_gpg_check=yes'

安装多个软件时候,用引号引起来,逗号隔开,如果gpgcheck不通过,就用最后的命令关闭gpg检查

[root@bastion ansible]# ansible prod  -m  dnf -a 'name=httpd state=absent'

卸载阿帕齐,只卸载软件不卸载依赖性。

[root@bastion ansible]# ansible prod  -m  dnf -a 'name=httpd state=absent autoremove=yes'

加上这个参数后,会卸载依赖性。
5.service模块

[root@bastion ansible]# ansible 172.25.250.12 -m service -a 'name=httpd state=started'

12受控机开启httpd

[root@bastion ansible]# ansible 172.25.250.12 -m shell -a 'systemctl status httpd'

查看开启状态

[root@bastion ansible]# ansible 172.25.250.12 -m service -a 'name=httpd state=reloaded'

重新加载httpd
6.firewalld模块

[root@bastion ansible]# ansible 172.25.250.12 -m firewalld -a 'zone=public service=http permanent=yes state=enabled immediate=yes'

immdiate立即生效,state是开和关
7.user模块
用户管理

[root@bastion ansible]# ansible 172.25.250.12 -m user -a 'name=test state=present'

建立用户

[root@bastion ansible]# ansible 172.25.250.12 -m user -a 'name=test1 state=present uid=6666'

指定id

[root@bastion ansible]# ansible 172.25.250.12 -m user -a 'name=test1 state=absent remove=yes uid=6666'

删除时候,一起删除家目录

group=指定组id
shell=/bin/sh指定shell
comment=‘test user’指定说明

8.group模块

[root@bastion ansible]# ansible 172.25.250.12 -m group -a 'name=wang state=present'

建立组

[root@bastion ansible]# ansible 172.25.250.12 -m group -a 'name=wang state=absent'

删除组

[root@bastion ansible]# ansible 172.25.250.12 -m group -a 'name=wang state=present gid=2000'

建立时指定gid
9.lineinfile
修改文件

[root@bastion ansible]# ansible 172.25.250.12 -m lineinfile -a 'path=/mnt/test line="hello you" create=yes'

创建并书写了文件内容

[root@bastion ansible]# ansible 172.25.250.12 -m lineinfile -a 'path=/mnt/test line="hello hehehe" create=yes'

再加一行,直接加在后面

[root@bastion ansible]# ansible 172.25.250.12 -m lineinfile -a 'path=/mnt/test line="hello you" create=yes regexp="test"'

把有test的行内容都改成hello you

[root@bastion ansible]# ansible 172.25.250.12 -m lineinfile -a 'path=/mnt/test line="nima" create=yes regexp="hello you"'

第一行和最后一行都是hello you但是只有最后一行的内容被改变。

[root@bastion ansible]# ansible 172.25.250.12 -m lineinfile -a 'path=/mnt/test state=absent regexp="hello you"'

删除带有hello you的一行内容
10.replace模块

[root@bastion ansible]# ansible 172.25.250.12 -m replace -a 'path=/mnt/test regexp="hello" replace="hi" backup=yes'

把hello都替换成hi,同时创建一个备份文件。
replace只能替换一个字符,但是lineinfile可以替换一行
11.setup模块
采集受控主机的信息

[root@bastion ansible]# ansible 172.25.250.12 -m setup

查看12的信息

[root@bastion ansible]# ansible 172.25.250.12 -m setup -a 'filter="ansible_all_ipv4_addresses"'

只取ipv4的部分
以后经常会用
11.debug模块
相当于shell中的echo

[root@bastion ansible]# ansible 172.25.250.12 -m debug -a 'msg="hello"'

在12中显示hello
msg=message
显示字符串,var显示变量的值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值