常用模块

file 模块 管理被控端文件

回显为绿色则,未变更,符合要求

黄色则改变

红色则报错

因为默认值为file,那么文件不存在,报错

改为touch则创建

将state改为directory变成创建目录(默认可以递归)

创建软链接或硬链接

[root@workstation modules]# ansible servera  -m file  -a  'path=/tmp/redhat1 state=absent'
absent删除文件 [root@workstation modules]# ansible servera -m file -a 'path=/tmp/file mode=755 owner=ansible'
servera | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "ansible",
"path": "/tmp/file",
"secontext": "unconfined_u:object_r:user_tmp_t:s0",
"size": 0,
"state": "file",
"uid": 1001
}
更改已经存在的目录 可以加state=touch 也可以不加效果一样 [root@workstation modules]# ansible servera -m file -a 'src=/tmp/file2 dest=/tmp/file33 state=link force=yes'
[WARNING]: Cannot set fs attributes on a non-existent symlink target. follow should be set to False to avoid
this.
servera | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"dest": "/tmp/file33",
"src": "/tmp/file2"
}

强制创建不存在源文件的链接文件

源文件不同则覆盖(不加force也可以)

根据红色报错来决定加不加force更合理

copy模块 将主控端文件给被控端

[root@workstation maosible]# ansible  servera -m  copy -a 'src=hosts  dest=/tmp/dir01'
servera | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "e7a86fde02d85341de7f8a7c1544a3943e6aff9a",
"dest": "/tmp/dir01",
"gid": 0,
"group": "root",
"md5sum": "46d0842e39f0fb11629b1b07653420e0",
"mode": "0644",
"owner": "root",
"secontext": "unconfined_u:object_r:user_home_t:s0",
"size": 16,
"src": "/home/ansible/.ansible/tmp/ansible-tmp-1662013817.618654-7648-138526025113835/source",
"state": "file",
"uid": 0
}

一个致命的小细节

[root@workstation maosible]# ln -s /important/  ./abca
[root@workstation maosible]# rm -f abca/
rm: cannot remove 'abca/': Is a directory
[root@workstation maosible]# rm -f abca
[root@workstation maosible]#

这个小小的/区别很大。一定要注意,哪些位置需要加/

[root@workstation maosible]# ansible servera -m copy -a 'content="hello world\n" dest=/tmp/file2'

copy也可以写文件(相当于重定向>)

backup 在覆盖之前将原文件备份。备份包含时间信息

force=no 防止覆盖

remote_src 复制被控端到被控端 默认no

validate 测试文件的语法如果测试不通过,则不执行

[root@workstation maosible]# cat /etc/sudoers.d/kk
xiaomao ALL=(ALL) NOPASSWD:ALL
[root@workstation maosible]# ansible servera -m copy -a "src=/etc/sudoers.d/kk dest=/etc/sudoers.d/user1 validate='visudo -cf %s'"
servera | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"checksum": "276588c6d80f03f87e149fb9cf406f7589b12299",
"dest": "/etc/sudoers.d/user1",
"gid": 0,
"group": "root",
"md5sum": "c647cd86fe29f8aae9ced2c5e4ce4063",
"mode": "0644",
"owner": "root",
"secontext": "system_u:object_r:etc_t:s0",
"size": 31,
"src": "/home/ansible/.ansible/tmp/ansible-tmp-1662016144.5676467-8325-177579230721100/source",
"state": "file",
"uid": 0
}

检查文件格式并发送,不正确不发

https://docs.ansible.com/ansible/latest/collections/ansible/builtin/file_module.html

更多参考文档

user模块 管理用户

查帮助

ansible-doc -l 列出所有模块

ansible-doc user 查user的帮助

[root@workstation maosible]#  ansible  servera -m user -a 'name=user1 uid=1100 state=present'

幂等性的缘故,所以可以重复执行命令达到想要的效果

[root@workstation maosible]#  ansible  servera -m user -a 'name=user1 uid=1100  group=ansible shell=/sbin/nologin state=present'

[root@workstation maosible]#  ansible  servera -m user -a 'name=user1 remove=yes state=absent'
连带家目录一起删除

设置密码

[root@workstation maosible]# ansible all -i localhost, -m debug -a "msg={{ 'redhat' | password_hash('sha512', 'mysecretsalt') }}"
localhost | SUCCESS => {
"msg": "$6$mysecretsalt$GcajIATSXc4CUJ.uOMrH.oB7A7dch4KSuaNfL12kfmhFZz7hH9gcttplfRfmk4rQ.sQnZieSBxqi6xPDFBGRC0"
}

来自官方文档的指引

[root@workstation maosible]# ansible  servera  -m user -a 'name=user1 uid=1101 state=present password="$6$mysecretsalt$GcajIATSXc4CUJ.uOMrH.oB7A7dch4KSuaNfL12kfmhFZz7hH9gcttplfRfmk4rQ.sQnZieSBxqi6xPDFBGRC0"'

 ansible localhost -m debug -a "msg={{ 'redhat' | password_hash('sha512', 'mysecretsalt') }}"
直接在ansible节点输出就好了

一次做完

[root@workstation maosible]# ansible servera -m user -a "name=user1 uid=1101 state=present password={{ '2redhat' | password_hash('sha512', 'mysecretsalt') }}"

将密码管道给password_hash('sha512', 'mysecretsalt') 因为里面有变量所以 {{}}

group 模块

[root@workstation maosible]# ansible servera -m group -a 'name=it1  state=present'

[root@workstation maosible]# ansible servera  -m user -a 'name=bob group=it1 state=present'

[root@workstation maosible]# ansible servera  -m user -a 'name=bob group=it1 groups=root,ansible state=present'
name group groups这些参数没有次序,想怎么放就怎么放
创建用户并指定组,并添加附加组

yum 模块

可以查ansible-doc
- name: Add multiple repositories into the same file (2/2)
yum_repository:
name: rpmforge
description: RPMforge YUM repo
file: external_repos
baseurl: http://apt.sw.be/redhat/el7/en/$basearch/rpmforge
mirrorlist: http://mirrorlist.repoforge.org/el7/mirrors-rpmforge
enabled: no [root@workstation maosible]# ansible servera -m yum_repository -a 'baseurl=file:///mnt enabled=yes description=abc file=abc gpgcheck=no name=dvd'

配仓库

[root@workstation maosible]# ansible all -m  yum  -a 'name=tree state=present'

装包

 [root@workstation maosible]# ansible all -m  yum  -a 'name="@Development tools" state=present'

装包组

[root@workstation maosible]# ansible servera -m yum -a 'name=* state=present'
相当于servera yum update -y

更新

ansible命令发到被控端是不好撤回的  ctrl+c不是很有用

package模块封装了yum与apt

service 模块

[root@workstation maosible]# ansible servera -m service -a 'name=sshd state=started enabled=yes'

systemd 模块

当 需要deamon-reload得需要systemd

cron 模块

[root@workstation maosible]# ansible  servera -m cron -a 'hour=05  user=user1 job="echo hello" name=fox'
servera | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"envs": [],
"jobs": [
"fox"
]
}
[root@workstation maosible]# ssh root@servera
Last login: Fri Sep 2 21:14:14 2022 from 192.168.230.164
[root@servera ~]# crontab -l -u user1
#Ansible: fox
* 05 * * * echo hello
[root@servera ~]#

加name,ansible需要一个cron标识

[root@workstation maosible]# ansible  servera -m cron -a 'hour=05  user=user1 job="echo hellwwo" name=fox cron_file=/etc/cron.d/cronmqy'
servera | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"cron_file": "/etc/cron.d/cronmqy",
"envs": [],
"jobs": [
"fox"
]
}
[root@workstation maosible]# ansible servera -m cron -a 'hour=05 user=root job="echo he2llwwo" name=fox cron_file=/etc/crontab'
servera | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/libexec/platform-python"
},
"changed": true,
"cron_file": "/etc/crontab",
"envs": [
"SHELL",
"PATH",
"MAILTO"
],
"jobs": [
"fox"
]
} [root@servera cron.d]# cat cronmqy
#Ansible: fox
* 05 * * * user1 echo hellwwo
[root@servera cron.d]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root # For details see man 4 crontabs # Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed #Ansible: fox
* 05 * * * root echo he2llwwo
[root@servera cron.d]#

这里name必须都不一样才好。我这里偷懒了

这里主要是显示出ansible的计划任务可以写进文件

cron.d下面自定义文件很方便。名字随便取

synchronize 同步

ansible servera -m synchronize -a 'src=/root/ansible/ dest=/tmp/data archive=no rsync_opts=-tr'

根据时间戳同步目录

-tro o为拥有人

ansible 003 常用模块的更多相关文章

  1. ansible中常用模块详解

    ansible中常用的模块详解: file模块 ansible内置的可以查看模块用法的命令如下: [root@docker5 ~]# ansible-doc -s file - name: Sets ...

  2. ansible api常用模块与参数

    ###ansibleAPI 常用模块 用于读取yaml,json格式的文件 from ansible.parsing.dataloader import DataLoader #用于管理变量的类,包括 ...

  3. Ansible之常用模块(一)

    ansible之所以功能强大,不是ansible本身,是因为它有众多的模块,前文我们介绍了ansible的基础介绍,系列命令的用法以及选项的说明,通过前文的学习我们知道了ansible是基于pytho ...

  4. ansible API 常用模块

    常用模块 用于读取yaml,json格式的文件 from ansible.parsing.dataloader import DataLoader #用于管理变量的类,包括主机,组,扩展等变量 fro ...

  5. ansible 四常用模块

    常用模块 Ansible默认提供了很多模块来供我们使用.在Linux中,我们可以通过 ansible-doc -l 命令查看到当前Ansible支持哪些模块,通过 ansible-doc -s [模块 ...

  6. Ansible Playbooks 常用模块

    官网链接:https://docs.ansible.com/ansible/latest/modules/list_of_all_modules.html ansible python module ...

  7. ansible小结常用模块

    根据官方的分类,将模块按功能分类为:云模块.命令模块.数据库模块.文件模块.资产模块.消息模块.监控模块.网络模块.通知模块.包管理模块.源码控制模块.系统模块.单元模块.web设施模块.window ...

  8. Ansible之常用模块(二)

    1.hostname:此模块的主要作用是管理远端节点主机名 模块帮助: root@localhost ~]# ansible-doc -s hostname - name: Manage hostna ...

  9. Ansible之常用模块介绍

    环境 ansible HOST-PATTERN -m MOD_NAME -a MOD_ARGS -C -f forks ssh-keygen -t rsa -P "" ssh-co ...

随机推荐

  1. powershell命令总结

    2021-07-21 初稿 ps命令采用动词-名词的方式命名,不区分大小写.默认当前文件夹为当前路径./.除去-match使用正则表达式匹配外,其他都使用*和?通配符. 速查 管道命令 前一个的输出作 ...

  2. 在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式

    在进行项目开发的时候,刚好需要用到对字符串表达式进行求值的处理场景,因此寻找了几个符合要求的第三方组件LambdaParser.DynamicExpresso.Z.Expressions,它们各自功能 ...

  3. 一文理解OpenStack网络

    摘要:如果你能理解OpenStack的网络,那么对于其他云平台的网络,应该也可以通过分析后理解掌握了. 本文分享自华为云社区<<跟唐老师学习云网络> - OpenStack网络实现& ...

  4. QT工程构建目录下,将生成的中间文件和可执行文件分离

    在QT工程中,当我们选择了构建目录后,编译生成程序后,总会发现在debug目录下会有混淆着各类文件,如下图 很多时候,我们又仅仅只需要可执行文件或者自定义的动态链接库.如下图 当然,如果不觉得麻烦,有 ...

  5. Java多线程下载分析

    为什么要多线程下载 俗话说要以终为始,那么我们首先要明确多线程下载的目标是什么,不外乎是为了更快的下载文件.那么问题来了,多线程下载文件相比于单线程是不是更快? 对于这个问题可以看下图. 横坐标是线程 ...

  6. Linux下修改RabbitMQ密码

    1,首先查看用户列表 rabbitmqctl list_users 2,修改对应用户密码 其中username 为用户名, newpasswd为新密码 rabbitmqctl change_passw ...

  7. 本地使用 Docker Compose 与 Nestjs 快速构建基于 Dapr 的 Redis 发布/订阅分布式应用

    Dapr(分布式应用程序运行时)介绍 Dapr 是一个可移植的.事件驱动的运行时,它使任何开发人员能够轻松构建出弹性的.无状态和有状态的应用程序,并可运行在云平台或边缘计算中,它同时也支持多种编程语言 ...

  8. 用KVM安装MacOS/OSX

    基本步骤按照大牛的步骤https://github.com/kholia/OSX-KVM 黑果镜像建议用黑果小兵的:macOS Big Sur(我试过,大牛的更卡),里面的双EFI就很够用. 将镜像名 ...

  9. ATM系统开发(Java版)

    ATM系统开发 技术点分析 1.面向对象编程 每个用户的账户都是一个对象,所以需要设计账户类Accent用于创建账户对象封装账户信息. 2.使用集合容器 系统需要提供一个容器用于存储这些账户对象的信息 ...

  10. ApiDay002_01 正则表达式

    正则表达式 用于检测.测试字符串规则的表达式. 经常用于检测字符串是否符合特定的规则,在网站上经常用于检测用户输入数据是否符合规范: 检测 用户名 是否为 8~10 数字 英文(大小写) 检测 电话号 ...