ansible 课堂练习

ansible 习题一

针对所有主机(创建用户)

- hosts: yz
  tasks:
    - name: create group
      group:
        name: www
        gid: '666'
    - name: create user
      user:
        name: www
        uid: '666'
        group: '666'

web集群

- hosts: web
  tasks:
    - name: Configure Nginx Repo
      yum_repository:
        name: nginx
        description: This Nginx Repo
        baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
        gpgcheck: no
        enabled: yes
    - name: configure php repo
      yum_repository:
        name: php
        description: This Php Repo
        baseurl: https://www.quwenrui.top/php/
        gpgcheck: no
        enabled: yes
        
    - name: Installed Nginx PHP
      yum:
        name: "{{ packages }}"
        state: present
      vars:
        packages:
          - nginx
          - php71w
          - php71w-cli
          - php71w-common
          - php71w-devel
          - php71w-embedded
          - php71w-gd
          - php71w-mbstring
          - php71w-pdo
          - php71w-xml
          - php71w-fpm
          - php71w-mysqlnd
          - php71w-opcache
          - php71w-mcrypt
          - php71w-pecl-memcached
          - php71w-pecl-mongodb
          - php71w-pecl-redis
          - php71w-pecl-zip
          - php71w-bcmath

    - name: Configure Nginx.conf
      copy:
        src: ./web.file/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
        owner: root
        group: root
        mode: '0644'
      notify: Restart Nginx Server
  
    - name: Configure Nginx VirtHost
      copy:
        src: ./web.file/cloud.yz.com.j2
        dest: /etc/nginx/conf.d/cloud.yz.com.conf
        owner: root
        group: root
        mode: '0644'
      notify: Restart Nginx Server

    - name: Configure php.ini
      copy:
        src: ./web.file/php.ini.j2
        dest: /etc/php.ini
        owner: root
        group: root
        mode: '0644'
      notify: Restart PHP Server

    - name: Configure php-fpm.d/www.conf
      copy:
        src: ./web.file/www.conf.j2
        dest: /etc/php-fpm.d/www.conf
        owner: root
        group: root
        mode: '0644'
      notify: Restart PHP Server

    - name: Create WebSite Directory
      file:
        path: /code/cloud
        state: directory
        owner: www
        group: www
        recurse: yes

    - name: Copy Cloud Code
      unarchive:
        src: ./kodexplorer4.40.zip
        dest: /code/cloud
        copy: yes
        owner: www
        group: www
        creates: "/code/cloud/index.php"

    - name: Systemd Nginx Server
      systemd:
        name: nginx
        state: started
        enabled: yes

    - name: Systemd PHP Server
      systemd:
        name: php-fpm
        state: started
        enabled: yes

  handlers:
    - name: Restart PHP Server
      systemd:
        name: php-fpm
        state: restarted

    - name: Restart Nginx Server
      systemd:
        name: nginx
        state: restarted

配置文件
在这里插入图片描述

lb+redis

- hosts: lb
  tasks:
    - name: Configure Nginx Repo
      yum_repository:
        name: nginx
        description: This Nginx Repo
        baseurl: http://nginx.org/packages/centos/$releasever/$basearch/
        gpgcheck: no
        enabled: yes
    - name: install nginx server
      yum:
        name: "{{ haha }}"
        state: present
      vars:
        haha:
          - nginx
          - redis
    - name: configure nginx
      copy:
        src: ./lb.file/nginx.conf.j2
        dest: /etc/nginx/nginx.conf
        owner: root
        group: root
        mode: '0644'
      notify: Restart Nginx Server
    - name: configure redis
      copy:
        src:  ./lb.file/redis.conf.j2
        dest: /etc/redis.conf
        owner: root
        group: root
        mode: '0644'
      notify: Restart Redis Server
    - name: configrue conf.d
      copy:
        src: /root/ansible_playbook/lb.file/cloud.conf.j2
        dest: /etc/cluod.yz.com.conf 
        owner: root
        group: root
        mode: '0644'
      notify: Restart Nginx Server
    - name: configrue conf.d
      copy:
        src: /root/ansible_playbook/lb.file/proxy_params.j2
        dest: /etc/nginx/proxy_params
        owner: root
        group: root
        mode: '0644'
    - name: started nginx
      systemd:
        name: nginx
        state: started
        enabled: yes
    - name: started redis
      systemd:
        name: redis
        state: started
        enabled: yes
  handlers:
    - name: Restart Nginx Server
      systemd:
        name: nginx
        state: restarted
    - name: Restart Redis Server
      systemd:
        name: redis
        state: restarted

配置文件
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 你可以使用ansible-playbook命令来运行项目练习中的剧本。根据引用\[1\]中的描述,你可以使用以下命令来运行剧本: ansible-playbook /home/devops/ansible/packages.yml 这个剧本将在dev、prod和test主机组中安装php和mariadb软件包,并在dev主机组中安装Development Tools包组,最后升级dev主机组中的所有软件包。另外,根据引用\[2\]中的描述,你还可以使用以下命令来运行另一个剧本: ansible-playbook /home/devops/ansible/internet.yml 这个剧本包含两个任务,第一个任务是在serverb.lab.example.com上安装firewalld、httpd、mariadb-server、php和php-mysqlnd软件包,并确保firewalld服务处于enabled和running状态,并允许访问httpd服务,同时确保httpd和Maria服务处于enabled和running状态。第二个任务是利用uri模块从控制节点测试serverb上运行web服务,检查返回状态代码200。所以,你可以使用以上命令来运行这两个剧本。 #### 引用[.reference_title] - *1* [ansible练习【2】](https://blog.csdn.net/JohnnyG2000/article/details/124188654)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [ansible-playbook(剧本)练习题](https://blog.csdn.net/weixin_56105279/article/details/128041261)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [ansible playbook练习](https://blog.csdn.net/m0_53122419/article/details/121535493)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值