zoukankan      html  css  js  c++  java
  • ansible语法

    ansible语法

    注意:需要提示分发公钥

    ansible test -a "free -h"
    ansible test -m command -a "free -h"
    

    -m:指定模块
    -a:指定参数
    test 对应 /etc/ansible/hosts中的[test]
    all 对应 /etc/ansible/hosts所有ip
    注意:默认模块不支持管道通信

    常用参数

    参数 解释
    -m 模块,默认command
    -a 参数
    -C,--checks 检查,预期执行
    -syntax-checks 检查剧本,预期执行

    颜色说明

    绿:正常
    红:异常
    黄:有变动

    模块详情

    查看模块信息

    ansible-doc -v command
    

    chdir参数的使用:

    ansible test -m command -a "chdir=/etc pwd"
    

    shell模块

    注意:执行时,各主机本地需要存在shell脚本
    与command模块的不同在于,shell模块可以使用变量,管道,定向符号等

    script

    当前节点的本地脚本,远程执行

    #本地节点脚本文件
    cat >/home/centos/sh.sh <<EOF
    #!/bin/bash
    echo $HOSTMANE
    w
    ping qq.com -c 2
    EOF
    #远程执行本地文件
    ansible test -m script -a "/home/centos/sh.sh"
    

    脚本文件不具有执行权限,也可以远程执行命令

    copy

    参数 说明
    backup 是否备份远程已存在的文件
    content 可以设定文件的值
    dest 远端路径
    directory_mode 递归设置目录的权限,默认为系统权限
    forces 文件内容存在差异,forces为yes则覆盖,为no,则不覆盖。默认覆盖。别名:thirsty
    src 路径以/结尾,只复制目录中的文件,否则递归复制所有文件,类似rsync
    mode 权限位设置
    owner 属主
    group 属组

    src 不能和 content一起使用

    copy使用示例

    远程复制

    ansible test -m copy -a "src=/etc/hosts dest=~/ mode=0644 owner=centos group=centos"
    ansilbe test -m shell -a "ls -la ~/host*"
    

    远程执行 主机复制本地文件

    #本地创建一个文件
    touch sh.sh 
    ansible test -m copy -a "src=~/sh.sh dest=~/sh.sh.sh remote_src=true"
    #此时仅能在本地机器上显示复制的文件,表明remote_src为true时只能复制远端已存在的文件
    ansible test -m shell -a "ls -l ~/sh*"
    

    内容传递
    注意:换行符使用 代替

    ansible test -m copy -a "content='hello
    world' dest=~/h.c mod=0600"
    ansilbe test -m shel -a "cat ~/.h.c"
    

    file

    主要用于创建文件,删除文件,设置文件属性

    普通参数

    参数 说明
    owner 属主
    group 属组
    mode 权限
    dest 远端路径
    src 本地路径

    state

    有存在多种状态,如下

    参数 说明
    directory 目录
    file 文件
    link 软链接
    hard 硬链接
    absent 递归删除目录,取消软链接
    touch 创建文件

    注意:不能同时递归创建目录和重命名
    创建目录

    ansible test -m file -a "path=~/a/b/c/d/e/f state=directory"
    ansible test -m shell -a "tree ~/"
    

    递归删除目录

    #根据之前创建的目录
    ansible test -m file -a "dest=~/a state=absent"
    ansible test -m shell -a "ls -l ~/"
    

    创建文件

    ansible test -m file -a "dest=~/a.txt state=touch"
    ansible test -m shell -a "ls ~/"
    

    创建软链接

    #根据之前创建的文件,创建软链接
    ansible test -m file -a "dest=~/softa.txt src=~/a.txt state=link"
    ansible test -m shell -a "ls -l ~/"
    

    创建硬链接

    #先给a.txt文件追加"hello
    world"
    ansible test -m shell -a "rm -f ~/a.txt"
    ansible test -m copy -a "content='hello
    world' dest=~/a.txt"
    ansible test -m shell -a "cat ~/a.txt"
    ansible test -m file -a "dest=~/ahard.txt src=~/a.txt state=hard"
    ansible test -m shell -a "cat ~/ahard.txt"
    ansible test -m shell -a "ls -il | grep a"
    
  • 相关阅读:
    匿名函数
    Python基础练习题5
    for循环实现一个注册小案例
    Python基础练习题4
    Python集合
    Python基础练习题3
    Python 元组和字典
    Python PEP8规范与python之禅
    Python基础练习题2
    常见的排序之冒泡排序
  • 原文地址:https://www.cnblogs.com/anyux/p/12002272.html
Copyright © 2011-2022 走看看