ansible-doc yum # 查看帮助 # 常用模块 command 命令模块-默认模块-执行命令 shell 执行命令 yum 安装软件模块 copy 复制 配置模块 service 启动服务模块 user group 用户管理模块 file 用户创建目录,创建文件,往文件写内容 cron 定时任务 mount 挂载 # ansible-doc copy 查看帮助 ################################# command 命令模块-默认模块-执行命令 ansible test -m command -a "w" ################################shell 执行命令 ansible test -m shell -a "w|awk 'NR==2'|wc -l" ################################# copy模块复制 文件 到 远端 ansible sh_dev -m copy -a "src=/etc/ansible/hosts dest=/tmp/test.txt backup=yes" ansible sh_dev -m copy -a "src=/etc/ansible/hosts dest=/tmp/test.txt owner=shop group=shdev mode=777" # 写内容到远端 ansible sh_dev -m copy -a "content='123' dest=/tmp/1.log owner=shop group=shop mode=600 backup=yes" # EXAMPLES: - name: Copy file with owner and permissions copy: src: /srv/myfiles/foo.conf dest: /etc/foo.conf owner: www group: wwww mode: '0644' ################################# yum 模块 # 状态 state removed # 移除 installed # 默认安装当前时间最新版本,当已安装了后,不会再更新 laster # 安装最新版 ansible sh_dev -m yum -a "name=rsync,nfs-utils state=installed" ansible sh_dev -m yum -a "name=rsync state=latest" ################################# service 服务模块 # 状态 state started 启动 stopped 停止 restarted 重启 reload 重载 enabled 开机自启动 ansible sh_dev -m service -a "name=rsync state=started enabled=yes" ################################# script模块 ansible sh_dev -m script -a "/tmp/123.sh" # file模块 不能copy文件 # 可用参数 state = absent, directory, file, hard, link, touch # 状态 directory # 创建目录 touch # 创建文件 link # 创建链接文件 path # 指定远程主机目录或文件信息 recurse # 递归授权 mode owner group # 设置权限 用户 用户组 # 创建目录 写上权限比较规范,不写默认755 644 ansible sh_dev -m file -a "path=/tmp/12311 state=directory" ansible sh_dev -m file -a "path=/tmp/12311 state=directory mode=777 group=shdev" # 删除文件 ansible sh_dev -m file -a "path=/tmp/12311 state=directory state=absent" # 创建文件 ansible sh_dev -m file -a "path=/tmp/yaohong state=touch mode=644 group=shdev" ansible sh_dev -m file -a "src=/tmp/yaohong path=/tmp/yaohong_link state=link" # 递归更改权限/ ansible sh_dev -m file -a "path=/tmp/12311 state=directory owner=shop group=shop mode=755 recurse=yes" ################################ group 模块 # 创建组 # 状态 state=present # 默认状态,创建 state=absent # 移除远端主机上的组 # 可用参数 name # 指定创建的组名 git # 指定创建组的 git ansible sh_dev -m group -a "name=honghong gid=888" - name: Ensure group "somegroup" exists group: name: somegroup gid:1200 state: present ################################ group 模块 # 创建用户 uid=888 gid=888 group=honghong 或 888 groups # 附加组 shell # /bin/bash create_home # 默认床架家目录 password expires: 1422403387 # 到期时间 force=yes # 强制 generate_ssh_key #创建密钥 如果存在则不覆盖现有的密钥 如果配合 force=yes 则会覆盖 state=present # 默认状态,创建 state=absent # 移除远端主机上的用户 # 创建不能登陆的用户 ansible sh_dev -m user -a "name=hong uid=888 group=888 shell=/sbin/nologin create_home=no" echo "123"|openssl passwd -1 -stdin # 密码不能是明文 随机加密 echo "123"|openssl passwd -1 -salt "12345678" -stdin # md5 加密固定 # -1:表示采用的是MD5加密算法。 # -salt: 指定salt值,不使用随机产生的salt。 # 在使用加密算法进行加密时,即使密码一样,salt不一样,所计算出来的hash值也不一样,除非密码一样, # salt值也一样,计算出来的hash值才一样。salt为8字节的字符串 ansible sh_dev -m user -a 'name=yao uid=999 group=888 shell=/bin/bash password="$1$.hIZ4jGE$myTFSwbwin4zvcNQXNWCu1' ################################ crond 模块 分 时 日 月 周 minute=* hour=* day=* month=* weekday=* # 设定执行时间 0-59, *, */2, etc 默认是 * job='/bin/sh /tmp/123.sh' # 要执行的脚本 name="cron-1" # 指定名字 state=absent # 删除 # 添加计划任务 如果name存在,则会被覆盖 ansible sh_dev -m cron -a "name=cron-2 minute=* hour=* day=* month=* weekday=* job='/bin/sh /tmp/123.sh'" ansible sh_dev -m cron -a "name=cron-2 job='/bin/sh /tmp/123.sh'" # 通过name 删除计划任务 ansible sh_dev -m cron -a "name=cron-2 state=absent" # 注释一个计划任务(这里不能只写名字,必须写上job) ansible sh_dev -m cron -a "name=cron-2 job='/bin/sh /tmp/125.sh' disabled=yes" - name: Ensure a job that runs at 2 and 5 exists. Creates an entry like "0 5,2 * * ls -alh > /dev/null" cron: name: "check dirs" minute: "0" hour: "5,2" job: "ls -alh > /dev/null" ################################# mount 模块 state # 状态 present # 只把 挂载配置 写到 /etc/fstab 没鸟用 mounted # 挂载/并写到 /etc/fstab absent # 卸载并且清除 /etc/fstab unmounted # 卸载 但不清除 /etc/fstab ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted" ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=unmounted" ansible web -m mount -a "src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=absent" # 示例 - name: Mount up device by UUID mount: path: /home src: UUID=b3e48f45-f933-4c8e-a700-22a159ec9077 fstype: xfs opts: noatime state: present - name: Mount DVD read-only mount: path: /mnt/dvd src: /dev/sr0 fstype: iso9660 opts: ro,noauto state: present