Ansible 练习题

示例1:创建用户

---
- name: create user
  hosts: all
  gather_facts: false
  tasks:
    - name: create {{ item }}
      user:
        name: "{{ item }}"
        groups: root
        password: "$6$Okahzlzi8PqXN30d$Zd.jyW0A9jO3k09OMpEujhSn5EMpjHECfOIQGC2cS6HQBfAhncrVL/iaUmkuCi4LFWqr7KARyoVMJzKtBrBoZ/"  #密码由openssl passwd -6 生成
      loop:
      - alex
      - jack

示例2:复制文件

---
- name: copy file
  hosts: all
  tasks:
    - name: copy ansible.cfg file
      copy:
        src: /etc/ansible/ansible.cfg
        dest: /mnt/
        mode: '755'

示例3:编辑playbook,使得浏览器中输入相应域名得到对应结果

create web vhost
www.westos.com 80 ------ > /var/www/html ------> www.westos.com

linux.westos.com 80 ------> /var/www/virtual/westos.com/linux ----->linux.westos.com

vim webs.yml

---
webs:
  - webname: www.westos.com
    doc: /var/www/html
  - webname: linux.westos.com
    doc: /var/www/virtual/westos.com/linux

vim http.yml

---
- name: httpd
  hosts: 172.25.73.10
  vars_files: ./webs.yml
  tasks:
    - name: dnf install httpd
      dnf:
        name: httpd
        state: present
    - name: mkdir
      file:
        path: "{{item.doc}}"
        state: directory
      loop:
        "{{webs}}"

    - name: touch index.html
      copy:
        dest: "{{item.doc}}/index.html"
        content: "{{item.webname}}"
      loop:
        "{{webs}}"
    - name: clear config
      file:
        name: /etc/httpd/conf.d/vhosts.conf
        state: absent
    - name: configure vhost
      lineinfile:
        path: /etc/httpd/conf.d/vhosts.conf
        create: yes
        line: |+
          <VirtualHost *:80>
            DocumentRoot "{{item.doc}}"
            ServerName "{{item.webname}}"
          </VirtualHost>
      loop:
        "{{webs}}"
    - lineinfile:
        path: /etc/httpd/conf.d/vhosts.conf
        line: |+
          <VirtualHost _default_:80>
            DocumentRoot /var/www/html
          </VirtualHost>
        insertbefore: BOF
    - name: start httpd
      service:
        name: httpd
        state: restarted
        enabled: yes
    - name: firewalld
      firewalld:
        service: http
        permanent: yes
        state: enabled
        immediate: yes

未完待续…

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿王不想秃头

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

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

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

打赏作者

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

抵扣说明:

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

余额充值