当前位置:首页 > 编程笔记 > 正文
已解决

Ansible中常用模块

来自网友在路上 169869提问 提问时间:2023-10-31 03:03:22阅读次数: 69

最佳答案 问答题库698位专家为你答疑解惑

目录

一、Ansible实现管理的方式

二、Ad-Hoc执行方式中如何获得帮助

三、Ansible命令运行方式及常用参数

四、Ansible的基本颜色代表信

五、Ansible中的常用模块

1、command模块

2、shell模块、script模块

3、copy模块、fetch模块

4、file模块

5、archive模块、unarchive模块

6、hostname模块、cron模块


 

一、Ansible实现管理的方式

Ad-Hoc 利用ansible命令直接完成管理,主要用于临时命令使用场景
playbook ansible脚本,主要用于大型项目场景,需要前期的规划

二、Ad-Hoc执行方式中如何获得帮助

ansible-doc 显示模块帮助的指令
格式
ansible-doc [参数] [模块...]常用参数
-l   列出可用模块
-s   显示指定模块的playbook片段
vim test.yml
cat test.yml
ansible-playbook test.yml

ansible-doc
ansible-doc -l | wc -l
ansible-doc -l | grrep shell

 

三、Ansible命令运行方式及常用参数

参数功能-i指定hosts文件路径,默认在/etc/ansible/hosts-m指定使用的module名称,默认command模块-a指定模块参数-k(小写)提示输入ssh密码,并非基于ssh密钥认证-K(大写)提示输入sudo密码-b使用sudo执行命令-become-user=指定sudo的用户-f,-forks=NUMNUM默认是整数5,指定fork开启同步进程的个数-u指定远程主机的执行用户-v详细模式,如果执行成功,输出详细结果,-vv -vvv更详细过程-C预执行检测-T执行命令的超时时间,默认10s--list显示主机列表,也可以用--list-hosts--version显示版本
ansible --version
ansible all -m shell -a 'whoami' -C
ansible all -m shell -a 'whoami' -u test
ansible all -m shell -a 'whoami' -utest
ansible all -m shell -a 'whoami'
ansible all -m shell -a 'whoami' -b -become-user=yyl
ansible all -m shell -a 'whoami' -b -become-user=yyl -K

 

 

四、Ansible的基本颜色代表信

绿色执行成功但未对远程主机做任何改变黄色执行成功并对远程主机做改变红色执行失败

五、Ansible中的常用模块

1、command模块

在远程主机执行命令,此模块为默认模块

常用参数功能chdir执行命令前先进入到指定目录cmd运行命令指定creates如果文件存在将不运行removes如果文件存在将运行
ansible all -m command -a 'chdir=/mnt pwd'
ansible all -m command -a 'chdir=/mnt cmd=pwd'
ansible all -m command -a 'chdir=/mnt  creates=/etc/passwd cat passwd'
ansible all -m command -a 'chdir=/mnt  removes=/etc/passwd cat passwd'
ansible all -m command -a 'chdir=/mnt  removes=/mnt/file cat passwd'
ansible all -m command -a 'chdir=/mnt touch file'
ansible all -m command -a 'chdir=/mnt  creates=/mnt/file pwd'
ansible all -m command -a 'chdir=/mnt  removes=/mnt/file pwd'

注意
Linux中的很多通配符在command模块中不支持

ansible all -m command -a "chdir=/mnt touch file{1..10}"

 

2、shell模块、script模块

(1)shell模块
shell模块与command模块类似

常用参数功能chdir执行命令前先进入到指定目录cmd运行命令指定creates如果文件存在将不运行removes如果文件存在将运行executable指定执行环境,默认为sh
ansible all -m shell -a 'chdir=/mnt/ touch file{1..3}'
ansible all -m shell -a 'chdir=/mnt/ ls -ld /mnt'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'

ansible all -m shell -a 'ps ax | grep $$'
ansible all -m shell -a 'executable=/bin/bash ps ax | grep $$'

(2)script模块
在ansible主机中写好的脚本在受控主机中执行

vim test.sh
cat test.sh
ansible all -m script -a "test.sh"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'

3、copy模块、fetch模块

(1)copy模块
从ansible主机复制文件到受控主机

参数功能src源文件dest目的地文件owner指定目的地文件所有人group指定目的地文件所有组mode指定目的地文件权限backup=yes当受控主机中存在文件时备份原文件content指定文本内容直接在受控主机中生成文件

复制当前目录的test.sh到受控主机的/mnt下,文件所有人为yyl,权限为755 

ansible all -m copy -a 'src=test.sh dest=/mnt/ owner=yyl mode=755'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'

在ansible主机上修改文件内容,开启备份,再次发送  先修改一下文件

ansible all -m copy -a 'src=test.sh dest=/mnt/ owner=yyl mode=755 backup=yes'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt'

 直接输入文件内容,在受控主机上生成文件

ansible all -m copy -a "content='hello westos\nhello linux\n' dest=/mnt/test.sh owner=yyl mode=755"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

(2)fetch模块
从受控主机把文件复制到ansible主机,但不支持目录

参数功能src受控主机的源文件dest本机目录flat基本名称功能,单纯只要文件,不要路径的层层目录
ansible all -m fetch -a "src=/mnt/test.sh dest=/home/ale/.ansible"

ansible all -m fetch -a "src=/mnt/test.sh dest=/home/ale/.ansible/ flat=yes"

4、file模块

设置文件的属性

参数功能path指定文件名称state指定操作状态
touch:建立
absent:删除
directory:递归目录
link:建立软链接
hard:建立硬链接mode设定权限owner设定文件用户group设定文件组src源文件dest目标文件recurse=yes递归更改
ansible all -m shell -a 'rm -fr /mnt/*'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

创建test.sh文件 

ansible all -m file -a 'path=/mnt/test.sh state=touch'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

删除 

ansible all -m file -a 'path=/mnt/test.sh state=absent'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

创建目录 

ansible all -m file -a 'path=/mnt/westos state=directory'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

ansible all -m file -a 'path=/mnt/westos/file1 state=touch'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

 递归更改目录权限

ansible all -m file -a 'path=/mnt/westos state=directory mode=777 recurse=yes'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

 软连接

ansible all -m file -a 'src=/mnt/file dest=/mnt/yyl state=link'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

硬链接

ansible all -m file -a 'src=/mnt/file dest=/mnt/yyl1 state=hard'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

 

5、archive模块、unarchive模块

(1)archive模块——压缩

参数功能path打包目录名称dest声称打包文件名称format打包格式owner指定文件所属人mode指定文件权限
ansible all -m archive -a "path=/mnt dest=/mnt/mnt.tar.gz format=gz owner=yyl mode=700"
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

 

(2)unarchive模块——解压缩

copy默认为yes,从ansible主机复制文件到受控主机
设定为no,从受控主机中寻找src源文件remote_src功能同copy且相反
设定为yes表示包在受控主机
设定为no表示包在ansible主机src压缩文件的路径dest受控主机目录mode解压后文件的权限<copy=yes>
tar zcf ansible.tar.gz  .
ls
ansible all -m unarchive -a 'src=./ansible.tar.gz dest=/mnt copy=yes'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

 

 提前在vm1,2上压缩一个不被限制的gz包 

ansible all -m unarchive -a 'src=/mnt/file.tar.gz  dest=/mnt copy=no'
ansible all -m shell -a 'chdir=/mnt/ ls -lR /mnt '

6、hostname模块、cron模块

(1)hostname模块
管理主机名称
常用参数:name:指定主机名称

ansible 192.168.67.112 -m hostname -a 'name=vm22'
ansible 192.168.67.112 -m shell -a 'hostname'
ansible 192.168.67.112 -m hostname -a 'name=vm1'
ansible 192.168.67.112 -m shell -a 'hostname'

 

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"Ansible中常用模块":http://eshow365.cn/6-28203-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!