ansible 常用模块

转自https://www.cnblogs.com/trymybesttoimp/p/6223979.html

1. 查看支持的模块

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

这里我们看下ansible的支持的模块个数

复制代码
[root@localhost ~]# ansible-doc -l |wc -l   #查看支持的模块个数
1039
[root@localhost ~]# ansible --version        #查看我们的ansible版本号
ansible 2.3.1.0
  config file = /etc/ansible/ansible.cfg
  configured module search path = Default w/o overrides
  python version = 2.6.6 (r266:84292, Aug 18 2016, 14:53:48) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]
复制代码

2.获取模块的帮助

这里我们使用ansible-doc获取下command模块的使用方式。

[root@localhost ~]# ansible-doc command

3.1 command模块

command :作为ansible的默认模块,可以允许远程主机范围内的所有shell命令。

注意: 在command的命令中含有像`$ HOME'这样的变量和像``<“',`”>“, `“”“”,“”;“”和“”&“'将无法正常工作(如果需要这些功能,请使用[shell]模块)

复制代码
[root@localhost ~]# ansible 192.168.168.11* -m command -a 'ip addr show dev eth0'
192.168.168.115 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:29:8d:e2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.115/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::250:56ff:fe29:8de2/64 scope link 
       valid_lft forever preferred_lft forever

192.168.168.111 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:77:77:91 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.111/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::20c:29ff:fe77:7791/64 scope link 
       valid_lft forever preferred_lft forever
复制代码

3.2 script模块

功能:在远程主机上执行主控端的脚本,相当于scp+shell组合。

[root@localhost ~]# ansible all -m script -a "/home/test.sh 12 34"

3.3 shell模块

功能:执行远程主机的shell脚本文件

[root@localhost ~]# ansible all -m shell -a "/home/test.sh"

shell替代command执行

复制代码
[root@localhost ~]# ansible 192.168.168.11* -m shell -a 'ip addr show dev eth0'
192.168.168.111 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:77:77:91 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.111/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::20c:29ff:fe77:7791/64 scope link 
       valid_lft forever preferred_lft forever

192.168.168.115 | SUCCESS | rc=0 >>
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:50:56:29:8d:e2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.168.115/24 brd 192.168.168.255 scope global eth0
    inet6 fe80::250:56ff:fe29:8de2/64 scope link 
       valid_lft forever preferred_lft forever
复制代码

3.4 copy模块

功能: 实现主控端向目标主机copy文件。

复制代码
[root@localhost ~]# ansible all -m copy -a "src=/home/test.sh dest=/tmp/ owner=root group=root mode=0755"    
#src 主控端文件位置
#dest 被控端目标位置
#owner 文件复制过去后的所有者
#group 文件复制过去后的所属组
#mode 文件的权限设定,执行a+x这种方式
复制代码

3.5 stat模块

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

[root@localhost ~]# ansible all -m stat -a "path=/etc/sysctl.conf"

3.6 yum模块

功能: 安装软件包。

[root@localhost ~]# ansible all -m yum -a "name=httpd state=latest disable_gpg_check=yes enablerepo=epel"
#name 包名
#state (Choices: present, installed, latest, absent, removed)[Default: present]
#disable_gpg_check:禁止gpg检查
#enablerepo:只启动指定的repo

3.7 cron模块

功能:远程主机crontab配置

复制代码
[root@localhost ~]# ansible all -m cron -a "name='test' hour='2-5' minute='*/5' day='1' month='3,4' weekday='1' job='ls -l' user=tom"
192.168.168.115 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "test"
    ]
}
192.168.168.111 | SUCCESS => {
    "changed": true,
    "envs": [],
    "jobs": [
        "test"
    ]
}
复制代码

我们去被控主机看下生成的crontab作业

[root@localhost ~]# crontab  -l -u tom
#Ansible: test
*/5 2-5 1 3,4 1 ls -l

删除指定crontab

[root@localhost ~]# ansible all -m cron -a "name=test state=absent"

3.8 mount模块

功能: 挂载文件系统

[root@localhost ~]# ansible 192.168.168.111 -m mount -a "path=/mnt/data src=/dev/sd0 fstype=ext3 ots=ro state=present"

注:mount已经使用path代替了原来的name参数,但是name参数还是可以使用的。

3.9 service模块

功能: 服务管理

[root@localhost ~]# ansible all -m service -a "name=httpd state=restarted"    #启动服务
[root@localhost ~]# ansible all -m service -a "name=httpd state=running"      #查看服务状态
[root@localhost ~]# ansible all -m service -a "name=httpd state=stoped"       #停止服务

3.10 synchronize模块

– 使用rsync同步文件,将主控方目录推送到指定节点的目录下,其参数如下: 
– delete: 删除不存在的文件,delete=yes 使两边的内容一样(即以推送方为主),默认no 
– src: 要同步到目的地的源主机上的路径; 路径可以是绝对的或相对的。如果路径使用”/”来结尾,则只复制目录里的内容,如果没有使用”/”来结尾,则包含目录在内的整个内容全部复制 
– dest:目的地主机上将与源同步的路径; 路径可以是绝对的或相对的。 
– dest_port:默认目录主机上的端口 ,默认是22,走的ssh协议。 
– mode: push或pull,默认push,一般用于从本机向远程主机上传文件,pull 模式用于从远程主机上取文件。 
– rsync_opts:通过传递数组来指定其他rsync选项。

# 将控制机器上的src同步到远程主机上
- synchronize:
    src: some/relative/path
    dest: /some/absolute/path

# 同步传递额外的rsync选项
- synchronize:
    src: /tmp/helloworld
    dest: /var/www/helloworld
    rsync_opts:
      - "--no-motd"
      - "--exclude=.git"

  

3.11、template模块

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

# Example from Ansible Playbooks
- template:
    src: /mytemplates/foo.j2
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: 0644

# 同样的例子,但使用等效于0644的符号模式
- template:
    src: /mytemplates/foo.j2
    dest: /etc/file.conf
    owner: bin
    group: wheel
    mode: "u=rw,g=r,o=r"

  

3.12、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标头。

- name: Download foo.conf
  get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    mode: 0440

- name: Download file with custom HTTP headers
  get_url:
    url: http://example.com/path/file.conf
    dest: /etc/foo.conf
    headers: 'key:value,key:value'

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

  

13、file模块

file模块主要用于远程主机上的文件操作,file模块包含如下选项: 
– force:需要在两种情况下强制创建软链接,一种是源文件不存在但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no 
– group:定义文件/目录的属组 
– mode:定义文件/目录的权限 
– owner:定义文件/目录的属主 
– path:必选项,定义文件/目录的路径 
– recurse:递归的设置文件的属性,只对目录有效 
– src:要被链接的源文件的路径,只应用于state=link的情况 
– dest:被链接到的路径,只应用于state=link的情况 
– state: 
   directory:如果目录不存在,创建目录 
   file:即使文件不存在,也不会被创建 
   link:创建软链接 
   hard:创建硬链接 
   touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间 
   absent:删除目录、文件或者取消链接文件

# 更改文件所有权,组和模式。 当使用八进制数指定模式时,第一个数字应始终为0。
- file:
    path: /etc/foo.conf
    owner: foo
    group: foo
    mode: 0644

# touch创建文件,使用符号模式设置权限(相当于0644)
- file:
    path: /etc/foo.conf
    state: touch
    mode: "u=rw,g=r,o=r"

# touch创建文件,添加/删除一些权限
- file:
    path: /etc/foo.conf
    state: touch
    mode: "u+rw,g-wx,o-rwx"

# 创建一个目录,如果它不存在
- file:
    path: /etc/some_directory
    state: directory
    mode: 0755

14、set_fact

    set_fact模块可以自定义facts,这些自定义的facts可以通过template或者变量的方式在playbook中使用。如果你想要获取一个进程使用的内存的百分比,则必须通过set_fact来进行计算之后得出其值,并将其值在playbook中引用。

下面是一个配置mysql innodb buffer size的示例:

- name: Configure MySQL
  hosts: mysqlservers
  tasks: 
    - name: install MySql
      yum: name=mysql-server state=installed

    - name: Calculate InnoDB buffer pool size
      set_fact: innodb_buffer_pool_size_mb="{{ ansible_memtotal_mb / 2 }}"

    - name: Configure MySQL 
      template: src=templates/my.cnf dest=/etc/my.cnf owner=root group=root mode=0644 
      notify: restart mysql 

    - name: Start MySQL 
      service: name=mysqld state=started enabled=yes 
  handlers: 
    - name: restart mysql 
      service: name=mysqld state=restarted

my.cnf的配置示例:
# ` ansible_managed `
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted
security risks
symbolic-links=0
# Configure the buffer pool
innodb_buffer_pool_size = {{ innodb_buffer_pool_size_mb|int }}M
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

  

3.13 user模块

功能: 远程主机的用户管理

[root@localhost ~]# ansible all -m user -a "name=jerry comment=' doubi jerry'"   #添加用户 详细参数参考ansible-doc user
[root@localhost ~]# ansible all -m user -a "name=jerry state=absent remove=yes"  #删除用户

原文地址:https://www.cnblogs.com/guxiaobei/p/8316903.html