Ansible playbook

  

 

  1. install mysql
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    ---
    - hosts: gale
      remote_user: root
      gather_facts: no
     
      tasks:
        - name: install packages
          yum: name=libaio,perl
        - {name: create group, group: name=mysql system=yes gid=306}
        - name: create user
          user: name=mysql system=yes group=mysql shell=/sbin/nologin home=/data/mysql uid=306 create_home=no
        - name: unarchive mysql
          unarchive: src=/data/files/mysql-5.6.46.tar.gz dest=/usr/local owner=mysql group=mysql
        - name: make link
          file: src=/usr/local/mysql-5.6.46 dest=/usr/local/mysql state=link
        - name: create data dir
          shell: chdir=/usr/local/mysql mysqld --initialize-insecure --datadir=/data/mysql --user=mysql
          tags: data
        - name: copy my.cnf
          copy: src=/data/my.cnf dest=/etc/my.cnf
        - name: enable service
          shell: systemctl start mysqld;systemctl enable mysql
          tags: service
        - name: config PATH
          copy: content=PATH=/usr/local/mysql/bin:$PATH dest=/etc/profile.d/mysql.sh
        - name: script
          script: /data/init.sh
          tags: script

      

  2. notify & handler
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    ---
    - hosts: web-server
      remote_user: root
     
      tasks:
        - name: install httpd
          yum: name=httpd state=present
        - name: copy config files
          copy: src=/config/httpd.conf dest=/etc/httpd/conf/
          notify:
            - restart httpd
            - check httpd process
        - name: config service
          service: name=httpd state=started enabled=yes
         
      handlers:
        - name: restart httpd
          service: name=httpd state=restarted
        - name: check httpd process
          shell: killall -0 httpd &> /tmp/httpd.log
    ...     

      

  3.  

    环境变量

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    - hosts: gale
      remote_user: root
      vars:
        - ip: "{{ ansible_default_ipv4['address'] }}"
        - username: user1
        - groupname: group1
        - filename: bbb  # ansible-playbook -e has great priority
      vars_files:
        - vars.yaml
     
      tasks:
        - name: create log file
          file: path=/dd/{{ ansible_nodename }}.log state=touch owner=rabid group=rabid mode=7000
        - name: create log file
          file: path=/dd/{{ ip }}.log state=directory
        - name: create group
          group: name={{ groupname }} state=present
        - name: create user
          user: name={{ username }} state=present
        - name: create file
          file: path=/data/{{ filename }} state=touch
        - name: create file
          file: path=/mnt/{{ a }} state=directory
        - name: create file
          file: path=/mnt/{{ b }} state=touch

      

    1
    2
    3
    4
    ---
    a: aaa
    b: bbb
    ...

      

  4.  template

     

     

     

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    ---
    - hosts: gale
      remote_user: root
     
      tasks:
        - name: install nginx
          yum: name=nginx state=present
        - name: actualize nginx template
          template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf
          notify: reset service
        - name: config nginx service
          service: name=nginx state=started enabled=yes
      handlers:
        - name: reset service
          service: name=nginx state=restarted

      

     

     
    for

     

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    ---
    - hosts: gale
      remote_user: root
      gather_facts: no
      vars:
        - vhosts:
            - listen: 80
              server_name: bb.io
            - listen: 8080
              server_name: pp.io
     
      tasks:
        - name: actualize nginx template
          template: src=nginx.conf.j2 dest=/mnt/nginx.conf

      

     

     
    template:

     

     yaml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    ---
    - hosts: gale
      remote_user: root
      gather_facts: no
      vars:
        - ports: [ 80, 81, 82]
     
      tasks:
        - name: actualize nginx template
          template: src=nginx.conf.j2 dest=/mnt/nginx.conf

      

     

     

     

    条件判断

     

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    ---
    - hosts: gale
      remote_user: root
      gather_facts: no
      vars:
        - vhosts:
            - web1:
              listen: 81
              root: /var/www/nginx/web1
            - web2:
              listen: 82
              server_name: web2.b.io
              root: /var/www/nginx/web2
            - web3:
              listen: 83
              server_name: web3.b.io
              root: /var/www/nginx/web3
     
      tasks:
        - name: actualize nginx template
          template: src=nginx.conf.j2 dest=/mnt/nginx.conf

      

     

     

  5.  

    条件测试

    1
    2
    3
    4
    5
    6
    7
    8
    ---
    - hosts: gale2:u1
      remote_user: root
     
      tasks:
        - name: shutdown RedHat flavored systems
          command: shutdown -h now
          when: ansible_os_family == 'RedHat'

      
    根据ip地址筛选

    1
    2
    3
    4
    5
    6
    7
    8
    ---
    - hosts: gale2:u1
      remote_user: root
     
      tasks:
        - name: orient copy files
          copy: src=/etc/rpc dest=/opt
          when: "ansible_default_ipv4['address'] == '192.168.8.12'"

      

  6. with_items

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    ---
    - hosts: gale
      remote_user: root
     
      tasks:
        - name: augment group
          group: name={{ item }} state=present
          with_items:
            - nginx
            - mysql
            - apache
        - name: augment user
          user: name={{ item.user }} state=present groups=wheel
          with_items:
            - user: augment1
            - user: augment2
        - name: augment user
          user: name={{ item.user }} group={{ item.group }} state=present
          with_items:
            - {name: nginx, group: nginx}
            - {name: mysql, group: mysql}
            - {name: apache, group: apache}

      

  7.  roles

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    ---
    - hosts: all
      remote_user: root
     
      roles:
        - {role: nginx, username: nginx, when: ansible_distribution_major_version == '7'}
    ---
    - hosts: all
      remote_user: root
     
      roles:
        - {role: nginx, tags: [nginx, web], when: ansible_distribution_major_version == '6'}
        - {role: httpd, tags: [httpd, web]}
        - {role: mysql, tags: [mysql, db]}
        - {role: mariadb, tags: [mariadb, db]}
     
    ansible-playbook --tags=nginx,httpd,mysql nginx-role.yaml

      

    1
    2
    3
    4
    5
    6
    - hosts: gale
      remote_user: root
       
      roles:
        - {role: httpd, tags: [httpd, web], when: ansible_distribution_major_version == '7'}
        - {role: nginx, tags: [nginx, web], when: ansible_distribution_major_version == '8'}

      

     

     

     

     


     

     

     

     

     

     

     

     

     

     

     

     

     


     

     

     


     

      

posted @   ascertain  阅读(54)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2020-07-31 OpenSSH for windows
2020-07-31 SQLserver -- 备份脚本
点击右上角即可分享
微信分享提示