about ansible 02

上一章节介绍了ansible playbook的一些编写规范,这一章节就来介绍一下ansible常用模块。
  1. file 模块
在目标主机创建文件或目录,并赋予其权限 
- name: create a file
# 代表在目标主机/root 下创建一个 foo.txt 的文件,属主属组都为 foo, 权限为 0755
  file: 'path=/root/foo.txt state=touch owner=foo group=foo mode=0755'
  1. copy 模块
实现 ansible服务端到被管理主机之间的文件传送
"""
remote_src : src 在本地还是在远端(copy源也可以在远端) 
False 本地, True 远端当前 
remote_src 还不支持递归 ,copy(Choices: True, False) [Default: no]
src: 源文件 
dest: 目标文件
mode: 权限
force: 如果内容不同,是否启用强制覆盖 (Choices: yes, no) [Default: yes]
"""
  
- name: copy a file 
  copy: 'remote_src=no src=roles/testbox/files/foo.sh dest=/root/foo.sh mode=0644 force=no' 
  1. stat 模块
获取远程文件的状态信息
-name: check file exists 
 stat: '/root/foo.sh'
  register: script_stat   # 判断 /root/foo.sh 是否存在,register 会把结果保存在 script_stat 这个变量中
  1. debug 模块
打印语句到ansible执行输出
-debug: msg=foo.sh exists 
 when: script_stat.stat.exists
#  这两句语句合起来的意思就代表,当 foo.sh 存在的时候,就打印输出 foo.sh exists 
  1. command/shell 模块
都是用来执行命令主机命令行,不同之处在于shell 会调用 /bin/bahs 解释器,所以可以使用 管道符,重定向等操作,而command则不能,所以这里推荐使用 shell 模块
-name: run the foo.sh 
 command: "sh /root/foo.sh"
-name: run the foo.sh 
 shell: "echo 'test' > /root/test.txt "  
  1. packaging
调用目标主机的包管理工具进行安装(yum apt)  centos 用yum, ubuntu用apt
- name: ensure nginx is at the latest version 
  yum: pkg=nginx state=latest    # 使用yum安装nginx最新版本
- name: ensure nginx is at the latest version 
  agt: pkg=nginx state=latest  #
  1. service 模块
管理目标主机系统服务
-name: start nginx service 
 service:  name=nginx state=started  # 启动nginx服务
  1. template 模块
实现ansible到目标主机 jinja2 模板传输
-name: write the nginx config file 
 template: src=roles/testbox/templates/nginx.conf.j2 dest=/etx/nginx/nginx.conf  
  1. 总结
- name: print server name and user to remote testbox
  shell: "echo 'current {{user}} is logging {{server_name}}' > {{output}}"
- name: create a file
  file: 'path=/root/test.file state=touch mode=0755 owner=foo group=foo'
- name: copy a file
  copy: 'remote_src=no src=/home/deploy/testplaybooks/roles/testbox/file/foo.sh dest=/root mode=0644 force=yes'
- name: check foo.sh exists
  stat: 'path=/root/foo.sh'
  register: script_stat
- debug: msg='foo.sh exists'
  when: script_stat.stat.exists
- name: run foo.sh
  command: 'sh /root/foo.sh'
- name: ensure nginx is at the latest version
  yum: pkg=nginx state=latest
- name: write the nginx config file
  template: src=/home/deploy/testplaybooks/roles/testbox/templates/nginx.conf.j2 dest=/etc/nginx/nginx.conf
- name: start nginx service
  service: name=nginx state=started

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1)安装2)常用模块3)inventory4)playbook(role\tag\template)5) yaml...
    秦记阅读 9,825评论 2 5
  • ansible介绍ansible常用模块使用playbooktemplates,模板条件测试和循环迭代roles,...
    哈喽别样阅读 5,219评论 0 3
  • 运维核心工作: 操作系统安装(物理机、虚拟机)--> 应用程序包部署(安装、配置、服务启动 )--> 批量操作 -...
    Net夜风阅读 5,526评论 0 4
  • 一、简介 在Linux自动化运维中,常见的自动化运维工具可分为需要安装终端的puppet、func和不需要安装终端...
    小尛酒窝阅读 8,012评论 0 6
  • 1.安装ansiblesudo yum install epel-releasesudo yum ins...
    小小全_阅读 3,666评论 0 1