ansible从入门到入土:教你如何编写ansible剧本实现yum安装软件

剧本内容

文件名:install.yaml

- hosts: hello
  connection: ssh
  remote_user: root
  tasks:
   - name: install git
     yum: name=git state=installed

剧本检查

ansible-playbook --syntax-check install.yaml

在这里插入图片描述

剧本执行

ansible-playbook -C install.yaml 

执行效果

root@debian:~# ansible-playbook -C install.yaml 

PLAY [hello] *****************************************************************************************************************************************************************************************************

TASK [Gathering Facts] *******************************************************************************************************************************************************************************************
ok: [server1]

TASK [install git] ***********************************************************************************************************************************************************************************************
[DEPRECATION WARNING]: State 'installed' is deprecated. Using state 'present' instead.. This feature will be removed in version 2.9. Deprecation warnings can be disabled by setting deprecation_warnings=False 
in ansible.cfg.
ok: [server1]

PLAY RECAP *******************************************************************************************************************************************************************************************************
server1                    : ok=2    changed=0    unreachable=0    failed=0   

在这里插入图片描述

剧本解析

- hosts: hello
  connection: ssh
  remote_user: root
  tasks:
   - name: install git
     yum: name=git state=installed
内容作用
- hosts: hello指定要操作的主机
connection: ssh连接的方式
remote_user: root使用的用户
tasks:定义一个任务
- name: install git这个子任务的名称
yum: name=git state=installed使用的模块(yum)及参数

注意事项

YAML采用变态的空格来做缩进,且对格式非常严格,所以如果格式不对请检查你的空格是否多了或者少了。

几个技巧

首行必须使用-来定义,然后一个空格加主线(hosts),以此定义被操作的对象
第二行必须使用两个空格去定义(同等级都是这样)
当遇到有子任务的主线(tasks),需要以冒号结尾,然后下一行参照第一行的格式继续

相关文章

ansible从入门到入土:常用模块---->apt模块

ansible从入门到入土:带你从安装到实现“hello world“

ansible从入门到入土:对剧本进行语法检查及真/伪执行剧本

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坐公交也用券

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值