使用ansible中的playbook

在这里插入图片描述
在这里插入图片描述

2、playbook执行的命令

在这里插入图片描述

写一个playbook

 [root@foundation50 .ansible]# vim test.yml
---
-  name: test yml
   hosts: 172.25.254.100
   tasks:
     -  name: action
        shell:
          hostname
[root@foundation50 .ansible]# ansible-playbook -C test.yml  对 test.yml  进行语法检测,  -C表示执行检测
[root@foundation50 .ansible]# ansible-playbook --syntax-check test.yml  --syntax-check语法检测
[root@foundation50 .ansible]# ansible-playbook  --list-hosts test.yml  列出运行主机

playbook: test.yml

  play #1 (172.25.254.100): test yml	TAGS: []
    pattern: ['172.25.254.100']
    hosts (1):
      172.25.254.100
[root@foundation50 .ansible]# ansible-playbook  --list-tags test.yml   列出tags

playbook: test.yml

  play #1 (172.25.254.100): test yml	TAGS: []
      TASK TAGS: []
[root@foundation50 .ansible]# ansible-playbook  --list-tasks test.yml   列出动作

playbook: test.yml

  play #1 (172.25.254.100): test yml	TAGS: []
    tasks:
      action	TAGS: []
[root@foundation50 .ansible]# ansible-playbook  --limit 172.25.254.200 test.yml -C  指定执行主机,但是hosts列表里面没有200这个主机

PLAY [test yml] *******************************************************************************************
skipping: no hosts matched

PLAY RECAP *******************************************************

 [root@foundation50 .ansible]# vim test.yml
---
-  name: test yml
   hosts: westos    将hosts列表172,25.254.100改成westos
   tasks:
     -  name: action
        shell:
          hostname
[root@foundation50 .ansible]#ansible-playbook  --limit 172.25.254.200 test.yml -C   指定200这台主机执行,westos列表清单里有200这台主机

PLAY [test yml] *******************************************************************************************

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

TASK [action] *********************************************************************************************
skipping: [172.25.254.200]   
 [root@foundation50 .ansible]# ansible-playbook  test.yml -v   -v 表示显示过程  -vv跟详细
      

在书写playbook时建议:

[root@foundation50 .ansible]# vim ~/.vimrc 
  1 set nu  ts=2 ai et     ts=2 表示tab建表示两个空格 ai表示自动缩进 et表示把tab建转换称空格

3 playbook简单示例及vim设定

[root@foundation50 .ansible]# vim westos.yml
---
- name: test play
  hosts: 172.25.254.100
  tasks:
    - debug:
        msg: hello world
[root@foundation50 .ansible]# ansible-playbook westos.yml         
PLAY [test play]   此处就是name名称,可以不写******************************************************************************************

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

TASK [debug] **********************************************************************************************
[root@foundation50 .ansible]# cat westos.yml  
---
- hosts: 172.25.254.100   把name名称可以直接去掉
  tasks:
    - debug:
        msg: hello world
[root@foundation50 .ansible]# ansible-playbook westos.yml 
ok: [172.25.254.100] => {
    "msg": "hello world"
  1 ---
  2 - hosts: 172.25.254.100                                                                                
  3   tasks: 
  4     - debug:
  5         msg: hello world
注意:hosts 和  tasks 都必须写

示例:用playbook部属apache

[root@foundation50 .ansible]# vim apache.yml
---
- name: Apache server
  hosts: 172.25.254.100
  tasks:
    - name: install apache
      dnf:
        name: httpd
        state: latest
        disable_gpg_check: yes    不检测

    - name: start apache
      service:
        name: httpd
        state: started
        enabled: yes

    - name: firewalld setup
      firewalld:
        service: http
        permanent: yes
        state: enabled
        immediate: yes
        zone: public

    - name: create index
      lineinfile:
        path: /var/www/html/index.html
        line: "hello westos"
        create: yes

 [root@foundation50 .ansible]# ansible-playbook apache.yml -C 执行检测
 [root@foundation50 .ansible]# ansible-playbook apache.yml  运行
 当不会写的时可以 查看模块帮助[root@foundation50 westos]# ansible-doc firewalld | less

在这里插入图片描述
vim设定
在这里插入图片描述

[root@foundation50 .ansible]# vim ~/.vimrc
1 autocmd  FileType yaml  setlocal ts=2 ai et sw=2                 setlocal表示只对当前yaml文件生效                                             
 2 set nu  针对所有
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小莫细说linux

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

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

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

打赏作者

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

抵扣说明:

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

余额充值