ansible--playbook剧本

一、初步说明

以一个简单的playbook为例,说明yaml的基本语法

  1. yaml⽂件以 --- 开头,以表明这是⼀个yaml⽂件,就像xml⽂件在开头使⽤ 宣称它是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对中,键与值通过由冒号和空格组成的分隔符隔开
name: svcrole
svcservice: http
svcport: 80

字典也可以使用内嵌块格式表示,其中多个key/value对用花括号括起,并由逗号和空格隔开

YAML列表

在YAML中,列表类似于其他编程语言中的数组
为表示一组列表项,使用一个短划线加一个空格作为每个列表项的前缀
hosts:

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

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

示例

一、部署web服务器

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

[student@ansible ansible]$ vim ljl.yml 
---
- name: web station
  hosts: node1
  tasks:
    - name: yum_repo
      yum_repository:
        file: server
        name: aa
        description: aa1
        baseurl: file:///mnt/BaseOS
        enabled: yes
        gpgcheck: no

    - name: yum_repo2
      yum_repository:
        file: server
        name: bb
        description: bb1
        baseurl: file:///mnt/AppStream
        enabled: yes
        gpgcheck: no


    - name: mount /dev/cdrom
      mount:
        src: /dev/cdrom
        path: /mnt
        fstype: iso9660
        state: mounted

    - name: yum http
      yum:
        name: httpd
        state: installed

    - name: create /www
      file:
        src: /var/www/html
        dest: /www
        state: link

    - name: get html
      get_url:
        url: http://ansible.example.com/index.html
        dest: /www

    - name: firewalld http
      firewalld:
        rich_rule: rule family=ipv4 source address=192.168.203.0/24 service name=http accept
        permanent: yes
        immediate: yes
        state: enabled

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

    - name: restorecon
      shell:
        cmd : restorecon -Rv /www/index.html

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

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

    - name: start httpd
      service:
        name: httpd
        state: restarted
        enabled: yes


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

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

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

TASK [yum_repo] *************************************************************************
ok: [node1]

TASK [yum_repo2] ************************************************************************
ok: [node1]

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

TASK [yum http] *************************************************************************
ok: [node1]

TASK [create /www] **********************************************************************
ok: [node1]

TASK [get html] *************************************************************************
ok: [node1]

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

TASK [context] **************************************************************************
ok: [node1]

TASK [restorecon] ***********************************************************************
changed: [node1]

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

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

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

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

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

二、使用notify....handlers

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

[student@ansible ansible]$ vim runtime.yml 
---
- name: test
  hosts: node1
  tasks:
    - name: create useraa
      user:
        name: aa
        shell: /sbin/nologin
        create_home: yes
        home: /www

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

    - name: date
      shell: date > /www/html

      notify:
        - kk

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

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

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

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

TASK [create useraa] ********************************************************************
ok: [node1]

TASK [create html] **********************************************************************
changed: [node1]

TASK [date] *****************************************************************************
changed: [node1]

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

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

posted @ 2022-10-25 16:08  罗家龙  阅读(142)  评论(0编辑  收藏  举报