ansible-playbook的基本组件——杂记

定义执行主机和执行用户
- hosts: webserver
remote_user: root
sudu: yes
——————————————
tasks列表,Play的主体部分是task列表,task列表中的各任务按次序逐个在hosts中指定的主机上执行,即在所有主机上完成第一个任务后再开始第二个任务。Task的目的是使用指定的参数执行模块,而在模块参数中可以使用变量,

- name: ensure apache is at the latest version
yum: name=httpd state=latest(见ansible模块参数)

执行时shell和command模块时忽略错误信息,防止剧本被打断。
tasks:
- name: run this command and ignore the result
shell: /usr/bin/somecommand
ignore_errors: True
————————————————

定义变量和模版
- hosts: test
vars:
- package: httpd
vars固定格式,package变量名,httpd变量值
变量名支持字母数字以及下划线,变量名须字母开头。

调用方法
tasks:
- name: ensure apache is at the latest version
yum: name={{ package }} state=latest

在task或template中引用变量都是使用双花括号中间引入变量名即可,如:{{ VAR_NAME }}。
————————————————

handlers
发生改变时执行提前定义好的操作

command执行命令
command: shell命令
————————————————
lineinfile
文件操作
匹配行修改
- name: seline modify enforcing
lineinfile:
dest: /etc/selinux/config
regexp: '^SELINUX='
line: 'SELINUX=enforcing'

匹配上上方插入
- name: httpd.conf modify 8080
lineinfile:
dest: /opt/playbook/test/http.conf
regexp: '^Listen'
insertbefore: '^#Port'
line: 'Listen 8080'

匹配行下方插入
- name: httpd.conf modify 8080
lineinfile:
dest: /opt/playbook/test/http.conf
regexp: '^Listen'
insertafter: '^#Port'
line: 'Listen 8080'

删除某行
- name: delete 192.168.1.1
lineinfile:
dest: /opt/playbook/test/hosts
state: absent
regexp: '^192\.'

末行添加
name: add a line
lineinfile:
dest: /opt/playbook/test/hosts
line: '192.168.1.2 foo.lab.net foo'

 


Register
将命令执行结果存储到变量中
tasks:

- name: retrieve the list of home directories
command: ls /home
register: home_dirs

- name: add home dirs to the backup spooler
file: path=/mnt/bkspool/{{ item }} src=/home/{{ item }} state=link
with_items: home_dirs.stdout_lines

 

 

 

转载于:https://www.cnblogs.com/Leonardo-li/p/8589567.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值