ansible模块

roles

定义主机对应的角色,角色是一组按照目录组合的配置,ansible自动完成文件搜索,去找对应目录下的main.yml文件来执行,具体目录结构如下:

  • defaults/ 默认的变量保存在这个目录下
  • files/ 文件
  • templates/ 模板
  • tasks/ 任务
  • handlers/ 处理器
  • vars/ 变量
  • meta/ 角色本身的信息,如通过dependencies指令指定依赖
  • library/ 私有模块

ad-hoc

语法 ansible <pattern> [-f forks] [-m module] [-a args] ARGUMENTS pattern 组名,或者主机名,匹配hosts文件

该命令选项的作用分别为:

  • -i:指定 inventory 文件,使用当前目录下的 hosts
  • all:针对 hosts 定义的所有主机执行,这里也可以指定组名或模式
  • -m:指定所用的模块,我们使用 Ansible 内置的 ping 模块来检查能否正常管理远端机器
  • -u:指定远端机器的用户

常用模块

copy模块

该模块让你拷贝数据到远程主机

把主控端/root目录下的a.sh文件拷贝到到指定节点上

ansible 172.16.254.105 -m copy -a 'src=/root/a.sh dest=/tmp/ owner=root group=root mode=0755'

file模块

file模块能够创建文件和文件夹,删除文件和文件夹,以及修改相关的权限

目的:更改指定节点上/tmp/t.sh的权限为755,属主和属组为root  
命令:ansible all -m file -a "dest=/tmp/t.sh mode=755 owner=root group=root"

修改文件的所有组、人、权限。

- file: path=/etc/foo.conf owner=foo group=foo mode=0644

操作链接的案例

- file: src=/file/to/link/to dest=/path/to/symlink owner=foo group=foo state=link

参数化案例

- file: src=/tmp/{{ item.path }} dest={{ item.dest }} state=link
  with_items:
    - { path: 'x', dest: 'y' }
    - { path: 'z', dest: 'k' }

使用touch来创建一个空文件并定义权限

- 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"

cron模块

目的:在指定节点上定义一个计划任务,每隔3分钟到主控端更新一次时间  
命令:ansible all -m cron -a 'name="custom job" minute=*/3 hour=* day=* month=* weekday=* job="/usr/sbin/ntpdate 192.168.247.152"'

group模块

目的:在所有节点上创建一个组名为nolinux,gid为2014的组  
命令:ansible all -m group -a 'gid=2014 name=nolinux'

uesr模块

目的:在所有节点上创建一个用户名为nolinux,组为nolinux的用户  
命令:ansible all -m user -a 'name=nolinux groups=nolinux state=present'
删除用户  

命令:ansible all -m user -a 'name=nolinux state=absent remove=yes'

user模块用于创建用户和操作已经存在的用户

ansible ubuntu -m user -a "name=web-user" --sudo -K

「sudo」的方式执行名 -K 提示输入sudo密码

yum模块

目的:在指定节点上安装 apache 服务  
命令:ansible all -m yum -a "state=present name=httpd"
#state=latest 安装最新版本

shell模块

该模块让你在远程主机上执行shell命令,如果执行「ansible」命令时,默认就是「shell」模块

目的:在指定节点上安装 apache 服务  
命令:ansible testgroup -m shell -a 'yum -y install httpd'

command模块

目的:在指定节点上运行hostname命令
命令:ansible 192.168.247.152 -m command -a 'hostname'

raw模块

目的:在192.168.247.152节点上运行ifconfig命令
命令:ansible 192.168.247.152 -m raw-a 'ifconfig|eth0'

script模块

目的:在指定节点上执行/root/a.sh脚本(该脚本是在ansible主控端)  
命令:ansible 10.1.1.113 -m script -a '/root/a.sh'

service模块

目的:启动指定节点上的 httpd 服务,并让其开机自启动  
命令:ansible 192.168.247.152 -m service -a 'name=httpd state=restarted enabled=yes'


 ansible  192.168.247.152 -m service -a "name=httpd state=started"

state 状态值

  • running
  • started
  • stopped
  • restarted
  • reloaded

ping模块

该模块检查远程服务器是否存活

目的:检查指定节点机器是否还能连通  
命令:ansible 192.168.247.152 -m ping

get_url

目的:下载百度下的图标文件到节点的/tmp文件下
命令:ansible testgroup -m get_url -a 'url=https://www.baidu.com/favicon dest=/tmp'
#结果为error.html,但是证明了模块是可用的

stat模块

目的:获取远程文件状态信息,包括atime、ctime、mtime、md5、uid、gid等信息
ansible web -m stat -a 'path=/etc/sysctl.conf'

template模块

template使用了Jinja2格式作为文件模版,进行文档内变量的替换的模块。它的每次使用都会被ansible标记为”changed”状态。

setup模块

得到更多的初始化信息,使用「setup」模块

ansible  -m setup 172.16.254.105

也可以过滤一下信息

ansible web -m setup -a "filter=ansible_nodename"

包管理模块

安装包

 ansible testgroup -m yum -a "name=acme state=present"

安装指定的版本

ansible  testgroup -m yum -a "name=acme-1.5 state=present"

确保包安装最新版本

ansible testgroup  -m yum -a "name=acme state=latest"

卸载包

ansible webservers -m yum -a "name=acme state=absent"

state的状态值:

  • present
  • lastest
  • absent

ansible-playbook

sudo.yml文件定义

- hosts: ubuntu
  sudo: yes 
  tasks:
    - name: root家目录创建一个文件
      shell: 'touch /root/my.ddt'

当前执行的用户是「ubuntu」,需要执行的远程主机组是「ubuntu」,接着在/root的家目录创建一个文件。 因此需要「sudo」权限。

ansible-playbook -i ./hosts sudo.yml --ask-sudo-pass

选项 --ask-sudo-pass也可以用-K代替,也可以用key文件进行免密连接。

apt模块

「apache.yml」文件定义

- hosts: ubuntu
  sudo: yes
  tasks:
    - name: 安装apache
      apt: pkg=apache2 state=installed

说明

  • ubuntu 表示要执行的主机
  • sudo 以sudo身份运行
  • apt是模块名称

运行 ansible-playbook

ansible-playbook -i ./hosts apache.yml -K

使用sudo 安装apache。「apt」模块安装debain包

file模块

file模块的作用

  • file 模块可以改变文件的权限和所属用户组
  • file 模块可以创建目录
  • file 模块可以删除文件

删除文件

dest=/etc/apache2/sites-enabled/default state=absent

删除文件 「/etc/apache2/sites-enabled/default」 。当state是 absent的时候,表示删除文件

创建文件,并修改相关权限

dest=/usr/local/src/test mode=600 owner=www group=www state=touch
  • dest 目标文件或文件夹
  • mode 权限位
  • owner 所有人
  • group 所属组

file.yml文件

- hosts: ubuntu
  sudo: yes
  tasks:
    - name: 创建一个文件
      file: dest=/tmp/my_temp.txt mode=600 owner=www group=www state=touch

运行ansible-playbook

ansible-playbook -i ./hosts  file.yml  -K

apache服务

 - hosts: ubuntu
   sudo: yes 
   tasks:
    - name: 安装apache 服务
      apt: pkg=apache2 state=installed
    - name: 推送默认的配置
      copy: src=files/awesome-app dest=/etc/apache2/sites-available/awesome-app mode=0640 
    - name: 创建文档根目录
      file: dest=/var/www/awesome-app state=directory
    - name: 移除默认的虚拟主机
      file: dest=/etc/apache2/sites-available/000-default.conf state=absent
      notify:
        - restart apache

   handlers:
    - name: restart apache
      service: name=apache2 state=restarted

运行命令

 ansible-playbook config.yml -K

templates 模板

创建一个用户

- hosts: ubuntu
  sudo: yes
  vars:
    - user: "abc"
  tasks:
    - name: 创建 {{ user }}
      user: name="{{ user }}"

运行命令

ansible-playbook template.yml -K

创建用户并同步文件
templates/file文件内容

hello {{ name }}

template.yml文件内容

- hosts: ubuntu
      sudo: yes
      vars:
        - user: "ppkm"
        - name: "ajax"
        - filename: "dest.log"
      tasks:
        - name: 创建 {{ user }}
          user: name="{{ user }}"
      tasks:
        - name: 更新文件
          template: src=templates/file dest=/tmp/{{ filename }}

运行命令

ansible-playbook template.yml -K

查看服务器的文件 cat /temp/dest.log

hello ajax

nginx实例

nginx模板文件

user {{ work_user }};

worker_processes {{ worker_processes }};
pid /run/nginx.pid;

events {
worker_connections 768;
}

http {

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

}

上面的模板文件定义了2个变量「work_user」和「worker_processes」

nginx.yml文件定义

- hosts: ubuntu #这个是你选择的主机
#这个是变量
  vars:
    work_user: www-data
    worker_processes: 2
#sudo方式运行
  sudo: yes
  tasks:
#利用apt模块来操作
  - name: ensure nginx is installed
    apt: pkg=nginx state=installed
  - name: write the nginx config file
    template: src=templates/nginx.j2 dest=/etc/nginx/nginx.conf
#触发重启服务器
    notify:
    - restart nginx
  - name: ensure nginx is running
    service: name=nginx state=started
#这里的restart nginx 和上面的触发是配对的。这就是handlers的作用。
  handlers:
    - name: restart nginx
      service: name=nginx state=restarted

运行命令

ansible-playbook nginx.yml -K

condition

- hosts: ubuntu
  sudo: yes
  vars:
    epic: true
  tasks:
     - name: 如果是ubuntu
       shell: 'touch /tmp/ubuntu.txt'
       when: ansible_os_family == 'Debian'
     - name: 输出
       shell: echo "aaa" > /tmp/aaa.txt
       when: epic
     - name:  安装ntp 在debian上
       apt: name=ntp state=installed
       when: ansible_os_family == 'Debian'
     - name:  安装ntp 在RedHat上
       yum: name=ntp state=installed
       when: ansible_os_family == 'RedHat'

第一个表达式,当操作系统是「Debian」时,输入内容到 「/tmp/aaa.txt」。 第二,三个表示是,如操作系统是「Debian」,执行「apt」。 如是「RedHat」执行 「yum」

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 162,158评论 4 370
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,600评论 1 307
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,785评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,655评论 0 220
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 53,075评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 41,002评论 1 225
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,146评论 2 318
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,918评论 0 211
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,671评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,838评论 2 254
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,318评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,636评论 3 263
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,343评论 3 244
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,187评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,982评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,126评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,934评论 2 279

推荐阅读更多精彩内容

  • 作为背锅侠运维工作的基本流程 运维工具的分类 : ansible的模块化: ansible密钥登陆 ansible...
    二郎5阅读 4,086评论 0 10
  • 文章来自于:ansible模块 查看模块帮助 ansible-doc -s module copy模块 把本地文件...
    carey_ff72阅读 318评论 0 1
  • ###### Ansible总结 ##### 运维工作: 系统安装(物理机、虚拟机)-->程序包安装、配置、服务启...
    二郎5阅读 1,960评论 0 4
  • ansible介绍ansible常用模块使用playbooktemplates,模板条件测试和循环迭代roles,...
    哈喽别样阅读 1,372评论 0 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,111评论 18 139