欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

ansible yml语法

发布时间:2025/6/15 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 ansible yml语法 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

playbook使用:ansible-playbook test.yaml

    playbook是由一个或多个“play”组成的列表。play的主要功能在于将事先归并为一组的主机装扮成事先通过ansible中的task定义好的角色。从根本上来讲,所谓task无非是调用ansible的一个module。将多个play组织在一个playbook中,即可以让它们联同起来按事先编排的机制同唱一台大戏

#这个是你选择的主机 - hosts: webservers #这个是变量vars:http_port: 80max_clients: 200 #远端的执行权限remote_user: roottasks: #利用yum模块来操作- name: ensure apache is at the latest versionyum: pkg=httpd state=latest- name: write the apache config filetemplate: src=/srv/httpd.j2 dest=/etc/httpd.conf #触发重启服务器notify:- restart apache- name: ensure apache is runningservice: name=httpd state=started #这里的restart apache 和上面的触发是配对的。这就是handlers的作用。相当于taghandlers:- name: restart apacheservice: name=httpd state=restarted


1、HOSTS和Users

    playbook中的每一个play的目的都是为了让某个或某些主机以某个指定的用户身份执行任务。

    hosts用于指定要执行指定任务的主机,其可以是一个或多个由冒号分隔主机组;

    remote_user则用于指定远程主机上的执行任务的用户。如上面示例中的

-hosts: webnodesremote_user: root

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

- hosts: webnodesremote_user: roottasks:- name: test connectionping:remote_user: rootsudo: yes

2、任务列表和cation

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

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

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

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

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

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

tasks:- name: disable selinuxcommand: /sbin/setenforce 0

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

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

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

tasks:- name: run this command and ignore the resultshell: /usr/bin/somecommandignore_errors: True

3、handlers

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

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

- name: template configuration filetemplate: src=template.j2 dest=/etc/foo.confnotify:- restart memcached- restart apache

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

handlers:- name: restart memcachedservice:  name=memcached state=restarted- name: restart apacheservice: name=apache state=restarted



例子

heartbeat.yaml- hosts: hbhostsremote_user: roottasks:- name: ensure heartbeat latest versionyum: name=heartbeat state=present- name: authkeys configure filecopy: src=/root/hb_conf/authkeys dest=/etc/ha.d/authkeys- name: authkeys mode 600file: path=/etc/ha.d/authkeys mode=600notify:- restart heartbeat- name: ha.cf configure filecopy: src=/root/hb_conf/ha.cf dest=/etc/ha.d/ha.cfnotify:- restart heartbeathandlers:- name: restart heartbeatservice: name=heartbeat state=restarted



参考博文:

http://nmshuishui.blog.51cto.com/1850554/1573941

转载于:https://blog.51cto.com/12899802/1948420

总结

以上是生活随笔为你收集整理的ansible yml语法的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。