ansible基础

目录

1、安装ansible

2、配置主机列表

3、编写测试yaml【debug】

4、查看语法是否正确

5、查看受控制主机

6、执行测试yaml【debug】

7、ansible编写yaml,使用echo命令打印how are you,并且创建目录/root/smdx


1、安装ansible

查看版本

[root@k8s02 ansible_nginx]# ansible --version

ansible 2.9.27

  config file = /etc/ansible/ansible.cfg

  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']

  ansible python module location = /usr/lib/python2.7/site-packages/ansible

  executable location = /usr/bin/ansible

  python version = 2.7.5 (default, Oct 14 2020, 14:45:30) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

2、配置主机列表

此处文件所在路径任意即可

[root@k8s02 ansible_nginx]# cat hosts    

[test]

k8s01

k8s02



[local]

k8s02





[test:vars]

# 填写被控端主机的ssh的密码

ansible_ssh_pass=yxlyxl

3、编写测试yaml【debug】

[root@k8s02 ansible_nginx]# more test.yml

---

- name: test

  hosts: all

  tasks:

    - name: debug

      debug:

        msg: hello

4、查看语法是否正确

ansible-playbook -i hosts test.yml  --syntax-check

5、查看受控制主机

[root@k8s02 ansible_nginx]# ansible-playbook -i hosts test.yml --list-hosts

此处匹配所有主机

我们稍加修改,此处只匹配到local标签下的k8s02 主机

6、执行测试yaml【debug】

执行操作ansible-playbook -i hosts test.yml

7、ansible编写yaml,使用echo命令打印how are you,并且创建目录/root/smdx

[root@k8s02 ansible_nginx]# more echo.yxml

---

- name: 使用echo命令打印how are you并创建目录/root/smdx

  hosts: all

  tasks:

    - name: 打印how are you

      command: echo "how are you"



    - name: 创建目录/root/smdx

      file:

        path: /root/smdx

        state: directory

执行操作

所有主机验证效果

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值