ansible常用模块介绍与使用

2021/5/24 18:57:36

本文主要是介绍ansible常用模块介绍与使用,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

文章目录

    • 安装 ansible
    • 生成key 无密连接
    • ansible 管理那些主机
    • Ansible常用模块介绍与使用
      • ping模块
      • command模块
      • shell模块
      • file模块
      • copy模块
      • service 模块
      • cron 模块
      • fileSystem模块
      • mount模块
      • sysctl模块
      • yum模块
      • apt模块
      • pip模块
      • user模块
      • group模块
      • get_url模块
      • stat模块
      • script模块
      • unarchive模块
      • debug模块
      • wait_for模块
      • template模块

安装 ansible

apt update
apt install -y software-properties-common
apt-add-repository --yes --update ppa:ansible/ansible
apt install -y ansible

生成key 无密连接

ssh-keygen -f ~/.ssh/id_rsa -P '' -q
#拷贝ssh key到远程主机,ssh的时候就不需要输入密码了
ssh-copy-id root@192.168.0.74

ansible 管理那些主机

Host Inventory 配置文件:
默认的文件是:
/etc/ansible/hosts

Ansible常用模块介绍与使用

ping模块

ping这个模块主要用来测试能否连通目标服务器

ansible test -m ping

command模块

Command模块自如其名,就是一个很纯粹的命令执行模块,用于在受管机上进行命令的执行,其本身也可以配合很多二级参数和指令进行功能的扩展。不支持管道功能
需要注意的是,在没有特地指定ansible的-m参数的时候,默认调用的Module就是command。 下面是command常用的几个二级参数:
creates
判断,当该文件存在时,则该命令不执行
free_form
需要执行的Linux指令
chdir
在执行命令之前,先切换到该指定的目录
removes
判断,当该文件不存在时,则该选项不执行

ansible test -m command -a "hostname"
#判断是否有aa.txt 文件,如果存在就跳过不创建
ansible test -m command  -a "creates=aa.txt mkdir -p /opt/aa"
ansible test -m command -a "echo aa"
ansible test -m command -a "chdir=/opt mkdir aa"
#判断是否有aa.txt 文件,如果没有就不执行
ansible test -m command  -a "creates=aa.txt mkdir -p /opt/aa"

shell模块

这个模块的主要特点就是囊括了所有的command的功能和二级参数支持,并且支持管道。

ansible test -m shell -a "ps -ef | grep ansible | grep -v grep"

file模块

file模块主要是对于文件的一些简单操作,主要是创建或者权限设定,已经文件的存在判断等。
group
定义文件/目录的属组
mode
定义文件/目录的权限
owner
定义文件/目录的属主
path
必选项,定义文件/目录的路径
recurse
递归的设置文件的属性,只对目录有效
src
要被链接到的路径,只应用于state=link的情况
dest
被链接到的路径,只应用于state=link的情况
state
directory:如果目录不存在,创建目录
file:即使文件不存在,也不会被创建
link:创建软链接
hard:创建硬链接
touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
absent:删除目录、文件或者取消链接文件

#更改文件所有权,组和模式。 当使用八进制数指定模式时,第一个数字应始终为0
ansible test -m file -a "path=/root/su.yaml  owner=test group=test mode=0644"
#制作软连接
ansible test -m file -a "src=/root/su.yaml dest=/opt/aa.yaml state=link"
# touch创建文件,使用符号模式设置权限(相当于0644)
ansible test -m file -a "path=/root/aa.txt state=touch mode='u=rw,g=r,o=r'"
# touch创建文件,添加/删除一些权限
ansible test -m file -a "path=/root/aa.txt state=touch mode='u+rw,g-wx,o-rwx'"
# 创建一个目录,如果它不存在
ansible test -m file -a "path=/opt/aa state=directory mode=0755"

copy模块

在远程主机执行复制操作文件。
src
要复制到远程主机的文件在本地的地址,可以是绝对路径,也可以是相对路径。如果路径是一个目录,它将递归复制。在这种情况下,如果路径使用”/”来结尾,则只复制目录里的内容,如果没有使用”/”来结尾,则包含目录在内的整个内容全部复制,类似于rsync。
content
用于替代”src”,可以直接设定指定文件的值
dest
必选项。要将源文件复制到的远程主机的绝对路径,如果源文件是一个目录,那么该路径也必须是个目录
directory_mode
递归的设定目录的权限,默认为系统默认权限
force
如果目标主机包含该文件,但内容不同,如果设置为yes,则强制覆盖,如果为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes
others
所有的file模块里的选项都可以在这里使用

#copy 本地文件到远端服务器并添加权限
ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode=0644"
ansible test -m copy -a "src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=foo group=foo mode='u=rw,g=r,o=r'"
#拷贝aa目录下所有文件
ansible test -m copy -a "src=/root/aa/ dest=/opt"

service 模块

用于管理服务,记得针对Centos7就不要使用这个模块了。
arguments
给命令行提供一些选项
enabled
是否开机启动 yes|no, 要求状态(state)和启用(enabled)中至少有一个。
name
必选项,服务名称
runlevel
运行级别
sleep
如果执行了restarted,在则stop和start之间沉睡几秒钟
state
对当前服务执行启动,停止、重启、重新加载等操作(started,stopped,restarted,reloaded)

# 启动服务httpd,如果不运行
ansible test -m service -a "name=httpd state=started"
# 停止服务httpd,如果运行
ansible test -m service -a "name=httpd state=stopped"
# 启用服务httpd的示例操作,而不使用运行状态
ansible test -m service -a "name=httpd enabled=yes"

cron 模块

Ansible cron模块主要用于添加、删除、更新操作系统的crontab任务计划
name
任务计划名称
cron_file
替换客户端该用户的任务计划的文件
minute
分(0-59, * ,/2)
hour
时(0-23, * ,
/2)
day
日(1-31, * ,*/2)
month
月(1-12, * , */2)
weekday
周(0-6或1-7, *)
job
任何计划执行的命令,state要等于present
backup
是否备份之前的任务计划
user
新建任务计划的用户
state
指定任务计划present、absent

#基于cron模块,创建crontab任务计划,例如:让所有的后端服务器在每天的00:00点从192.168.0.250主机上用ntpdate同步实践,任务名称为:Ntpdate server for sync time,一定要注意这个定时服务,一定要在192.168.0.250配置好ntp服务器 
ansible test -m cron -a "minute=0 hour=0 day=* month=* weekday=* name='Ntpdate server for sync time' job='ntpdate 192.168.0.250'"
#backup=yes,表示开启备份,备份文件会存放在客户端/tmp/目录下面 
ansible test -m cron -a "minute=0 hour=0 day=* month=* weekday=* name='Ntpdate server for sync time' backup=yes job='ntpdate time.ntp.org'"
#删除crontab任务计划 
ansible test -m cron -a "name='Ntpdate server for sync time' state=absent"

fileSystem模块

FileSystem模块用于配置受管机的文件系统。改模块涉及高位操作,请务必小心,常用参数为:
dev
目标块设备
force
在一个已有文件系统的设备上强制创建
fstype
文件系统的类型
opts
传递给mkfs命令的选项

#格式话磁盘
ansible test -m filesystem -a "dev=/dev/sdb fstype=ext4"

mount模块

远程主机分区挂载
dump
存储(见fstab文件第5列)。注意,如果设置为null并且状态设置为present,它将停止工作,并且将在后续运行中进行重复条目。对Solaris系统没有影响。
fstype
必选项,文件系统类型,要求状态是present或mounted
name
必选项,挂载点
opts
传递给mount命令的参数
src
必选项,要挂载的设备路径。要求状态是present或mounted
state
必选项。选项如下:
present 只处理fstab中的配置
absent 删除挂载点
mounted 自动创建挂载点并挂载
unmounted 卸载

ansible test -m mount -a "name=/mnt/data src=/dev/sdb1 fstype=ext4 opts=ro state=present"

sysctl模块

远程linux主机sysctl配置模块
name
变量名
value

reload
文件被更新时,是否使用 sysctl -p reload 文件
state
是在文件中 移除(absent)或者设置(present)
sysctl_file
如果不是默认文件,指定其他文件
sysctl_set
使用sysctl 命令设置,不一定需要reload 文件

ansible-doc sysctl 可以查看到下面的示例

#以下是定义在yml格式文件中的例子:
# Set vm.swappiness to 5 in /etc/sysctl.conf
- sysctl:
    name: vm.swappiness
    value: 5
    state: present
# Remove kernel.panic entry from /etc/sysctl.conf
- sysctl:
    name: kernel.panic
    state: absent
    sysctl_file: /etc/sysctl.conf
# Set kernel.panic to 3 in /tmp/test_sysctl.conf
- sysctl:
    name: kernel.panic
    value: 3
    sysctl_file: /tmp/test_sysctl.conf
    reload: no
# Set ip forwarding on in /proc and do not reload the sysctl file
- sysctl:
    name: net.ipv4.ip_forward
    value: 1
    sysctl_set: yes
# Set ip forwarding on in /proc and in the sysctl file and reload if necessary
- sysctl:
    name: net.ipv4.ip_forward
    value: 1
    sysctl_set: yes
    state: present
    reload: yes

yum模块

这个模块是RedHat / CentOS作为远端节点的OS的时候,用的最多的。Yum是啥就不多说了,RedHat / CentOS包管理工具
使用yum软件包管理器管理软件包,其选项有:
config_file
yum的配置文件 (optional)
disable_gpg_check
关闭gpg_check (optional)
disablerepo
不启用某个源 (optional)
enablerepo
启用某个源(optional)
name
要进行操作的软件包的名字,默认最新的程序包,指明要安装的程序包,可以带上版本号,也可以传递一个url或者一个本地的rpm包的路径
state
状态(present,absent,latest),表示是安装还卸载
   present:默认的,表示为安装
   latest: 安装为最新的版本
   absent:表示删除

#安装http最新版本
ansible test -m yum -a ‘name=httpd state=latest’ 
##安装 rpm 包
ansible test -m yum -a ‘name=http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm state=present’

apt模块

这个模块是ubuntu作为远端节点的OS的时候,用的最多的。Apt是啥就不多说了,Ubuntu/Debian的包管理工具。
deb
用于安装远程机器上的.deb后缀的软件包(optional)
install_recommends
这个参数可以控制远程电脑上是否只是下载软件包,还是下载后安装,默认参数为true,设置为false的时候只下载软件包,不安装
update_cache
当这个参数为yes的时候等于apt-get update(optional)
name
apt要下载的软件包名字,支持name=git=1.6 这种制定版本的模式
state
状态(present,absent,latest),表示是安装还卸载
   present:默认的,表示为安装
   lastest: 安装为最新的版本
   absent:表示删除

# 在安装foo软件包前更新然后安装foo
ansible test -m apt -a "name=foo update_cache=yes"
# 移除foo软件包
ansible test -m apt -a "name=foo state=absent"
# 安装foo软件包
ansible test -m apt -a "name=foo state=present"
# 安装foo 1.0软件包
ansible test -m apt -a "name=foo=1.00 state=present"
# 安装nginx最新的名字为squeeze-backport发布包,并且安装前执行更新
ansible test -m apt -a "name=nginx state=latest default_release=squeeze-backports update_cache=yes"
# 只下载openjdk-6-jdk最新的软件包,不安装
ansible test -m apt -a "name=openjdk-6-jdk state=latest install_recommends=no"
# 安装所有软件包到最新版本
ansible test -m apt -a "upgrade=dist"
# 更新apt-get的list
ansible test -m apt -a "update_cache=yes"
# 3600秒后停止update_cache
ansible test -m apt -a "update_cache=yes cache_valid_time=3600"
# 安装远程节点上的/tmp/mypackage.deb软件包
ansible test -m apt -a "deb=/tmp/mypackage.deb"

pip模块

用于管理Python库依赖项,为了使用pip模块,必须提供参数name或者requirements
chdir
执行pip命令前cd进入的目录
name
要安装的Python库的名称或远程包的URL。
requirements
一个pip requirements.txt文件的路径,它应该是远程系统的本地文件,如果使用chdir选项,则可以将文件指定为相对路径。
version
指定的Python库的安装版本。
extra_args
额外的参数传递给pip。
executable
显式可执行文件或可执行文件的路径名,用于为系统中安装的特定版本的Python运行pip。 例如pip-3.3,如果系统中安装了Python 2.7和3.3,并且想要为Python 3.3安装运行pip。 它不能与“virtualenv”参数一起指定(在2.1中添加)。 默认情况下,它将采用适用于python解释器的版本。 pip3在python 3上,pip2或pip在python 2上。
virtualenv
要安装到的virtualenv目录的可选路径。 它不能与’executable’参数一起指定(在2.1中添加)。 如果virtualenv不存在,则将在安装软件包之前创建它。 可选的virtualenv_site_packages,virtualenv_command和virtualenv_python选项会影响virtualenv的创建。
virtualenv_command
用于创建虚拟环境的命令或路径名。 例如pyvenv,virtualenv,virtualenv2,~/bin /virtualenv,/usr/local/bin/virtualenv。
virtualenv_python
用于创建虚拟环境的Python可执行文件。 例如python3.5,python2.7。 未指定时,将使用用于运行ansible模块的Python版本。 当virtualenv_command使用pyvenv或-m venv模块时,不应使用此参数。
state
状态(present,absent,latest, forcereinstall),表示是安装还卸载
   present:默认的,表示为安装
   lastest: 安装为最新的版本
   absent:表示删除
   forcereinstall:"forcereinstall"选项仅适用于可ansible 2.1及更高版本。

# 安装bottle python包。
ansible test -m pip -a "name=bottle"
# 在0.11版安装bottle python包。
ansible test -m pip -a "name=bottle version=0.11"
# 使用远程协议(bzr +,hg +,git +,svn +)安装MyApp。 您不必在extra_args中提供'-e'选项。
ansible test -m pip -a "name=svn+http://myrepo/svn/MyApp#egg=MyApp"
# 使用远程协议(bzr +,hg +,git +)安装MyApp。
ansible test -m pip -a "name=git+http://myrepo/app/MyApp"
# 从本地压缩包安装MyApp
ansible test -m pip -a "name=file:///path/to/MyApp.tar.gz"
# 将bottle安装到指定的virtualenv中,继承全局安装的模块
ansible test -m pip -a "name=bottle virtualenv=/my_app/venv virtualenv_site_packages=yes"
# 使用Python 2.7将bottle安装到指定的virtualenv中
ansible test -m pip -a "name=bottle virtualenv=/my_app/venv virtualenv_command=virtualenv-2.7"
# 在用户主目录中安装bottle。
ansible test -m pip -a "name=bottle extra_args=--user"
# 安装指定的python requirements
ansible test -m pip -a "requirements=/my_app/requirements.txt"
# 在指定的virtualenv中安装指定的python requirements。
ansible test -m pip -a "requirements=/my_app/requirements.txt virtualenv=/my_app/venv"
# 安装指定的python requirements和自定义pip源URL
ansible test -m pip -a "requirements=/my_app/requirements.txt extra_args=-i https://example.com/pypi/simple"
# 专门为Python 3.3安装bottle,使用'pip-3.3'可执行文件。
ansible test -m pip -a "name=bottle executable=pip-3.3"
# 安装 bottle,如果已安装,强制重新安装
ansible test -m pip -a "name=bottle state=forcereinstall"

user模块

user模块是请求的是useradd, userdel, usermod三个指令
home
指定用户的家目录,需要与createhome配合使用。
groups
指定用户的属组。
uid
指定用的uid。
password
指定用户的密码。
注意指定password参数时,不能使用明文密码,因为后面这一串密码会被直接传送到被管理主机的/etc/shadow文件中,所以需要先将密码字符串进行加密处理。然后将得到的字符串放到password中即可。
name
指定用户名。
createhome
是否创建家目录 yes|no。
system
是否为系统用户。
remove
当state=absent时,remove=yes则表示连同家目录一起删除,等价于userdel -r。
state
是创建还是删除。(present,absent)
shell
指定用户的shell环境。
generate_ssh_key
是否为相关用户生成SSH密钥。 这不会覆盖现有的SSH密钥。
ssh_key_bits
可选择指定要创建的SSH密钥中的位数。
ssh_key_passphrase
设置SSH密钥的密码。 如果没有提供密码,SSH密钥将默认没有密码。
ssh_key_file
指定SSH密钥文件名(可选)。 如果这是一个相对的文件名,那么它将是相对于用户的主目录。
ssh_key_type
指定要生成的SSH密钥的类型(可选)。 可用的SSH密钥类型将取决于目标主机上的实现。

# 使用bash shell添加用户"test",将组"管理员"和"开发人员"附加到用户组
ansible test -m user -a "name=test shell=/bin/bash groups=admins,developers append=yes"
# 删除用户'test'
ansible test -m user -a "name=test state=absent remove=yes"
# 在~/.ssh/id_rsa中为用户test创建一个2048位的SSH密钥
ansible test -m user -a "name=test generate_ssh_key=yes ssh_key_bits=2048 ssh_key_file=.ssh/id_rsa"

group模块

goup模块请求的是groupadd, groupdel, groupmod 三个指令。
gid
指定用的gid。
name
指定用户名。
state
是创建还是删除。(present,absent)
system
如果是,则表示创建的组是系统组。

#来自Ansible Playbooks的示例组命令
ansible test -m group -a "name=somegroup state=present"

get_url模块

该模块主要用于从http、ftp、https服务器上下载文件(类似于wget),主要有如下选项:
sha256sum
下载完成后进行sha256 check;
timeout
下载超时时间,默认10s
url
下载的URL
url_password、url_username
主要用于需要用户名密码进行验证的情况
dest
将文件下载到哪里的绝对路径。如果dest是目录,则使用服务器提供的文件名,或者如果没有提供,将使用远程服务器上的URL的基本名称。
headers
以格式“key:value,key:value”为请求添加自定义HTTP标头。

# playbook的演示示例
- name: Download foo.conf
  get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    mode: '0440'

- name: Download file and force basic auth
  get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    force_basic_auth: yes

- name: Download file with custom HTTP headers
  get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    headers:
      key1: one
      key2: two

- name: Download file with check (sha256)
  get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    checksum: sha256:b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c

stat模块

获取远程文件状态信息,包括atime,ctime,mtime,md5,uid,gid等信息

ansible test -m stat -a "path=/etc/sysctl.conf"
#显示所有的ansible默认变量信息
ansible test -m setup

script模块

这个模块用于在受管机上执行sh脚本。

#一个简单的创建目录的例子,创建/usr/local/src/data/log 目录,如下:
# cat test1.sh 
#---------------------------------------------------
#!/bin/bash
if [ -z $1 ] || [ -z $2 ];then echo "Wrong,Please input two args" echo "Usage `basename $0` arguments arguments" exit 6
fi
mkdir -pv /usr/local/src/$1/$2
# cat createdir.yml 
---
- hosts: test
  user: root
  gather_facts: True
  tasks:
    - name: Create Dir in client server 
    script: /etc/ansible/test1.sh data log
#执行:
ansible-playbook createdir.yml

unarchive模块

用于解压文件,模块包含如下选项:
copy
在解压文件之前,是否先将文件从本地 ‘master’ 复制到远程主机,默认为yes。若为no,则要求目标主机上压缩包必须存在。此选项与remote_src互斥。
creates(path)
如果指定的绝对路径(文件或目录)已经存在,则将不会运行此步骤。
dest
远程主机上的一个路径,即文件解压的绝对路径。
keep_newer( yes | no)
不要替换比存档中的文件更新的现有文件。
group
解压后的目录或文件的属组
owner
解压后文件或目录的属主
remote_src
设置为yes指示已归档文件已在远程系统上,而不是在Ansible控制器本地。此选项与copy互斥。
list_files
如果为yes,则会列出压缩包里的文件,默认为no,2.0版本新增的选项
mode
解压后文件的权限
src (path / required)
如果remote_src=no(默认),则为要复制到目标服务器的存档文件的本地路径;可以是绝对的或相对的。
如果为remote_src=yes,则目标服务器上要解压缩的现有存档文件的路径。
如果remote_src=yes和src包含://,则远程计算机将首先从URL下载文件。(版本2.0)。仅在简单情况下,要完全下载支持,请使用get_url模块。

#name: 将foo.tgz解压缩到/var/lib/foo中
ansible test -m unarchive -a "src=foo.tgz dest=/var/lib/foo"
#name: 解压远程计算机上已存在的文件
ansible test -m unarchive -a "src=/tmp/foo.zip dest=/usr/local/bin remote_src=yes"
#name: 解压文档需要下载的文件(2.0中添加)
ansible test -m unarchive -a "src=https://example.com/example.zip dest=/usr/local/bin remote_src=yes"

debug模块

该模块 用于在调试中输出信息,对于调试变量或表达式非常有用,而不必停止播放本。与’when:'指令一起调试很有用。Windows目标也支持此模块。
msg
调试输出的消息
var
将某个任务执行的输出作为变量传递给debug模块,debug会直接将其打印输出
verbosity
debug的级别(默认是0级,全部显示)

示例:

---
- hosts: test
  gather_facts: F   #开启debug
  vars:
    war: "ps -ef | grep tomcat | grep -v grep | awk '{print $2}'"
  tasks:
    - name: stop tomcat 
      shell: nohup /bin/bash /tmp/stop_tomcat.sh& 
      ignore_errors: True 
      register: tomcat_out  #定义变量存储返回的结果
    - name: show 结果 #定义输出结果的task 
      debug: 
        var: tomcat_out 
        verbosity: 0 #这样写不光输出命令结果,还返回相关调试信息,只输出执行结果则使用 tomcat_out.stdout。
    - name: back war 
      shell: cp /home/admin/taobao-tomcat-production-7.0.59.3/deploy/ROOT.war /tmp/
    - name: remove romate dir 
      file: 
        path: /home/admin/taobao-tomcat-production-7.0.59.3/deploy/ROOT 
        state: absent
    - name: remove romate war 
      file: 
        path: /home/admin/taobao-tomcat-production-7.0.59.3/deploy/ROOT.war 
        state: absent
    - name: copy war 
      copy: 
        src: /home/admin/.jenkins/jobs/NET-hangfa/workspace/aecc_purchase_portal_web/xx.war  
        dest: /home/admin/taobao-tomcat-production-7.0.59.3/deploy/ROOT.war 
        owner: admin 
        group: wheel 
        mode: 0644
    - name: start tomcat 
      shell: nohup sh /home/admin/taobao-tomcat-production-7.0.59.3/bin/startup.sh &
    - name: tomcatalive 
      shell: "{{war}}" 
      register: check
    - name: show 
      debug: 
        var: check.stdout 
        verbosity: 0 #check.stdout 显示出的信息会看的更清晰点

wait_for模块

在playbook的执行过程中,等待某些操作完成以后再进行后续操作

connect_timeout

在下一个任务执行之前等待连接的超时时间
delay
等待一个端口或者文件或者连接到指定的状态时,默认超时时间为300秒,在这等待的300s的时间里,wait_for模块会一直轮询指定的对象是否到达指定的状态,delay即为多长时间轮询一次状态。
host
wait_for模块等待的主机的地址,默认为127.0.0.1
port
wait_for模块待待的主机的端口
path
路径,只有当这个文件存在时,下一任务才开始执行,即等待该文件创建完成
state
等待的状态,即等待的文件或端口或者连接状态达到指定的状态时,下一个任务开始执行。当等的对象为端口时,状态有started,stoped,即端口已经监听或者端口已经关闭;当等待的对象为文件时,状态有present或者started,absent,即文件已创建或者删除;当等待的对象为一个连接时,状态有drained,即连接已建立。默认为started
timeout
wait_for的等待的超时时间,默认为300秒

示例:

---
- hosts: test
  user: root
  tasks:
    - wait_for: 
        port: 8080 
        state: started #等待8080端口已正常监听,才开始下一个任务,直到超时
    - wait_for: 
        port: 8000 
        delay: 10 #等待8000端口正常监听,每隔10s检查一次,直至等待超时
    - wait_for: 
        host: 0.0.0.0 
        port: 8000 
        delay: 10 
        state: drained #等待8000端口直至有连接建立
    - wait_for: 
        host: 0.0.0.0 
        port: 8000 
        state: drained 
        exclude_hosts: 10.2.1.2,10.2.1.3 #等待8000端口有连接建立,如果连接来自10.2.1.2或者10.2.1.3,则忽略。
    - wait_for: 
        path: /tmp/foo #等待/tmp/foo文件已创建
    - wait_for: 
        path: /tmp/foo 
        search_regex: completed #等待/tmp/foo文件已创建,而且该文件中需要包含completed字符串
    - wait_for: 
        path: /var/lock/file.lock 
        state: absent #等待/var/lock/file.lock被删除
    - wait_for: 
        path: /proc/3466/status 
        state: absent #等待指定的进程被销毁
    - local_action: 
        wait_for port: 22 
        host: "{{ ansible_ssh_host | default(inventory_hostname) }}" 
        search_regex: OpenSSH 
        delay: 10 #等待openssh启动,10s检查一次

template模块

基于模板方式生成一个文件复制到远程主机(template使用Jinjia2格式作为文件模版,进行文档内变量的替换的模块。它的每次使用都会被ansible标记为”changed”状态。)
backup
如果原目标文件存在,则先备份目标文件 。默认值:no
src
在ansible控制器上的Jinja2格式化模板的路径。 这可以是相对或绝对的路径。
dest
将模板渲染到远程机器上的位置。
force
是否强制覆盖,默认为yes
owner
目标文件属主
group
目标文件属组
mode
目标文件的权限模式,模式可以被指定为符号模式(例如,u + rwx或u = rw,g = r,o = r)。

- name: 将文件模板到/etc/files.conf
  template:
    src: /mytemplates/foo.j2
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: '0644'

- name: 使用符号模式(相当于0644)来对文件进行模板化
  template:
    src: /mytemplates/foo.j2
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: u=rw,g=r,o=r

- name: 复制依赖于操作系统的named.conf版本。 通过对原始文件执行ls -Z /etc/named.conf获得的setype
  template:
    src: named.conf_{{ ansible_os_family }}.j2
    dest: /etc/named.conf
    group: named
    setype: named_conf_t
    mode: 0640

- name: 从模板创建DOS样式的文本文件
  template:
    src: config.ini.j2
    dest: /share/windows/config.ini
    newline_sequence: '\r\n'

- name: 通过visudo验证后,将新的sudoers文件复制到位
  template:
    src: /mine/sudoers
    dest: /etc/sudoers
    validate: /usr/sbin/visudo -cf %s

- name: 安全更新sshd配置,避免锁定自己
  template:
    src: etc/ssh/sshd_config.j2
    dest: /etc/ssh/sshd_config
    owner: root
    group: root
    mode: '0600'
    validate: /usr/sbin/sshd -t -f %s
    backup: yes


这篇关于ansible常用模块介绍与使用的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程