ansible系列第二篇(模块使用)

ansible系列第二篇(模块使用)

  • 模块使用

    • 设置ansible提权

    在hosts文件加入sudo提权的密码:

    18.18.23.102  ansible_become_pass='passwd'

    执行: ansible test -S -R root -m shell -a "ls -l /"

    • 查看ansible有那些模块:

    ansible-doc -l

    • 获取各个模块详细帮助信息

      ansible-doc -s ping

    • ping模块:

    ansible test -m ping

    • 从受控主机拉取文件:

    调用模块并传入模块所需要的参数

    ansible test -m fetch -a "src=/etc/passwd dest=/automation/matchine"

    ansible会自动在管控端创建各个主机对应的目录。

  • 文件操作模块

    查看模块的帮助方式: ansible-doc -s modulename

    • copy模块

    • 常用参数

      src参数: copy文件或目录。

      dest参数: 指定文件被拷贝到远程主机的哪个目录中。

      content参数: 使用content直接指定文件内容。

      force参数: 默认值yes,覆盖。为no时,不执行覆盖操作。

      backup参数: yes,先备份,再拷贝文件到远程主机。

      mode参数: 文件权限。

      owner参数: 文件属主。

      group参数: 文件属组。

    • file模块

    • 常用参数:

      path参数: 指定文件或目录。

      state参数: absent,删除文件或目录。touch文件,link创建软链接文件。

      recurse参数: yes时可递归修改目录中文件的属性。

    例如:
      创建test.txt文件:
       ansible test -m file -a "path=/home/yuan/test.txt state=touch"
       ansible test -S -R root -m file -a "path=/opt/abc.txt state=touch"
      删除test.txt文件:
       ansible test -m file -a "path=/home/yuan/test.txt state=absent"
       ansible test -S -R root -m file -a "path=/opt/abc.txt state=absent"
      创建目录hello:
       ansible test -m file -a "path=/home/yuan/hello state=directory" 
    
      创建软链接文件:
      软链接名为go,
      ansible test -m file "path=/home/yuan/bin/linkfile state=link src=/home/yuan/services/go/bin/gofile"
    
      递归创建目录并递归修改属主和属组:
       ansible test -S -R root -m file -a "path=/abc/test/aaa state=directory owner=yuan group=yuan recurse=yes"
    • blockinfile模块

    在指定文件插入文本.

    • 常用参数:

      path:文件路径

      block: 指定文本

      state: 执行插入,更新,删除操作。

      backup: 是否在修改文件之前对文件进行备份。

      create: 文件不存在,是否创建文件。

    • lineinfile模块

    查看一行文本是否存在于指定文件中或从文件中删除指定文本。

    • find模块:

    在被控机查找符合条件的文件。

    • 常用参数:

      paths: 在哪个目录查找。

      recurse: 递归查找。

      hidden: yes时可查找隐藏文件。

      file_type: 指定文件类型。

      patterns:通配符或正则表达式匹配文件。

      contains: 根据文章内容查找文件。

      age: 根据时间范围查找文件。

      size: 根据文件大小查找文件。

    • replace模块:

    根据指定的正则表达式替换文件中的字符串。

  • 命令行模块:

    • command模块:

    在被控机上执行命令。

    • 参数:

      free_form:指定远程执行命令

      chdir: 指定一个目录。

      creates: 指定文件存在就不执行。

      removes: 指定文件不存在,就不执行对应命令。

    • shell模块

    在被控机执行命令时,会经过被控机的/bin/sh程序处理。

    • script模块

    在被控机上执行管控机上的脚本。

    参数和command模块类似。

    例如: $ ansible test -m script -a "/home/yuan/abc.sh"

  • 系统类模块

    • cron模块

    管理被控机的计划任务。

    • 参数:

      minute: 设定分钟。

      hours: 设定小时。

      day: 设定天数

      month: 设定月

      weekday: 设定周几

      special_time: @开头的时间设定格式使用该参数设置。可用值:reboot,yearly或annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时).

      user: 设置计划任务属于哪个用户。

      job: 指定任务中需要执行的命令或脚本。

      name: 设置计划任务名称。

      state: 根据任务名称修改或删除任务。

    例如:
      给普通用户设置计划任务:
       ansible test -m cron -a "name='echo' minute=1 hour=* job='echo xiao3 >> /home/yuan/abc.txt'"
      配置重启时执行该计划任务:
       ansible test -S -R root -m cron -a "name='reboot' special_time=reboot job='echo reboot xiao3 >> /home/yuan/abc.txt'"
      删除计划任务:
       ansible test -S -R root -m cron -a "name='reboot' state=absent"
    • service模块

    管理被控机的服务。service模块和systemd模块类似。

    • 参数:

      name: 服务名称

      state: 指定服务的状态。started,stopped,restarted,reloaded.

      enabled: 指定是否将服务设置为开机自启。

      例如:
      启动mysql服务:
      ansible test -S -R root -m systemd -a "name=mysql state=started
      设置开机自启:
      ansible test -S -R root -m systemd -a "name=mysql enabled=yes"
    • user模块:
      管理被控机的用户。
    • group模块:
      管理被控机的组。
  • 包管理模块:

    • yum_repository模块
      管理被控机为centos系列的yum仓库。
    • yum模块:
      在被控机为centos系列通过yum源管理软件包。
    • apt模块:
      在被控机为debian系列通过apt源管理软件包
      例如:
      安装tree命令:
      ansible test -S -R root -m apt -a "name=tree state=latest update_cache=yes"

      微信公众号: ansible系列第二篇(模块使用)

    欢迎各位大佬投稿和关注,非常感谢,如对文章内容有什么意见,也可以多多指出。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章