Ansible 入门指南

介绍

module 文档:

在playbook脚本中,tasks 中的每一个 action都是对 module的一次调用。在每个 action中:

  • 冒号前面是 module 的名字
  • 冒号后面是调用 module 的参数

ansible-doc <module_name> 也可以查看module的用法

Ansible 提供一些常用功能的 Module,同时 Ansible 也提供 API,让用户可以自己写 Module,使用的编程语言是 Python

更多 ansible 模块 - Extra Modules

Ansible Module 文档 查看时,文档的底部都会标识, 这是一个”Core Module”, 或者这是一个”Extra Module”.

Core Module:

  • 不需要格外下载和配置,安装ansible后就可以直接使用的.
  • 比较常用的module
  • 经过严格测试的.

Extra module

  • 进行下载和格外的配置才能使用
  • 次常用的
  • 还有可能存在bug的

安装 extra module

  1. 下载相应的 module 仓库
  2. 修改配置文件或者环境变量
  • ansible.cfg 中添加一行,library = ./<extra module name>/
  • export ANSIBLE_LIBRARY=/project/demo/demoansible/library/ansible-module-extras

copy – Copies files to remote locations

从当前机器上 copy 文件到远程节点上,可以设置合理的文件权限。copy module拷贝文件的时候,会先比较下文件的checksum,如果相同则不会拷贝,返回状态OK;如果不同才会拷贝,返回状态为changed。

注意点:

  • 远程路径要写对,并不会自动创建不存在的目录

示例:

- name: test copy whether mkdir dir
  copy: src=./deploy.yml dest=/tmp/deploy2.yml owner=michael group=michael mode=0644

参数较多时,用下面这种写法更清晰:

- name: test copy whether mkdir dir
  copy:
    src: ./deploy.yml
    dest: /tmp/deploy2.yml
    owner: michael
    group: michael
    mode: 0777

参考

template – Templates a file out to a remote serve

在一些文件中,有些变量或者参数的值是需要根据具体的主机信息的,这时候不能写死,这样的文件拷贝到远程主机时就需要用到 template 模块了。想起学习 python flask 时接触的一个 Jinja 包,就是类似的模板渲染功能。

比如安装apache后,你需要给节点拷贝一个测试页面index.html,index.html里面需要显示当前节点的主机名和IP,这时候就需要用到template。

index.html中,你需要指定你想替换的哪个部分,那么这个部分用变量来表示,template使用的是python的j2模版引擎,你不需要了解什么是j2,你只需要知道表量的表示法是{{}}就可以了

./template/index.html.j2 内容:

<html>
<title>Demo</title>
<body>
<div class="block" style="height: 99%;">
    <div class="centered">
        <h1>#46 Demo</h1>
        <p>Served by {{ ansible_hostname }} : {{ ansible_default_ipv4.address }}<p>
    </div>
</div>
</body>
</html>

在index.html.j2使用的两个变量ansible_hostnameansible_default_ipv4.address都是facts变量,ansible会替我们搜索,直接可以在playbook中使用。我们也可以自定义普通变量。

我运行 scp root@192.168.3.43:/etc/httpd/conf/httpd.conf ./templates 命令拷贝了默认生成的配置文件,修改./template/httpd/conf.j2 内容:

ServerRoot "/etc/httpd"
...
Listen {{ httpd_port }}
...

普通变量不是在调用template的时候传进去,而是通过playbook中vars关键字定义。当然如果在playbook中可以直接使用的变量,都可以在template中,包括后面的章节会提到的定义在inventory中的变量

- hosts: centos
  vars:
    httpd_port: 8080
  remote_user: root
  tasks:
  - name: Write the configuration file
    template: src=templates/httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf

template 具有和 copy 模块类似的权限设置、文件备份、验证功能,例如:

- template:
    src: etc/ssh/sshd_config.j2
    dest: /etc/ssh/sshd_config.j2
    owner: root
    group: root
    mode: '0600'
    validate: /usr/sbin/sshd -t %s
    backup: yes
  • validate:默认none,验证复制后的文件,%s指代复制后的文件

file – Sets attributes of files

file module设置远程值机上的文件、软链接(symlinks)和文件夹的权限,也可以用来创建和删除他们。

---
- hosts: centos
  vars:
    httpd_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - debug:
      msg: "System {{ inventory_hostname }} has gateway {{ansible_default_ipv4.gateway}}"

  # 拷贝时,不存在路径时,不会自动创建路径
  - name: test copy whether mkdir dir
    copy:
      src: ./demo.txt
      dest: /tmp/demo.txt
      owner: michael
      group: michael
      mode: 0600

  # 设置权限
  - name: change owner for file
    file:
      path: /tmp/demo.txt
      owner: root
      group: root
      mode: 0644

  # 创建软连接
  - name: create file soft link
    file:
      src: /etc/httpd/
      dest: /tmp/httpd_link
      owner: root
      group: root
      state: link

  # 删除文件
  - name: delete deploy.yml in tmp
    file:
      path: "/tmp/{{ item }}"
      state: absent
    with_items:
      - ["deploy.yml", "deploy2.yml"]

  # 新建一个文件
  - name: 新建一个文件
    file:
      path: /tmp/new_file_by_ansible.txt
      state: touch
      mode: 0644

  # 新建一个文件夹
  - name: 新建文件夹
    file:
      path: /tmp/new_dir_by_ansible
      state: directory
      mode: 0755

这个 playbook 中的 task 有一个涉及到循环的,一开始怎么都调试不对,后来看了几个带循环的,发现了问题,with_items 这个 action 并不是属于 file 模块本身的参数,所以,它的缩进就不能如下这么写:

 # 删除文件
  - name: delete deploy.yml in tmp
    file:
      path: "/tmp/{{ item }}"
      state: absent
      with_items:
      - ["deploy.yml", "deploy2.yml"]

当然,批量删除文件,我觉得还有更好的方式去做,有待进一步学习。

参考

yum – Manages packages with the yum package manager

---
- hosts: centos
  vars:
    httpd_port: 80
  remote_user: root
  tasks:
  - debug:
      msg: "System {{ inventory_hostname }} has gateway {{ansible_default_ipv4.    gateway}}"

  # 安装指定源的指定版本的包
  - name: install one specific version of Apache
    yum:
      name: httpd-2.4.6-88.el7.centos
      enablerepo: base
      state: present

  # 从本地文件安装包
  - name: install nginx from a local file
    yum:
      name: /usr/local/src/nginx-release-centos-6-0.el6.ngx.noarch.rpm
      state: present
    ignore_errors: True

  # 从 URL 安装包
  - name: install the nginx rpm from a remote repo
    yum:
      name: http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
      state: present
  • yum 模块来安装软件包
  • name 指定安装的软件包的名字
  • state 指定安装软件包时的行为,它有几个选项值,
    • installed/present 它俩是等价的,表示如果远程主机上有这个包了,则不重新安装了;
    • latest 顾名思义,会去安装最新版的,这个对于生产是比较危险的,有可能因此破坏了生产的环境

Yum 模块

state 值的具体区别,参考了这个问题:

service – Manage services

设置服务开机启动:

- service:
    name: httpd
    enabled: yes

启动网络服务下的接口:

- service:
    name: network
    state: restarted
    args: eth0

state 有几个可选值(Choices: reloaded, restarted, started, stopped)[Default: (null)]

  • started'/stopped' are idempotent[幂等] actions that will not run commands unless necessary.
  • restarted will always bounce the service.
  • reloaded will always reload. At least one of state and enabled are required. Note that reloaded will start the service if it is not already started, even if your chosen init system wouldn't normally.

shell – Execute commands in nodes

通过/bin/sh在远程节点上执行命令。如果一个操作你可以通过Module yum,copy操作实现,那么你不要使用shell或者command这样通用的命令module。因为通用的命令module不会根据具体操作的特点进行status的判断,所以当没有必要再重新执行的时候,它还是要重新执行一遍

  • shell 模块支持 $home,支持$HOME和”<”, “>”, “|”, “;” and “&”
  • command 模块不支持&&>

今天使用时,就发现构建机器上没有默认的 /bin/sh,所以,看看下面的使用吧:

执行命令前,改变工作目录,指定用bash运行命令:

- shell: cat < /tmp/*txt
  args:
    chdir: somedir/
    executable: /bin/bash

参考

参考

原文地址:https://www.cnblogs.com/michael-xiang/p/10461501.html