ansible 003 常用模块

常用模块

file 模块 管理被控端文件

回显为绿色则,未变更,符合要求
黄色则改变
红色则报错

ansible 003 常用模块

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

ansible 003 常用模块

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

创建软链接或硬链接

ansible 003 常用模块
[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'
连带家目录一起删除

设置密码

ansible 003 常用模块
[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下面自定义文件很方便。名字随便取

ansible 003 常用模块

synchronize 同步

ansible 003 常用模块

ansible 003 常用模块

ansible servera -m synchronize -a ‘src=/root/ansible/ dest=/tmp/data archive=no rsync_opts=-tr’
根据时间戳同步目录
-tro o为拥有人

Original: https://www.cnblogs.com/supermao12/p/16649725.html
Author: supermao12
Title: ansible 003 常用模块

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/576958/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • vulnhub靶场之Beelzebub

    准备: 攻击机:虚拟机kali、本机win10。 靶机:Beelzebub: 1,网段地址我这里设置的桥接,所以与本机电脑在同一网段,下载地址:https://download.v…

    Python 2023年10月15日
    028
  • BugKu CTF Web

    BugKu CTF Web 滑稽 计算器 GET POST Simple_SSTI_1 矛盾 eval 变量1 Simple_SSTI_2 alert 你必须让他停下 * 方法一 …

    Python 2023年8月14日
    0130
  • THREE.JS实现看房自由(VR看房)

    VR看房 * – 一、前言 – 二、基础知识 – 三、场景 – + 3.1 网络模型 + 3.2 光照 + * 3.2.1 环境光 …

    Python 2023年9月17日
    042
  • Java注解(4):一个真实的Elasticsearch案例

    昨天把拼了一半的注解+Elasticsearch积木放下了,因为东西太多了拼不好,还容易乱。休息了一晚上接着来。 接着昨天,创建elasticsearch文档注解(相当于数据表的注…

    Python 2023年10月18日
    028
  • 瑞吉外卖实战项目全攻略——总结篇

    瑞吉外卖实战项目全攻略——总结篇 该系列将记录一份完整的实战项目的完成过程,该篇属于总结篇,主要负责总结整个项目技术点和注意点 案例来自B站黑马程序员Java项目实战《瑞吉外卖》,…

    Python 2023年10月16日
    047
  • Java实战-用Java mail实现Exchange发邮件给你喜欢的人

    目录 1. 官方指导文章 2. 需要用到com.microsoft.ews-java-api * 2.1 maven中添加 2.2 gradle中添加 3. 完整代码:新建一个Ma…

    Python 2023年8月2日
    051
  • 神经网络模型量化基础

    1,模型量化概述 1.1,模型量化优点 1.2,模型量化的方案 1.2.1,PTQ 理解 1.3,量化的分类 1.3.1,线性量化概述 2,量化算术 2.1,定点和浮点 2.2,量…

    Python 2023年10月24日
    042
  • linux 上安装 NTL(wsl2)

    说明: NTL_GMP_LIP flag set GMP version check (6.1.2/6.1.2) 安装 m4 sudo apt-get install m4 安装g…

    Python 2023年6月10日
    072
  • python scrapy实践-爬取豆瓣读书

    按scrapy官网的介绍来说,scrapy是一种快速的高级 web crawling和 web scraping,用于对网站进行爬取并从其页面提取结构化数据的框架,也就是爬虫。它可…

    Python 2023年10月2日
    046
  • AI艺术的背后:详解文本生成图像模型【基于 Diffusion Model】

    系列文章链接: AI艺术的背后:详解文本生成图像模型【基于 VQ-VAE】AI艺术的背后:详解文本生成图像模型【基于GAN】AI艺术的背后:详解文本生成图像模型【基于Diffusi…

    Python 2023年9月15日
    083
  • 带权路径长度

    给定n个权值作为n个叶子结点,构造哈夫曼树, 求其带权路径长度。 输入由多组数据组成。每组数据分成两行。第一行仅一个整数n(2 Original: https://www.cnbl…

    Python 2023年6月12日
    066
  • python logging模块使用方法

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    Python 2023年6月12日
    066
  • pandas中drop用法_python进行数据清理之pandas中的drop用法

    好久好久没有更新博客了,之前自学的估计也都忘记差不多了。由于毕业选择从事的行业与自己的兴趣爱好完全两条路,心情也难过了很久,既然入职了就要好好干,仍要保持自己的兴趣,利用业余时间重…

    Python 2023年8月8日
    075
  • 关于阿里云服务器上Django、Mysql、uwsgi、TensorFlow安装配置实验报告

    目录 背景 位置 实验-1 * 实验配置 实验相关命令 实验结果 实验-2 * 位置 实验配置 实验内容 – Mysql配置 + 1.Mysql基础操作 2.Cento…

    Python 2023年8月4日
    037
  • Gunicorn运行flask

    Gunicorn”绿色独角兽”是一个被广泛使用的高性能的Python WSGI UNIX HTTP服务器,移植自Ruby的独角兽(Unicorn )项目,使…

    Python 2023年8月13日
    047
  • Pytorch中Tensor数据类型转换

    Pytorch中Tensor数据类型转换: 1)Tensor的int、float数据类型转换: 在Tensor后加 .long(), .int(), .float(), .doub…

    Python 2023年8月24日
    038
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球