首页 > 其他分享 >ansible03-ansible模块

ansible03-ansible模块

时间:2024-05-10 09:58:08浏览次数:20  
标签:file name dest host10 state ansible 模块 ansible03

所有模块一览

https://blog.csdn.net/sj349781478/article/details/106898574?utm_source=app&app_version=4.13.0

command模块

默认模块 可以不写

ansible  host  -a  'chdir=/data ls'
ansible  host  -a  'creates=/root/test ls'
ansible  host  -a  'remove=/root/test ls'

选项
chdir    执行命令前,先切换到该目录
creates  指定的文件存在,则不执行命令
remove   指定的文件不存在,则不执行命令

command 模块不能解析变量(如$HOSTNAME)和某些特殊字符(<  >  |  &), 带这些符号的命令用shell模块

script模块

远程主机运行本地脚本
在执行脚本前,ansible会将本地脚本传输到远程主机,执行后删除.在执行脚本的时候,采用的是远程主机上的shell环境
ansible host -m script -a /tmp/test.sh

copy模块

拷贝本地文件到远程
ansible host -m copy -a 'src=/etc/passwd dest=/etc/passwd backup=yes mode=600 owner=root group=root'
ansible host -m copy -a 'src=/root/user.sh dest=/tmp backup=yes'
ansible host -m copy -a 'content="hello\nworld" dest=/tmp/f1'

当src采用相对路径时,该路径表示当前用户家目录

选项
backup   当远程主机存在同名但内容不同的文件时,在目标目录将原文件重命名,名称含有时间戳信息
content  以content选项给定的内容作为拷贝至远程主机的文件内容,相当于在远程主机创建文件

fetch模块

从远程主机拿文件到本地

存储为/tmp/fstab,最常用
ansible host10 -m fetch -a "src=/etc/fstab dest=/tmp/ flat=yes"

存储为/tmp/host10/etc/fstab
ansible host10 -m fetch -a "src=/etc/fstab dest=/tmp"

存储为/tmp/fstab-host10
ansible host10 -m fetch -a "src=/etc/fstab dest=/tmp/fstab-{{inventory_hostname}} flat=yes"

file模块

管理⽂件、⽬录的属性,也可以创建⽂件或⽬录。
需要注意的是, file模块可以递归创建⽬录,但是不能在不存在的⽬录中创建⽂件,只能先创建⽬录,再在此⽬录中创建⽂件。

选项
group             file/directory的所属组
owner             file/directory的所有者
mode              修改权限,格式可以是0644、 'u+rwx'或'u=rw,g=r,o=r'等
path                指定待操作的⽂件,可使⽤别名'dest'或'name'来替代path
recurse           (默认no)递归修改⽂件的属性信息,要求state=directory
src                  创建链接时使⽤,指定链接的源⽂件
state               他有以下几个选项             
                       touch:创建文件或修改文件属性
                       directory:如果⽬录不存在则递归创建
                       absent:⽬录和其中的⽂件会被递归删除,⽂件或链接将取消链接状态
                       link:修改或创建软链接
                       hard:修改或创建硬链接
                       file:⽂件不存在时,不会被创建(默认值)

path和dest有时可以替换使用

递归创建目录并附加相应权限
ansible host10 -m file -a 'path=/tmp/xyz state=directory owner=root group=root mode=0755 recurse=yes'

承上,修改目录权限
ansible host10 -m file -a 'path=/tmp/xyz state=touch mode=0644'

创建文件
ansible host10 -m file -a 'path=/tmp/xyz/a.txt state=touch mode=0644

创建软连接
ansible host10 -m file -a 'src=/etc/fstab dest=/data/fstab.link state=link'

删除软连接
ansible host10 -m file -a 'dest=/data/fstab/fstab.link state=absent'

hostname模块

ansible host -m hostname -a 'name=server10'

该命令会立即生效
修改/etc/hostname文件后,建议再修改/etc/hosts文件中的主机名(127.0.0.1那一行)

cron模块

创建计划任务, 每2分钟进⾏⼀次时间同步,并且⾃定义cron_file
ansible host10 -m cron -a 'name="ntpdate" job="/usr/sbin/ntpdate ntp1.aliyun.com" cron_file=ntpdate_cron minute=*/2'

ansible host10 -m cron -a  'minute=10 hour=10 day=* month=* weekday=* name="My-NTP" job="ntpdate ntp1.aliyun.com"'

验证
ansible host10 -m shell -a 'cat /etc/cron.d/ntpdate_cron'

移除⼀个job,要求name必须匹配。如有必要,需要同时指定cron_file和user。
ansible host10 -m cron -a 'name="ntpdate" state=absent cron_file=ntpdate_cron user=root' -o

禁用一个job
ansible host10 -m cron -a 'disabled=true job="/usr/sbin/ntpdate ntp1.aliyun.com" name=ntpdate'

yum模块

name需要配合state来使⽤,如果state指定为present/installed/latest将安装包,其中latest是安装最新包,默认
为present。如果指定为absent/removed则⽤于卸载包。
在ansible中,很多地⽅都会出现present和absent的状态,它们⼀般都表示⽬标是否应该存在还是不存在,也就是
要进⾏的动作是创建和删除

选项
disable_gpg_check                 # 安装包时禁⽌gpgcheck,仅在state=present或latest时生效
disablerepo                            # 禁⽤指定的repoid,多个repoid使⽤逗号分隔
enablerepo                            # 明确使⽤该repoid
exclude                                  # 排除哪些包不安装,仅在state=present或latest时⽣效
list                                          # 类似于yum list
name=                                   # 指定安装的包名,可带上版本号。多个包可使⽤逗号分隔
update_cache=                     # 强制更新yum的cache    yes或no
state                                      # 状态 present、installed、latest⽤于安装包
                                              # absent、removed ⽤于移除已安装包

安装,安装多个时,包名用逗号间隔
ansible host10 -m yum -a 'name=httpd state=latest'

安装本地指定的包
ansible host10 -m yum -a '/root/vsftpd-3.0.2-22.e17.x86_64.rpm'

卸载,卸载多个时,包名用逗号间隔
ansible host10 -m yum -a 'name=httpd state=removed'

service模块

选项
name=       服务名
state=      started 或 stopped 或 reloaded
enabled=    yes 或 no

user模块

新建账号
ansible host10 -m user -a 'name=nginx shell=/sbin/nologin home=/home/nginx system=yes  groups=nginx '

删除账号,和家目录
ansible host10 -m user -a 'name=nginx state=absent remove=yes'

修改密码
ansible host10 -m user -a "name=caolixun password=$6$GD8Q update_password=always"   #password 后面接的是加密以后的密码。

get_url

下载文件到指定目录:
ansible all -m get_url -a "url=http://www.guojinbao.com dest=/tmp/guojinbao mode=0440 force=yes"
需要添加登录名密码的网站,使用url_password、url_username参数来定义

unarchive

目标目录不存在报错

解压ansible主机上的文件到远程主机
ansible all:\!host114 -m unarchive -a 'src=/root/server_info_2.tgz dest=/root/ mode=0755 copy=yes'

解压远程主机上的文件
ansible all:\!host114 -m unarchive -a 'src=/root/server_info_2.tgz dest=/root/ mode=0755 copy=no'

标签:file,name,dest,host10,state,ansible,模块,ansible03
From: https://www.cnblogs.com/lixunblogs/p/18167309

相关文章

  • ansible05-ansible roles
    5.ansiblerolesroles将变量、任务、模板等文件单独配置在yaml文件中,在main.yaml中用include字段来指定要执行的yaml文件官方建议目录/etc/ansible/roles调用roles的yml文件与roles目录平级roles的目录结构roles目录中创建角色名比如叫做project,其子目录包含如下:files/ ......
  • ansible01-ansible安装配置
    中文权威指南http://www.ansible.com.cn/docs/playbooks_variables.html#yaml安装wget-O/etc/yum.repos.d/epel.repohttp://mirrors.aliyun.com/repo/epel-7.repowget-O/etc/yum.repos.d/Centos-7.repohttp://mirrors.aliyun.com/repo/Centos-7.repo依赖  paramiko......
  • ansible04-ansible-playbook
    4.ansible-playbookplaybook文件示例执行playbook,拷贝服务配置文件到目标主机后,对原文件修改后再次执行playbook,已启动的服务不会再次重启-hosts:host10 remote_user:root tasks:  -name:installhttpd   yum:name=httpdstate=present  -name:......
  • ansible06-ansible-galaxy
    ansible系列命令ansible-galaxy连接https://galaxy.ansible.com下载相应的roles列出所有已经安装的galaxyansible-galaxylist从官网下载ansible-galaxyinstallgeerlingguy.nginx一般下载至/etc/ansible/roles/geerlingguy.nginx删除andible-galaxyremovegeerli......
  • ansible02-ansible变量的定义与引用
    4.1ansible变量的定义与引用playbook中变量的定义变量名只能由数字、字母、下划线组成,且只能由字母开头变量的来源(1)setup模块中的所有变量都可以直接调用(2)在/etc/ansible/hosts中定义- 普通变量:主机组中主机单独定义,优先级高于公共变量- 公共变量:也称组变量,针对主......
  • CH592 蓝牙透传模块
    设备架构串口透传协议说明模块通过串口和用户MCU相连,建立用户MCU和BLE设备之间的双向通讯。用户可以通过串口,使用指定的AT指令对串口波特率、BLE连接间隔,以及不同的发包间隔,模块将会有不同的数据吞吐能力。串口默认配置为115200bps。模块的串口Rx一次最大可输入3K字节。......
  • 固态继电器 一体式调压模块
    固态继电器: 无触点的中间继电器,相应时间1ms,(普通中间继电器10ms)      单相调压模块 ......
  • ssh、PAM模块
    .ssh/known_hosts存储ssh指纹sshd服务器端/etc/ssh/sshd_config服务器端的配置文件man5sshd_config服务器端的配置文件帮助echoroot:1111|chapasswd修改密码opensslrand-base649随机取9位密码(随机数经过base编码取9位)ssh常用参数:Port 22  #生产建议修......
  • 模块学习之hashlib模块
    【一】什么是摘要算法Python的hashlib提供了常见的摘要算法,如MD5、SHA1等等摘要算法又称哈希算法、散列算法它通过一个函数,把任意长度的数据转换为一个长度固定的数据串(通常用16进制的字符串表示)摘要算法就是通过摘要函数f()对任意长度的数据data计算出固定长度的摘要digest......
  • 模块学习之logging模块
    【一】函数式简单配置importlogginglogging.debug('debugmessage')logging.info('infomessage')logging.warning('warningmessage')logging.error('errormessage')logging.critical('criticalmessage')默认情况......