您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码:  验证码,看不清楚?请点击刷新验证码 必填



  求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
   
 
     
   
 订阅
  捐助
ansible 原理及详解
 
来源:51CTO 发布于 2017-9-27
  26268  次浏览      17
 

简介

ansible是个什么东西呢?官方的title是“Ansible is Simple IT Automation”——简单的自动化IT工具。这个工具的目标有这么几项:

  • 自动化部署APP;
  • 自动化管理配置项;
  • 自动化的持续交互;
  • 自动化的(AWS)云服务管理;

所有的这几个目标从本质上来说都是在一个台或者几台服务器上,执行一系列的命令而已。通俗的说就是批量的在远程服务器上执行命令。当然,最主要的是它是基于 paramiko 开发的。这个paramiko是什么呢?它是一个纯Python实现的ssh协议库。因此fabric和ansible还有一个共同点就是不需要在远程主机上安装client/agents,因为它们是基于ssh来和远程主机通讯的。简单归纳一下:

Ansible

—基于 Python paramiko 开发,分布式,无需客户端,轻量级,配置语法使用 YMAL 及 Jinja2模板语言,更强的远程命令执行操作

类似的自动化运维工具有很多常用的还有:

Puppet

—基于 Ruby 开发,采用 C/S 架构,扩展性强,基于 SSL,远程命令执行相对较弱

SaltStack

—基于 Python 开发,采用 C/S 架构,相对 puppet 更轻量级,配置语法使用 YMAL,使得配置脚本更简单

Ansible 工作机制

Ansible 在管理节点将 Ansible 模块通过 SSH 协议(或者 Kerberos、LDAP)推送到被管理端执行,执行完之后自动删除,可以使用 SVN 等来管理自定义模块及编排

由上面的图可以看到 Ansible 的组成由 5 个部分组成:

  • Ansible : 核心
  • Modules : 包括 Ansible 自带的核心模块及自定义模块
  • Plugins : 完成模块功能的补充,包括连接插件、邮件插件等
  • Playbooks : 剧本;定义 Ansible 多任务配置文件,由Ansible 自动执行
  • Inventory : 定义 Ansible 管理主机的清单

ansible 安装

(1)编译安装

解决依赖:

#yum -y install python-jinja2 PyYAML python-paramiko python-babel python-crypto

安装

#tar xf ansible-1.5.4.tar.gz
#cd ansible-1.5.4
#python setup.py build
#python setup.py install
#mkdir /etc/ansible
#cp -r examples/* /etc/ansible

(2)YUM 安装,要有epel

yum-y install ansible

ansible命令参数

语法: ansible<host-pattern> [-f forks] [-m module_name] [-a args] 。。。

  • -v,–verbose

详细模式,如果命令执行成功,输出详细的结果 (-vv –vvv -vvvv)

  • -i PATH,–inventory=PATH

指定 host 文件的路径,默认是在 /etc/ansible/hosts

  • -f NUM,–forks=NUM

NUM 是指定一个整数,默认是 5 ,指定 fork 开启同步进程的个数。

  • -m NAME,–module-name=NAME

指定使用的 module 名称,默认是 command

  • -m DIRECTORY,–module-path=DIRECTORY

指定 module 的目录来加载 module ,默认是/usr/share/ansible,

  • -a,MODULE_ARGS

指定 module 模块的参数

  • -k,–ask-pass

提示输入 ssh 的密码,而不是使用基于 ssh 的密钥认证

  • –sudo

指定使用 sudo 获得 root 权限

  • -K,–ask-sudo-pass

提示输入 sudo 密码,与 –sudo 一起使用

  • -u USERNAME,–user=USERNAME

指定移动端的执行用户

  • -C,–check

测试此命令执行会改变什么内容,不会真正的去执行

YAML基本语法

YAML的语法和其他高阶语言类似,并且可以简单表达清单、散列表、标量等数据结构。其结构(Structure)通过空格来展示,序列(Sequence)里的项用"-"来代表,Map里的键值对用":"分隔。下面是一个示例。

name: John Smith
age: 41
gender: Male
spouse:
-name: Jane Smith
age:37
gender: Female
children:
-name: Jimmy Smith
age: 17
gender: Male
-name: Jenny Smith
age 13
gender: Female

YAML文件扩展名通常为.yaml,如example.yaml。

注意,代码的排版有严格要求,缩进为2个字符!序列项的 - 后必须跟一个空格!:后也要跟一个空格!

  • list列表的所有元素均使用“-”打头,例如:
#A list of tasty fruits
-Apple
-Orange
-Strawberry
-Mango
  • dictionary字典通过key与valuef进行标识,例如:
---
#An employee record
name:Example Developer
job:Developer
skill:Elite

也可以将key:value放置于{}中进行表示,例如:

---
#An employee record
{name:Example Developer, job: Developer, skill: Elite}

Ansible基础元素

(1)变量

变量命名

变量名仅能由字母、数字和下划线组成,且只能以字母开头。

(2)facts

facts是由正在通信的远程目标主机发回的信息,这些信息被保存在ansible变量中。要获取指定的远程主机所支持的所有facts,
可使用如下命令进行:

# ansible hostname -m setup

ansible all -m setup 显示所有节点的信息

(3)register

把任务的输出定义为变量,然后用于其他任务,示例如下:

tasks:
- shell: /usr/bin/foo
register: foo_result
ignore_errors: True

(4)变量传递

  • 通过命令行传递变量

在运行playbook的时候也可以传递一些变量供playbook使用,示例如下:

ansible-playbook test.yml --extra-vars "hosts=wwwuser=mageedu"
  • 通过roles传递变量

当给一个主机应用角色的时候可以传递变量,然后在角色内使用这些变量,示例如下:

-hosts: webservers
roles:
- common
- { role: foo_app_instance, dir:'/web/htdocs/a.com', port: 8080 }

(5)Inventory

ansible的主要功用在于批量主机操作,为了便捷地使用其中的部分主机,可以在inventory file中将其分组命名。默认的 inventoryfile为/etc/ansible/hosts,可通过/etc/ansible/ansible.cfg定义。

inventory file可以有多个,且也可以通过Dynamic Inventory来动态生成。

inventory文件格式

inventory文件遵循INI文件风格,中括号中的字符为组名。可以将同一个主机同时归并到多个不同的组中;此外,当如若目标主机使用了非默认的SSH端口,还可以在主机名称之后使用冒号加端口号来标明。

ntp.magedu.com
[webservers]
www1.magedu.com:2222
www2.magedu.com
[dbservers]
db1.magedu.com
db2.magedu.com
db3.magedu.com

如果主机名称遵循相似的命名模式,还可以使用列表的方式标识各主机,例如:

[webservers]
www[01:50].example.com
[databases]
db-[a:f].example.com

(6)主机变量

可以在inventory中定义主机时为其添加主机变量以便于在playbook中使用。例如:

[webservers]
www1.magedu.com http_port=80maxRequestsPerChild=808
www2.magedu.com http_port=8080maxRequestsPerChild=909

(7)组变量

组变量是指赋予给指定组内所有主机上的在playbook中可用的变量。例如:

[webservers]
www1.magedu.com
www2.magedu.com
[webservers:vars]
ntp_server=ntp.magedu.com
nfs_server=nfs.magedu.com

注意:上面vars为固定的格式

(8)组嵌套

inventory中,组还可以包含其它的组,并且也可以向组中的主机指定变量。不过,这些变量只能在ansible-playbook中使用,而ansible不支持。例如:

[apache]
httpd1.magedu.com
httpd2.magedu.com
[nginx]
ngx1.magedu.com
ngx2.magedu.com

[webservers:children] children是固定格式,必须要写上的

apache
nginx
[webservers:vars]
ntp_server=ntp.magedu.com

(9) inventory 参数

ansible基于ssh连接inventory中指定的远程主机时,还可以通过参数指定其交互方式;这些参数如下所示:

(可以实现在inventory中指定账号密码用于ssh 连接)

ansible_ssh_host
Thename of the host to connect to, if different from the alias you wish to give toit.
ansible_ssh_port
Thessh port number, if not 22
ansible_ssh_user
Thedefault ssh user name to use.
ansible_ssh_pass
Thessh password to use (this is insecure, we strongly recommend using --ask-passor SSH keys)
ansible_sudo_pass
Thesudo password to use (this is insecure, we strongly recommend using--ask-sudo-pass)
ansible_connection
Connection type of the host. Candidates are local, ssh or paramiko. The default is paramiko before Ansible
1.2,and 'smart' afterwards which detects whether usage of 'ssh' would be feasiblebased on whether ControlPersist is supported.
ansible_ssh_private_key_file
Private key file used by ssh.Useful if using multiple keys and you don't want to use SSH agent.
ansible_shell_type
Theshell type of the target system. By default commands are formatted using'sh'-style syntax by default. Setting this to 'csh' or 'fish' will causecommands executed on target systems to follow those shell's syntax instead.
ansible_python_interpreter
Thetarget host python path. This is useful for systems with morethan one Python or not located at
"/usr/bin/python" such as\*BSD, or where /usr/bin/pythonis not a 2.X series Python. We do not use the
"/usr/bin/env" mechanism as that requires the remote user'spath to be set right and also assumes the "python"
executableis named python, where the executable mightbe named something like "python26".
ansible\_\*\_interpreter
Works for anything such as ruby or perl and works just like ansible_python_interpreter.
This replaces shebang of modules which will run on that host.

(10)条件测试

如果需要根据变量、facts或此前任务的执行结果来做为某task执行与否的前提时要用到条件测试。

when语句

在task后添加when子句即可使用条件测试;when语句支持Jinja2表达式语法。例如:

tasks:
-name: "shutdown Debian flavored systems"
command: /sbin/shutdown -h now
when: ansible_os_family == "Debian"

when语句中还可以使用Jinja2的大多“filter”,例如要忽略此前某语句的错误并基于其结果(failed或者sucess)运行后面指定的语句,可使用类似如下形式:

tasks:
-command: /bin/false
register: result
ignore_errors: True
-command: /bin/something
when: result|failed
-command: /bin/something_else
when: result|success
-command: /bin/still/something_else
when: result|skipped

此外,when语句中还可以使用facts或playbook中定义的变量。

(11)迭代

当有需要重复性执行的任务时,可以使用迭代机制。其使用格式为将需要迭代的内容定义为item变量引用,并通过with_items语句来指明迭代的元素列表即可。例如:

- name: add several users

user: name={{ item }} state=present groups=wheel 这行中的item是固定变量名,一定要这样写,它会循环使

用with_items定义的列表

with_items:
- testuser1
- testuser2

上面语句的功能等同于下面的语句:

- name: add user testuser1
user: name=testuser1 state=present groups=wheel
- name: add user testuser2
user: name=testuser2 state=present groups=wheel

事实上,with_items中可以使用元素还可为hashes(),例如:

- name: add several users
user: name={{ item.name }} state=present groups={{ item.groups }}
with_items:
-{ name: 'testuser1', groups: 'wheel' }
-{ name: 'testuser2', groups: 'root' }

ansible的循环机制还有更多的高级功能,具体请参见官方文档(http://docs.ansible.com/playbooks_loops.html)。

playbook 的组成结构

Inventory

Modules

AdHoc Commands

Playbooks

Tasks:任务,即调用模块完成的某操作

Variables:变量

Templates:模板

Handlers:处理器,由某事件触发执行的操作

Roles:角色

playbook 基础组件简介

(1)Hosts 和Users

playbook中的每一个play的目的都是为了让某个或某些主机以某个指定的用户身份执行任务。hosts用于指定要执行指定任务的主机,其可以是一个或多个由冒号分隔主机组;remote_user则用于指定远程主机上的执行任务的用户。如上面示例中的

-hosts:webnodes
remote_user: root

不过,remote_user也可用于各task中。也可以通过指定其通过sudo的方式在远程主机上执行任务,其可用于play全局或某任务;此外,甚至可以在sudo时使用sudo_user指定sudo时切换的用户。

-hosts: webnodes
remote_user: mageedu
tasks:
- name: test connection
ping:
remote_user: mageedu
sudo: yes

(2)任务列表和action

play的主体部分是task list。task list中的各任务按次序逐个在hosts中指定的所有主机上执行,即在所有主机上完成第一个任务后再开始第二个。在运行自上而下某playbook时,如果中途发生错误,所有已执行任务都可能回滚,因此,在更正playbook后重新执行一次即可。

task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量。模块执行是幂等的,这意味着多次执行是安全的,因为其结果均一致。

每个task都应该有其name,用于playbook的执行结果输出,建议其内容尽可能清晰地描述任务执行步骤。如果未提供name,则action的结果将用于输出。

定义task的可以使用“action: moduleoptions”或“module: options”的格式,推荐使用后者以实现向后兼容。如果action一行的内容过多,也中使用在行首使用几个空白字符进行换行。

tasks:
- name: make sure apache is running
service: name=httpd state=running

在众多模块中,只有command和shell模块仅需要给定一个列表而无需使用“key=value”格式,例如:

tasks:
- name: disable selinux
command: /sbin/setenforce 0

如果命令或脚本的退出码不为零,可以使用如下方式替代:

tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand || /bin/true

或者使用ignore_errors来忽略错误信息:

tasks:
- name: run this command and ignore theresult
shell: /usr/bin/somecommand
ignore_errors: True

(3) handlers

用于当关注的资源发生变化时采取一定的操作。

“notify”这个action可用于在每个play的最后被触发,这样可以避免多次有改变发生时每次都执行指定的操作,取而代之,仅在所有的变化发生完成后一次性地执行指定操作。在notify中列出的操作称为handler,也即notify中调用handler中定义的操作。

-name: template configuration file
template: src=template.j2 dest=/etc/foo.conf
notify:
- restart memcached
- restart apache

handler是task列表,这些task与前述的task并没有本质上的不同。

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

例:

heartbeat.yaml
-hosts: hbhosts
remote_user: root
tasks:
- name: ensure heartbeat latest version
yum: name=heartbeat state=present
- name: authkeys configure file
copy: src=/root/hb_conf/authkeysdest=/etc/ha.d/authkeys
- name: authkeys mode 600
file: path=/etc/ha.d/authkeys mode=600
notify:
- restart heartbeat
- name: ha.cf configure file
copy: src=/root/hb_conf/ha.cfdest=/etc/ha.d/ha.cf
notify:
-restart heartbeat
handlers:
- name:restart heartbeat
service: name=heartbeat state=restarted

(4)roles

ansilbe自1.2版本引入的新特性,用于层次性、结构化地组织playbook。roles能够根据层次型结构自动装载变量文件、tasks以及 handlers等。要使用roles只需要在playbook中使用include指令即可。简单来讲,roles就是通过分别将变量、文件、任务、模块 及处理器放置于单独的目录中,并可以便捷地include它们的一种机制。角色一般用于基于主机构建服务的场景中,但也可以是用于构建守护进程等场景中。

一个roles的案例如下所示:

site.yml
webservers.yml
fooservers.yml
roles/
common/
files/
templates/
tasks/
handlers/
vars/
meta/
webservers/
files/
templates/
tasks/
handlers/
vars/
meta/

而在playbook中,可以这样使用roles(调用common和webservers角色了):

---
-hosts: webservers
roles:
- common
- webservers

也可以向roles传递参数,例如:

---
-hosts: webservers
roles:
- common
- { role: foo_app_instance, dir:'/opt/a', port: 5000 }
- { role: foo_app_instance, dir:'/opt/b', port: 5001 }

甚至也可以条件式地使用roles,例如:

---
-hosts: webservers
roles:
- { role: some_role, when:"ansible_os_family == 'RedHat'" }

a)创建role的步骤

(1)创建以roles命名的目录;

(2)在roles目录中分别创建以各角色名称命名的目录,如webservers等;

(3)在每个角色命名的目录中分别创建files、handlers、meta、tasks、templates和vars目录;用不到的目录可以创建为空目录,也可以不创建;

(4)在playbook文件中,调用各角色;

b) role内各目录中可用的文件

  • tasks目录:至少应该包含一个名为main.yml的文件,其定义了此角色的任务列表;此文件可以使用include包含其它的位于此目录中的task文件;
  • files目录:存放由copy或script等模块调用的文件;
  • templates目录:template模块会自动在此目录中寻找Jinja2模板文件;
  • handlers目录:此目录中应当包含一个main.yml文件,用于定义此角色用到的各handler;在handler中使用include包含的其它的handler文件也应该位于此目录中;
  • vars目录:应当包含一个main.yml文件,用于定义此角色用到的变量;
  • meta目录:应当包含一个main.yml文件,用于定义此角色的特殊设定及其依赖关系;ansible 1.3及其以后的版本才支持;
  • default目录:为当前角色设定默认变量时使用此目录;应当包含一个main.yml文件;

c) roles使用案例

1.建立相应目录

ansible_playbooks/roles/ {websrvs,dbsrvs}/ {tasks,files,templates,meta, handlers,vars}

2.解决第一个角色websrvs

先把要用到的配置文件拷过来(待会要复制给各个服务器使用的)

cp/etc/httpd/conf/httpd.conf /ansible_playbooks/roles/websrvs/files

3.定义任务tasks vim/ansible_playbooks /roles/websrvs/tasks/main.yml

内容:

-name: install httpd package
yum: name=httpd
-name:install configuration file
copy: src=httpd.conf dest= /etc/httpd/conf/httpd.conf 注意路径,都可以使用相对路径直接调用
tags:
- conf
notify:
- restart httpd
-name: start httpd
service: name=httpd state=started

4.由于tasks中使用了notify,即需要另外定义handler,所以必须在/ansible_playbooks /roles/websrvs/handlers编写

添加main.yml文件

vim/ansible_playbooks/roles/ websrvs/handlers/main.yml

内容如下:

-name: restart httpd
service: name=httpd state=restarted

5.如果没有其他内容了就可以了,有的话继续在相应的目录下建立main.yml定义

假如又使用了变量,就要在vars下定义一个main.yml指定变量

- httpd_port: 80
- maxClients: 180

6.创建/ansible_playbooks/site.yml 指定创建了的角色

- hosts: websrvs
remote_user: root
roles:
- websrvs

至此roles创建完成,要想把相应的角色用在某个主机上的话,直接在site.yml上- hosts:websrvs 指定,roles下设置
对应角色即可。

这样:

-hosts: websrvs
remote_user: root
roles:
- websrvs
-hosts: websrvs2
remote_user: root
roles:
- websrvs2
- dbsrvs

最后执行ansible-playbooks site.yml

注意:复制到相应目录的文件,都可以直接写文件名调用

d)Tags

tags用于让用户选择运行或略过playbook中的部分代码。ansible具有幂等性,因此会自动跳过没有变化的部分,即便如此,有些
代码为测试其确实没有发生变化的时间依然会非常地长。此时,如果确信其没有变化,就可以通过tags跳过此些代码片断。

apache.yml内容:

- hosts: websrvs
remote_user: root
vars:
-package: httpd
-service: httpd
tasks:
-name: install httpd package
yum: name={{ package }} state=latest
-name: install configuration file for httpd
template: src= /root/templates/httpd.conf.j2dest =/etc/httpd/conf/httpd.conf
tags:
-conf
notify:
-restart httpd
-name: start httpd service
service: enabled=true name={{ service }} state=started
handlers:
-name:restart httpd
service: name=httpd state=restarted

调用:

ansible-playbook apache.yml--tags="conf",

这样只执行了

-name: install configuration file for httpd
template: src= /root/templates/httpd.conf.j2dest =/etc/httpd/conf/httpd.conf
tags:
-conf

e)Jinja2相关

1.1 字面量

表达式最简单的形式就是字面量。字面量表示诸如字符串和数值的 Python 对象。下面 的字面量是可用的:

“HelloWorld”:

双引号或单引号中间的一切都是字符串。无论何时你需要在模板中使用一个字符串(比如函数调用、过滤器或只是包含或继承一个模板的参数),它们都是 有用的。

42/ 42.23:

直接写下数值就可以创建整数和浮点数。如果有小数点,则为浮点数,否则为整数。记住在 Python 里, 42 和 42.0 是不一样的。

[‘list’,‘of’, ‘objects’]:

一对中括号括起来的东西是一个列表。列表用于存储和迭代序列化的数据。例如你可以容易地在 for 循环中用列表和元组创建一个链接的列表:

<ul>
{%for href, caption in [('index.html', 'Index'), ('about.html', 'About'),
('downloads.html','Downloads')] %}
<li><a href="{{ href}}">{{ caption }}</a></li>
{%endfor %}
</ul>
(‘tuple’,‘of’, ‘values’):

元组与列表类似,只是你不能修改元组。如果元组中只有一个项,你需要以逗号结尾它。元组通常用于表示两个或更多元素的项。更多细节见上面的例子。

{‘dict’:‘of’, ‘key’: ‘and’, ‘value’: ‘pairs’}:

Python中的字典是一种关联键和值的结构。键必须是唯一的,并且键必须只有一个 值。字典在模板中很少使用,罕用于诸如 xmlattr() 过滤器之类。

true/ false:

true永远是 true ,而 false 始终是 false 。

2.2 算术运算

Jinja 允许你用计算值。这在模板中很少用到,但是为了完整性允许其存在。支持下面的运算符:

+   把两个对象加到一起。通常对象是素质,但是如果两者是字符串或列表,你可以用这种方式来衔接它们。无论如何这不是首选的连接字符串的方式!连接字符串见 ~ 运算符。 {{ 1 + 1 }} 等 于 2 。

-   用第一个数减去第二个数。 {{ 3 - 2 }} 等于 1 。

/   对两个数做除法。返回值会是一个浮点数。 {{ 1 / 2 }} 等于 {{ 0.5 }} 。

//   对两个数做除法,返回整数商。 {{ 20 // 7 }} 等于 2 。

%   计算整数除法的余数。 {{ 11 % 7 }} 等于 4 。

*   用右边的数乘左边的操作数。 {{ 2 * 2 }} 会返回 4 。也可以用于重复一个字符串多次。 {{ ‘=’ * 80 }} 会打印 80 个等号的横条。

**   取左操作数的右操作数次幂。 {{ 2**3 }} 会返回 8 。

3.3 比较操作符

==   比较两个对象是否相等。

!=   比较两个对象是否不等。

>    如果左边大于右边,返回 true 。

>=   如果左边大于等于右边,返回 true 。

<    如果左边小于右边,返回 true 。

<=   如果左边小于等于右边,返回 true 。

4.4 逻辑运算符

对于 if 语句,在 for 过滤或 if 表达式中,它可以用于联合多个表达式:

and   如果左操作数和右操作数同为真,返回 true 。

or    如果左操作数和右操作数有一个为真,返回 true 。

not   对一个表达式取反(见下)。

(expr)

表达式组。

yaml 文件执行方法

很简单:ansible-play 文件名

ansible 常用模块及示例

要查看详细信息 ansible-doc -s 模块名称

(1)command: 命令模块,默认模块,用于在远程执行命令;

例 ansible all -a 'date' 或 ansible a2 -a 'date' 查看a2主机上的日期

(2)cron:计划任务模块

参数

state:

present: 安装

absent: 移除

例:

ansible websrvs -m cron -a 'minute="*/10" job="/bin/echo hell" name="test cronjob"'

ansible a2 -m cron -a 'hour="*/10" job="/bin/echo 11" name="test"'

添加明为test的定时任务,这个name其实是注释,看crontab内容就知道

ansible a2 -m corn -a 'state=absent name="test"'

删除a2上名为test的定时任务

(3)user:用户模块

name=: 指明创建的用户的名字;

例:

ansible websrvs -m user -a 'name=mysqluid=306 system=yes group=mysql'

ansible a2 -m user -a 'name=mytest password=123456 createhome=false shell=/bin/tcsh'

添加密码为123456的用户mytest,不建家目录,用tcsh shell

(4)group: 组模块

例:

ansible websrvs -m group -a 'name=mysql gid=306 system=yes'

(5)copy:复制文件

src=:定义本地源文件路径

dest=:定义远程目标文件路径

content=:取代src=,表示直接用此处指定的信息生成为目标文件内容;

例:

ansible all -m copy -a 'src=/etc/fstabdest= /tmp/fstab.ansbile owner=root mode=640'

ansible all -m copy -a 'content= "HelloAnsible\nHi MageEdu" dest=/tmp/test.ansible'

ansible a2 -m copy -a 'src=/root/a1-filedest =/root/file.ansible owner= mytest group=mytest mode=640'

(6)file: 设定文件属性

path=:指定文件路径,可以使用name或dest来替换;

创建文件的符号链接:

src=:指明源文件

path=:指明符号链接文件路径

ansible all -m file -a 'path =/tmp/fstab.link src =/tmp/fstab.ansbile state=link'

创建符号链接文件fstab.link

(7)ping:测试指定主机是否能连接;

例:

ansible a2 -m ping

ansible all -m ping

(8)service:指定运行状态;

enabled=:是否开机自动启动,取值为true或者false;

name=:服务名称

state=:状态,取值有started, stopped, restarted;

例:

ansiblea2 -m service -a 'name=mysqld state=started'

ansiblea2 -m service -a 'name=mysqld enabled=true'

(9)shell:在远程主机上运行命令

尤其是用到管道等功能的复杂命令就要用shell而不能用command了;

例:

ansibleall -m command -a 'echo 123456 | passwd --stin user1' 这样执行是失败的

ansibleall -m shell -a 'echo 123456 | passwd --stin user1'这样才是正确的

(10)script:将本地脚本复制到远程主机并运行之;

注意:要使用相对路径指定脚本

例:

ansiblea2 -m script -a 'test.sh'

(11)yum:安装程序包

name=:指明要安装的程序包,可以带上版本号;

state=:present(默认), latest表示安装,absent表示卸载;

例:

ansible a2 -m yum -a 'name=zsh'

(12)setup: 收集远程主机的facts

每个被管理节点在接收并运行管理命令之前,会将自己主机相关信息,如操作系统版本、IP地址等报告给远程的ansbile 主机;

例:

ansible all -m setup 显示所有节点的信息

   
26268 次浏览       17
相关文章

DevOps转型融入到企业文化
DevOps 能力模型、演进及案例剖析
基于 DevOps 理念的私有 PaaS 平台实践
微软开发团队的DevOps实践启示
相关文档

DevOps驱动应用运维变革与创新
运维管理规划
如何实现企业应用部署自动化
运维自动化实践之路
相关课程

自动化运维工具(基于DevOps)
互联网运维与DevOps
MySQL性能优化及运维培训
IT系统运维管理