ansible剧本

ansible剧本

ansible的playbook采用了yaml语法,它简单地实现了json格式的事件描述
yaml⽂件以 — 开头,以表明这是⼀个yaml⽂件,就像xml⽂件在开头使⽤ <?xml version="1.0" encoding="utf-8"?> 宣称它是xml⽂件⼀样。但即使没有使⽤ — 开头,也不会有什么影响。
2. yaml中使⽤"#“作为注释符,可以注释整⾏,也可以注释⾏内从”#"开始的内容。
3. yaml中的字符串通常不⽤加任何引号,即使它包含了某些特殊字符。但有些情况下,必须加引号,最常见的是在引⽤变量的时候。
4. 关于布尔值的书写格式,即true/false的表达⽅式。其实playbook中的布尔值类型⾮常灵活,可分为两种情况:
模块的参数: 这时布尔值作为字符串被ansible解析。接受yes/on/1/true/no/of f /0/false,这时被ansible解析。例如上⾯⽰例中的 update_cache=yes 。
⾮模块的参数: 这时布尔值被yaml解释器解析,完全遵循yaml语法。接受不区分⼤⼩写的 true/yes/on/y/f alse/no/off /n。例如上⾯的 gpgcheck=no 和 enabled=True 。建议遵循ansible的官⽅规范,模块的布尔参数采⽤yes/no,⾮模块的布尔参数采⽤True/False

playbook的内容
每个play都包含⼀个hosts和⼀个tasks,hosts定义的是inventory中待控制的主机,tasks下定义的是⼀系列task任务列表,⽐如调⽤各个模块。这些task按顺序⼀次执⾏⼀个,直到所有被筛选出来的主机都执⾏了这个task之后才会移动到下⼀个task上进⾏同样的操作。
需要注意的是,虽然只有被筛选出来的主机会执⾏对应的task,但是所有主机(此处的所有主机表⽰的是,hosts选项所指定的那些主机)都会收到相同的task指令,所有主机收到指令后,ansible主控端会筛选某些主机,并通过ssh在远程执⾏任务。

YAML字典
YAML中使用的key/value对也称为字典、散列或关联数组 在key/value对中,键与值通过由冒号和空格组成的分隔符隔开
字典也可以使用内嵌块格式表示,其中多个key/value对用花括号括起,并由逗号和空格隔开 - {name: svcrole, svcservice: http, svcport: 80}
YAML列表
在YAML中,列表类似于其他编程语言中的数组 为表示一组列表项,使用一个短划线加一个空格作为每个列表项的前缀

列表也可使用内嵌块表示,其中多个列表项用方括号括起来并由逗号和空格隔开

playbook的使用
ansible-playbook用于运行剧本,-C 测试运行结果,并不是真的执行任务。

一、部署web服务器
1、部署yum仓库
2、安装httpd
3、新建/www目录
4、在/www中新建index.html,内容为my name is lihao
5、该web服务器的DocumentRoot为/www
5、实现在ansible中能够使用http://node1访问到该网页内容

[student@ansible ansible]$ vim web.yml
[student@ansible ansible]$ cat web.yml
---
- name: web
  hosts: node1
  tasks:
    - name: mount cdrom
      mount:
        src: /dev/cdrom
        path: /mnt
        fstype: iso9660
        state: present

    - name: yum_repo
      yum_repository:
        file: xuanning
        name: AppStream
        description: AppStream
        baseurl: file:///mnt/AppStream
        enabled: 1
        gpgcheck: 0

    - name: yum_repo2
      yum_repository:
        file: xuanning
        name: BaseOS
        description: BaseOS
        baseurl: file:///mnt/BaseOS
        enabled: 1
        gpgcheck: 0

    - name: install httpd
      yum:
        name: httpd
        state: installed

    - name: httpd enabled
      service:
        name: httpd
        enabled: yes

    - name: link
      file:
        src: /var/www/html
        dest: /www
        owner: apache
        group: apache
        state: link
        mode: 0755

    - name: httpd.conf
      replace:
        path: /etc/httpd/conf/httpd.conf
        regexp:  DocumentRoot "/var/www/html"
        replace: DocumentRoot "/www"

    - name: httpd.conf Directory
      replace:
        path: /etc/httpd/conf/httpd.conf
        regexp: <Directory "/var/www">
        replace: <Directory "/">

    - name: create index.html
      copy:
        content: my name is lihao
        dest: /www/index.html

    - name: index.html context
      file:
        path: /www/index.html
        owner: apache
        group: apache
        setype: httpd_sys_content_t

    - name: firewalld
      firewalld:
        service: http
        permanent: yes
        state: enabled
        immediate: yes
    - name: httpd restart
      service:
        name: httpd
        state: restarted
[student@ansible ansible]$ ansible-playbook web.yml

PLAY [web] ******************************************************************************************************************************************************

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

TASK [mount cdrom] **********************************************************************************************************************************************
ok: [node1]

TASK [yum_repo] *************************************************************************************************************************************************
changed: [node1]

TASK [yum_repo2] ************************************************************************************************************************************************
changed: [node1]

TASK [install httpd] ********************************************************************************************************************************************
changed: [node1]

TASK [httpd enabled] ********************************************************************************************************************************************
changed: [node1]

TASK [link] *****************************************************************************************************************************************************
changed: [node1]

TASK [httpd.conf] ***********************************************************************************************************************************************
changed: [node1]

TASK [httpd.conf Directory] *************************************************************************************************************************************
changed: [node1]

TASK [create index.html] ****************************************************************************************************************************************
ok: [node1]

TASK [index.html context] ***************************************************************************************************************************************
ok: [node1]

TASK [firewalld] ************************************************************************************************************************************************
ok: [node1]

TASK [httpd restart] ********************************************************************************************************************************************
changed: [node1]

PLAY RECAP ******************************************************************************************************************************************************
node1                      : ok=13   changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

[student@ansible ansible]$ curl  http://node1
my name is lihao

二、使用notify…handlers
1、写一个剧本runtime.yml,只对node1操作
2、创建用户aa,该用户不能用于登录,家目录/www
3、在/www创建一个文件html
4、每次执行该剧本时,将系统的当前时间输入到html文件中。
5、如果html中的时间发生变化,那么创建/tmp/kk的文件

[student@ansible ansible]$ vim runtime.yml
[student@ansible ansible]$ cat runtime.yml
---
- name: xuanning
  hosts: node1
  tasks:
    - name: useradd aa
      user:
        name: aa
        home: /www
        shell: /sbin/nologin

    - name: mkdir /www/html
      file:
        path: /www/html
        state: touch

    - name: time
      shell: date > /www/html
      notify:
        - kk

  handlers:
    - name: kk
      file:
        path: /tmp/kk
        state: touch


[student@ansible ansible]$ ansible-playbook runtime.yml

PLAY [xuanning] *************************************************************************************************************************************************

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

TASK [useradd aa] ***********************************************************************************************************************************************
changed: [node1]

TASK [mkdir /www/html] ******************************************************************************************************************************************
changed: [node1]

TASK [time] *****************************************************************************************************************************************************
changed: [node1]

RUNNING HANDLER [kk] ********************************************************************************************************************************************
changed: [node1]

PLAY RECAP ******************************************************************************************************************************************************
node1                      : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值