Ansible的7个常用模块使用以Centos7.4为例

环境都为Centos 7.4主机

192.168.241.100管理节点  192.168.241.101被控端  192.168.241.102被控端

使用命令查看模块:

[root@liudongyi ~]# ansible-doc -l

ansible-doc –s copy 查看模块文档

官方的模块文档网址是:https://docs.ansible.com/ansible/latest/modules/modules_by_category.html

1.Shell模块:在目标主机执行shell命令。

- name: 将命令结果输出到指定文件
  shell: somescript.sh >> somelog.txt
- name: 切换目录执行命令
  shell:
    cmd: ls -l | grep log
    chdir: somedir/
- name: 编写脚本
  shell: |
      if [ 0 -eq 0 ]; then
         echo yes > /tmp/result
      else
         echo no > /tmp/result
      fi
  args:
    executable: /bin/bash

-a 设置参数 'ls'表示在webservers组中的主机都执行ls命令,把结果显示在下方('之间可以写多条shell命令用;相隔就可以')



2.copy:将文件复制到远程主机。

用法:

- name: 创建目录
  file:
    path: /etc/some_directory
    state: directory
    mode: '0755'
- name: 删除文件
  file:
    path: /etc/foo.txt
    state: absent
- name: 递归删除目录
  file:
    path: /etc/foo
    state: absent
[root@liudongyi ~]# ansible webservers -m copy -a "src=/etc/passwd dest=/mnt/passwd mode=777"

 -m 指定是copy模块  -a 指定参数    指定src源路径  dest目的路径  mode设置复制后的权限为777

我们查看一下复制后的文件:(复制的时候就把权限设置成为了777)

3.file:管理文件和文件属性。

- name: 创建目录
  file:
    path: /etc/some_directory
    state: directory
    mode: '0755'
- name: 删除文件
  file:
    path: /etc/foo.txt
    state: absent
- name: 递归删除目录
  file:
    path: /etc/foo
    state: absent

使用file模块我们把刚才的/mnt/passwd文件删除了

[root@liudongyi ~]# ansible webservers -m file -a "path=/mnt/passwd state=absent"

 -m 指定file模块  -a指定参数  path指定路径  state指定状态   (absent表示卸载删除,present,latest:表示安装 )

我们查看一下/mnt目录下是否还存在/mnt/passwd文件

创建一个目录:    state=指定目录类型

[root@liudongyi ~]# ansible webservers -m file -a "path=/mnt/ldylove state=directory"

查看是否创建了目录:可以看到是目录类型ldylove已经创建

4.yum:软件包管理

- name: 安装最新版apache
  yum:
    name: httpd
    state: latest
- name: 安装列表中所有包
  yum:
    name:
      - nginx
      - postgresql
      - postgresql-server
    state: present
- name: 卸载apache包
  yum:
    name: httpd
    state: absent 
- name: 更新所有包
  yum:
    name: '*'
    state: latest
- name: 安装nginx来自远程repo
  yum:
    name: http://nginx.org/packages/rhel/7/x86_64/RPMS/nginx-1.14.0-1.el7_4.ngx.x86_64.rpm
    # name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
    state: present

安装最新版的apache:state选的是latest是更新包包含安装  present表示安装  absent 卸载安装

[root@liudongyi ~]# ansible webservers -m yum  -a "name=httpd state=latest"

我们查看一下httpd是否安装成功:

我们可以看到我们安装成功了

在name后面可以直接加rpm的路径(http://nginx.org/packages/rhel/7/x86_64/RPMS/nginx-1.14.0-1.el7_4.ngx.x86_64.rpm

卸载掉httpd这个服务:

[root@liudongyi playbook]# ansible webservers -m yum -a "name=httpd state=absent "

查看是否卸载成功:

5、service/systemd  (service是Centos6    systemd是Centos7)管理服务

- name: 服务管理
  service:
    name: etcd
    state: started
    #state: stopped
    #state: restarted
    #state: reloaded
- name: 设置开机启动
  service:
    name: httpd
    enabled: yes
- name: 服务管理  
  systemd: 
	name=etcd 
	state=restarted 
	enabled=yes 
	daemon_reload=yes

启动httpd服务

[root@liudongyi ~]# ansible webservers -m systemd -a "name=httpd state=started "

我们查看一下httpd服务是否启动:

6、unarchive : 解压模块

- name: 解压
  unarchive: 
    src=test.tar.gz 
    dest=/tmp

7、debug:执行过程中打印语句。

- debug:
    msg: System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}

- name: 显示主机已知的所有变量
  debug:
    var: hostvars[inventory_hostname]
    verbosity: 4

执行命令时打印一下变量hostvars的内容

[root@liudongyi ~]# ansible webservers -m debug -a "var=hostvars"

显示主机清单中的自定义变量:比如我定义一个role=web1

[root@liudongyi ~]# vim /etc/ansible/hosts 

执行debug模块显示出我定义的变量信息role

[root@liudongyi ~]# ansible webservers -m debug -a "var=role"

针对于webservers定义一个组变量ldy:

我们查看会发现。webservers组内的所有主机都会打印lldy=520这个定义的变量

还可以定义所有组(all:vars)的变量

 所以任意一个组都可以进行

 

以上的7个模块比较常用!!至此结束!!

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值