ansible模块

roles

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

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

ad-hoc

语法 ansible  [-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」

你可能感兴趣的:(ansible模块)