此章节的版本信息以下:html
ansible 2.4.2.0 python version = 2.7.5 (default, Aug 4 2017, 00:39:18) [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]
功能:尝试链接到主机,验证并返回pong成功。node
-对于Windows目标,请改用win_ping模块 -不使用icmp协议,使用ssh协议。
例子: #ansible db -m ping 7-db-3.hunk.tech | SUCCESS => { "changed": false, "ping": "pong" > 返回pong代表成功通信 }
功能:在远程节点上执行命令python
-变量 和操做符号 "<", ">", "|", ";" and "&" 不能正常工做。若是须要使用,请使用shell模块 -Ansible默认不指定模块时,将使用此模块。
- chdir 命令运行前先切换到此目录 - creates 条件判断,若是文件存在,将不执行后面的命令。 #ansible web -m command -a 'creates=/app echo /app not found' 6-web-1.hunk.tech | SUCCESS | rc=0 >> skipped, since /app exists = free_form The command module takes a free form command to run. There is no parameter actually named 'free form'. See the examples! - removes 条件判断,若是文件不存在,将不执行后面的命令。 #ansible web -m command -a 'removes=/app echo /app not found' 6-web-1.hunk.tech | SUCCESS | rc=0 >> /app not found - stdin 将命令的stdin直接设置为指定的值 - warn 若是 ansible.cfg 设置了开启警报, 将不会对这一行进行警报
功能:在远程节点上执行命令。与command模快使用一致,可是,变量 和操做符号 "<", ">", "|", ";" and "&" 能正常工做linux
下面2个例子对比能清晰的表示出不一样的地方 #ansible db -m command -a 'echo $RANDOM' 7-db-3.hunk.tech | SUCCESS | rc=0 >> $RANDOM #ansible db -m shell -a 'echo $RANDOM' 7-db-3.hunk.tech | SUCCESS | rc=0 >> 5159 这些复杂命令,即便使用shell也可能会失败,解决办法:写到脚本时, copy到远程,执行,再把须要的结果拉回执行命令的机器 #ansible web -m shell -a df | awk '{print $5}'
功能:把脚本复制到远程节点后,在远程节点本地运行脚本nginx
给定的脚本将经过远程节点上的shell环境进行处理。 这个模块在远程系统上不须要python,就像原始脚本同样。
#ansible web -m script -a './shell.sh' 6-web-1.hunk.tech | SUCCESS => { "Wed Jan 31 23:17:17 CST 2018", "/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin" }
功能:复制文件或目录到远程节点。默认会覆盖目标文件git
backup:在覆盖以前将原文件备份,备份文件包含时间信息。有两个选项:yes|no content:用于替代"src",能够直接设定指定文件的内容,至关于echo 重定向内容到文件 dest:必选项。要将源文件复制到的远程主机的绝对路径,若是源文件是一个目录,那么该路径也必须是个目录 directory_mode:递归的设定目录的权限,默认为系统默认权限 force:若是目标主机包含该文件,但内容不一样,若是设置为yes,则强制覆盖,若是为no,则只有当目标主机的目标位置不存在该文件时,才复制。默认为yes others:全部的file模块里的选项均可以在这里使用 src:要复制到远程主机的文件在本地的地址,能够是绝对路径,也能够是相对路径。若是路径是一个目录,它将递归复制。在这种状况下,若是路径使用"/"来结尾,则只复制目录里的内容,若是没有使用"/"来结尾,则包含目录在内的整个内容所有复制,相似于rsync。 #ansible dns -m copy -a 'src=/tmp/abc.txt dest=/app backup=yes' 复制目录时,斜线不要写 #ansible dns -m copy -a 'src=/tmp/dir1 dest=/app' 根据内容生成文件,至关于echo abc123 > /app/123.txt #ansible dns -m copy -a 'content="abc123" dest=/app/123.txt'
功能:从远程节点获取文件(只能是文件)到本地目录。默认会以主机清单中的主机名为目录存放获取到的文件github
#ansible all -m fetch -a 'src=/var/log/messages dest=/app' #tree -L 1 /app /app ├── 6-dns-1.hunk.tech ├── 6-web-1.hunk.tech ├── 7-db-3.hunk.tech ├── 7-web-0.hunk.tech └── 7-web-2.hunk.tech 注意,src=/var/log/mess* 这种通配符语法是不支持的
功能:设置远程节点的文件的文件属性web
force:须要在两种状况下强制建立软连接,一种是源文件不存在但以后会创建的状况下;另外一种是目标软连接已存在,须要先取消以前的软链,而后建立新的软链,有两个选项:yes|no group:定义文件/目录的属组 mode:定义文件/目录的权限 owner:定义文件/目录的属主 path:必选项,定义文件/目录的路径 recurse:递归的设置文件的属性,只对目录有效 src:要被连接的源文件的路径,只应用于state=link的状况 dest:被连接到的路径,只应用于state=link的状况 state: 操做方法 directory:若是目录不存在,建立目录 file:即便文件不存在,也不会被建立 link:建立软连接 hard:建立硬连接 touch:若是文件不存在,则会建立一个新的文件,若是文件或目录已存在,则更新其最后修改时间 absent:删除目录、文件或者取消连接文件。至关于rm -rf 建立空文件,相似于touch #ansible dns -m file -a 'path=/app/dir2/abc.txt state=touch mode=0666 owner=ftp' 建立空目录, 相似于mkdir -p #ansible dns -m file -a 'path=/app/dir2/dir3/dir4 state=directory mode=0666 owner=ftp' ├ dir2 │ └── dir3 │ └── dir4 建立软连接 #ansible dns -m file -a 'path=/app/abc.txt state=link src=/app/dir2/abc.txt' lrwxrwxrwx 1 root root 17 Feb 1 00:58 abc.txt -> /app/dir2/abc.txt
功能:设置远程节点主机名正则表达式
#ansible dns -m hostname -a 'name=6-dns-1.hunk.tech'
功能:管理计划任务shell
backup:对远程主机上的原任务计划内容修改以前作备份 cron_file:若是指定该选项,则用该文件替换远程主机上的cron.d目录下的用户的任务计划 day:日(1-31,*,*/2,……) hour:小时(0-23,*,*/2,……) minute:分钟(0-59,*,*/2,……) month:月(1-12,*,*/2,……) weekday:周(0-7,*,……) job:要执行的任务,依赖于state=present name:该任务的描述 special_time:指定何时执行,参数:reboot,yearly,annually,monthly,weekly,daily,hourly state:确认该任务计划是建立仍是删除 user:以哪一个用户的身份执行 #ansible dns -m cron -a 'name="test cron job" minute=*/2 job="/usr/bin/wall hello world"' 禁用某个计划任务 #Ansible: test cron job */2 * * * * /usr/bin/wall hello world 正确的写法:必须完整的写完,包括name等属性 #ansible dns -m cron -a 'disabled=yes name=None minute=*/3 job="/usr/bin/wall hello world"' #ansible dns -m cron -a 'disabled=yes job="/usr/bin/wall hello world"' > 这种写法是不对的,它会建立一条如下记录并禁用它 #Ansible: None #* * * * * /usr/bin/wall hello world 删除计划任务 #ansible dns -m cron -a 'state=absent name=None'
功能:使用yum包管理器来管理软件包
config_file:yum的配置文件 disable_gpg_check:关闭gpg_check disablerepo:不启用某个源 enablerepo:启用某个源 name:要进行操做的软件包的名字,也能够传递一个url或者一个本地的rpm包的路径 state:Whether to install (`present' or `installed', `latest'), or remove (`absent' or `removed') a package. (可选值: present, installed, latest, absent, removed) [Default: present] #ansible all -m yum -a 'name=tree state=present' #ansible all -a 'rpm -q tree' -o 6-web-1.hunk.tech | SUCCESS | rc=0 | (stdout) tree-1.5.3-3.el6.x86_64 6-dns-1.hunk.tech | SUCCESS | rc=0 | (stdout) tree-1.5.3-3.el6.x86_64 7-web-0.hunk.tech | SUCCESS | rc=0 | (stdout) tree-1.6.0-10.el7.x86_64 7-web-2.hunk.tech | SUCCESS | rc=0 | (stdout) tree-1.6.0-10.el7.x86_64 7-db-3.hunk.tech | SUCCESS | rc=0 | (stdout) tree-1.6.0-10.el7.x86_64
功能:配置管理yum源
reposdir: repo文件存放目录 file: repo文件名,默认为name的值 name: 惟一的repository ID gpgkey:设置gpgkey gpgcheck:设置gpg检查 enabled:设置开启关闭 bandwidth:控制带宽,0为无限 state:状态(present,absent description:描述 #ansible dns -m yum_repository -a 'state=present name=epel enabled=yes gpgcheck=yes description="Aliyun EPEL" baseurl="http://mirrors.aliyun.com/epel/6/$basearch,http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/" gpgkey="https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-6Server"'
功能:配置管理服务
arguments:给命令行提供一些选项 。可使用别名 args enabled:是否开机启动 yes|no name:必选项,服务名称 pattern:定义一个模式,若是经过status指令来查看服务的状态时,没有响应,就会经过ps指令在进程中根据该模式进行查找,若是匹配到,则认为该服务依然在运行 runlevel:运行级别 sleep:若是执行了restarted,在则stop和start之间沉睡几秒钟 state:对当前服务执行启动,中止、重启、从新加载等操做(started,stopped,restarted,reloaded) #ansible db -m service -a 'name=httpd state=started' > 一次只能操做一个服务
功能:收集关于远程主机的信息。
在playbooks里常常会用到的一个参数gather_facts就与该模块相关
--tree :将全部主机的输出信息保存到/tmp/目录下,以/etc/ansible/hosts里的主机名为文件名 ansible all -m setup -a 'filter=ansible_distribution_version' --tree /tmp/ filter :过滤关键字 #ansible db -m setup -a 'filter=ansible_distribution_version' gather_subset:按子集收集信息,值有all, min, hardware, network, virtual, ohai, facter。不包含请使用!号,如,!network
关键字 | 说明 | 返回值例子 |
---|---|---|
ansible_nodename | 节点名 | "6-dns-1.hunk.tech" |
ansible_fqdn | FQDN名 | "6-dns-1.hunk.tech" |
ansible_hostname | 主机短名称 | "6-dns-1" |
ansible_domain | 主机域名后缀 | "hunk.teh" |
ansible_memtotal_mb | 总物理内存 | "ansible_memtotal_mb": 222 |
ansible_swaptotal_mb | SWAP总大小 | "1023" |
ansible_processor | CPU信息 | Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz |
ansible_processor_cores | CPU核心数量 | 4 |
ansible_processor_vcpus | CPU逻辑核心数量 | 2 |
ansible_all_ipv4_addresses | 有所IPV4地址 | 192.168.0.200 |
ansible_all_ipv6_addresses | 全部IPV6地址 | |
ansible_default_ipv4 | 默认网关的网卡配置信息 | |
ansible_eth2 | 具体某张网卡信息 | 不一样系统名称须要变化 |
ansible_dns | DNS设置信 | |
ansible_architecture | 系统架构 | x86_64 |
ansible_machine | 主机类型 | x86_64 |
ansible_kernel | 内核版本 | "2.6.32-696.el6.x86_64" |
ansible_distribution | 发行版本 | "CentOS" |
ansible_distribution_major_version | 操做系统主版本号 | "6" |
ansible_distribution_release | 发行版名称 | "Final" |
ansible_distribution_version | 完整版本号 | "7.4.1708" |
ansible_pkg_mgr | 软件包管理方式 | "yum" |
ansible_service_mgr | 进行服务方式 | "systemd" |
ansible_os_family | 家族系列 | "RedHat" |
ansible_cmdline | 内核启动参数 | |
ansible_selinux | SElinux状态 | "disabled" |
ansible_env | 当前环境变量参数 | |
ansible_date_time | 时间相关 | |
ansible_python_version | python版本 | "2.6.6" |
ansible_lvm | LVM卷相关信息 | |
ansible_mounts | 全部挂载点 | |
ansible_device_links | 全部挂载的设备的UUID和卷标名 | |
ansible_devices | 全部/dev/下的正在使用的设备的信息 | |
ansible_user_dir | 执行用户的家目录 | "/root" |
ansible_user_gecos | 执行用户的描述信息 | "The root " |
ansible_user_gid | 执行用户的的GID | 0 |
ansible_user_id | 执行用户的的用户名 | "root" |
ansible_user_shell | 执行用户的shell类型 | "/bin/bash" |
ansible_user_uid | 执行用户的UID | 0 |
功能:管理用户帐号
选项太多,与useradd这类系统命令差很少 http://docs.ansible.com/ansible/latest/user_module.html name:用户名,可使用别名user #ansible db -m user -a 'name=hunk4 shell=/sbin/nologin system=yes comment="name is hunk"' 7-db-3.hunk.tech | SUCCESS => { "changed": true, "comment": "name is hunk", "createhome": true, "group": 996, "home": "/home/hunk4", "name": "hunk4", "shell": "/sbin/nologin", "state": "present", "system": true, "uid": 998 } 删除用户 #ansible db -m user -a 'name=hunk4 state=absent' 7-db-3.hunk.tech | SUCCESS => { "changed": true, "force": false, "name": "hunk4", "remove": false, "state": "absent" } 修改用户指定信息 #ansible db -m user -a 'name=hunk4 state=present comment=" hunk is my"' remove:删除用户时一并删除用户家目录,须要与state=absent一块儿使用 #ansible db -m user -a 'name=hunk3 state=absent remove=yes' 7-db-3.hunk.tech | SUCCESS => { "changed": true, "force": false, "name": "hunk3", "remove": true, "state": "absent" } state:操做方法。(present , absent) groups: 添加辅助组 group: 指定用户的主组 如下这个例子注意看,group和groups所表明的含义不一样。 - name: add user user: name={{ username }} group=ftp groups={{ groupname }}
功能:添加组或删除组
group模块请求的是groupadd, groupdel, groupmod 三个指令. 用法都是差很少的。
功能:从 HTTP, HTTPS, or FTP 下载文件
checksum:下载完成后进行checksum;格式: e.g. checksum="sha256:D98291AC[...]B6DC7B97".值有sha1, sha224, sha384, sha256, sha512, md5 timeout:下载超时时间,默认10s url:下载的URL url_password、url_username:主要用于须要用户名密码进行验证的状况 use_proxy:是事使用代理,代理需事先在环境变动中定义 force:yes目标存在时是否下载,no目标文件不存在时下载 backup:建立一个包含时间戳信息的备份文件 #ansible dns -m get_url -a 'dest=/app/ url="https://github.com/bennojoy/nginx/archive/master.zip"' #ansible dns -m get_url -a 'dest=/app/ELS.txt checksum=sha1:8c9e20bd25525c3ed04ebaa407097fe875f02b2c url="ftp://172.18.0.1/pub/Files/ELS.txt" force=yes' 6-dns-1.hunk.tech | SUCCESS => { "changed": false, "checksum_dest": "8c9e20bd25525c3ed04ebaa407097fe875f02b2c", "checksum_src": "8c9e20bd25525c3ed04ebaa407097fe875f02b2c",
功能:自定义消息失败
- fail: msg: "The system may not be provisioned according to the CMDB status." when: cmdb_status != "to-be-staged" 默认返回 'Failed as requested from task'
功能:替换一个文件中特定的行,或者使用一个反引用的正则表达式替换一个现有的行。
只有找到的最后一行将被替换 backup:建立一个包含时间戳信息的备份文件 backrefs: 为no时,若是没有匹配,则添加一行line。若是匹配了,则把匹配内容替被换为line内容。 为yes时,若是没有匹配,则文件保持不变。若是匹配了,把匹配内容替被换为line内容。 insertafter:配合state=present。该行将在指定正则表达式的最后一个匹配以后插入。一个特殊的价值是在EOF; EOF用于在文件的末尾插入行。若是指定的正则表达式没有匹配,则将使用EOF insertBefore:state=present。该行将在指定正则表达式的最后一个匹配以前插入。 BOF用于在文件的开头插入行。若是指定的正则表达式不匹配,则该行将被插入到文件的末尾。不能使用backrefs valiate:在保存sudoers文件前,验证语法,若是有错,执行时,会报出来,从新编辑playbook regexp: 正则表达式 # Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path' - lineinfile: path: /etc/selinux/config regexp: '^SELINUX=' line: 'SELINUX=enforcing' - lineinfile: path: /etc/sudoers state: absent regexp: '^%wheel' - lineinfile: path: /etc/hosts regexp: '^127\.0\.0\.1' line: '127.0.0.1 localhost' owner: root group: root mode: 0644 - lineinfile: path: /etc/httpd/conf/httpd.conf regexp: '^Listen ' insertafter: '^#Listen ' line: 'Listen 8080' - lineinfile: path: /etc/services regexp: '^# port for http' insertbefore: '^www.*80/tcp' line: '# port for http by default' # Add a line to a file if it does not exist, without passing regexp - lineinfile: path: /tmp/testfile line: '192.168.1.99 foo.lab.net foo' # Fully quoted because of the ': ' on the line. See the Gotchas in the YAML docs. - lineinfile: path: /etc/sudoers state: present regexp: '^%wheel\s' line: '%wheel ALL=(ALL) NOPASSWD: ALL' # Yaml requires escaping backslashes in double quotes but not in single quotes - lineinfile: path: /opt/jboss-as/bin/standalone.conf regexp: '^(.*)Xms(\\d+)m(.*)$' line: '\1Xms${xms}m\3' backrefs: yes # Validate the sudoers file before saving - lineinfile: path: /etc/sudoers state: present regexp: '^%ADMIN ALL=' line: '%ADMIN ALL=(ALL) NOPASSWD: ALL' validate: '/usr/sbin/visudo -cf %s'
功能:替换一个文件中符合匹配的全部行,或者使用一个反引用的正则表达式替换全部的行。
- replace: path: /etc/selinux/config regexp: '^SELINUX=.*' replace: 'SELINUX=disabled' # Before 2.3, option 'dest', 'destfile' or 'name' was used instead of 'path' - replace: path: /etc/hosts regexp: '(\s+)old\.host\.name(\s+.*)?$' replace: '\1new.host.name\2' backup: yes # Replace after the expression till the end of the file (requires >=2.4) - replace: path: /etc/hosts regexp: '(\s+)old\.host\.name(\s+.*)?$' replace: '\1new.host.name\2' after: 'Start after line.*' backup: yes # Replace before the expression till the begin of the file (requires >=2.4) - replace: path: /etc/hosts regexp: '(\s+)old\.host\.name(\s+.*)?$' replace: '\1new.host.name\2' before: 'Start before line.*' backup: yes # Replace between the expressions (requires >=2.4) - replace: path: /etc/hosts regexp: '(\s+)old\.host\.name(\s+.*)?$' replace: '\1new.host.name\2' after: 'Start after line.*' before: 'Start before line.*' backup: yes - replace: path: /home/jdoe/.ssh/known_hosts regexp: '^old\.host\.name[^\n]*\n' owner: jdoe group: jdoe mode: 0644 - replace: path: /etc/apache/ports regexp: '^(NameVirtualHost|Listen)\s+80\s*$' replace: '\1 127.0.0.1:8080' validate: '/usr/sbin/apache2ctl -f %s -t' - name: short form task (in ansible 2+) necessitates backslash-escaped sequences replace: dest=/etc/hosts regexp='\\b(localhost)(\\d*)\\b' replace='\\1\\2.localdomain\\2 \\1\\2' - name: long form task does not replace: dest: /etc/hosts regexp: '\b(localhost)(\d*)\b' replace: '\1\2.localdomain\2 \1\2'
模块太多了,这里仅仅是列出范例用法。
请使用ansible-doc或者去官网吧。http://docs.ansible.com/