“Playbook”的版本间的差异

来自linux中国网wiki
跳到导航 跳到搜索
第84行: 第84行:
  
 
=基本语法=
 
=基本语法=
 +
== 3.4.4 条件语句==
 +
<pre>
 +
#如果是debian类,关机
 +
playbooks cat when.yml
 +
---
 +
- name: shtudown
 +
  hosts: pi3
 +
  tasks:
 +
    - name: shutdown if debian
 +
      command: /sbin/shutdown -t now
 +
      when: ansible_os_family =="Debian"
 +
 +
 +
#更加好的例子
 +
---
 +
- name: Install vim
 +
  hosts: all
 +
  tasks:
 +
    - name:Install VIM via yum
 +
      yum:
 +
        name: vim-enhanced
 +
        state: installed
 +
      when: ansible_os_family =="RedHat"
 +
     
 +
    - name:Install VIM via apt
 +
      apt:
 +
        name: vim
 +
        state: installed
 +
      when: ansible_os_family =="Debian"
 +
     
 +
    - name: Unexpected OS family
 +
      debug: msg="OS Family {{ ansible_os_family }} is not supported" fail=yes
 +
      when: not ansible_os_family =="RedHat" or ansible_os_family =="Debian"
 +
 +
 +
</pre>
 +
 +
[https://www.cnblogs.com/breezey/p/10996632.html Ansible14:Playbook条件语句]
 
== 3.4.5循环控制==
 
== 3.4.5循环控制==
 
<pre>
 
<pre>

2023年8月24日 (四) 04:07的版本

简单例子

在线playbook分享平台:https://galaxy.ansible.com

ansible tree  ansible-nginx 
ansible-nginx
├── files
│   └── nginx.conf.j2
└── playbook.yml

1 directory, 2 files

➜  ansible-nginx cat files/nginx.conf.j2 
server {
  listen 80;

  root /tmp/;
  index index.html index.htm;

  server_name a.com;
  
  location / {
   default_type "text/html";
   try_files $uri.html $uri $uri/ =404;
  }
}

➜  ansible-nginx cat playbook.yml 
---
- hosts: d11
  name: playbook demo
  become: yes
  gather_facts: false
  remote_user: root
  become_user: root 
  tasks:
    - name: Update apt cache and install Nginx
      apt:
        name: nginx
        state: latest
        update_cache: yes

    - name: Apply Nginx template
      template:
        src: files/nginx.conf.j2
        dest: /etc/nginx/sites-available/default
      notify: Restart Nginx

    - name: Enable new site
      file:
        src: /etc/nginx/sites-available/default
        dest: /etc/nginx/sites-enabled/default
        state: link
      notify: Restart Nginx

    - name: Allow all access to tcp port 80
      ufw:
        rule: allow
        port: '80'
        proto: tcp

  handlers:
    - name: Restart Nginx
      service:
        name: nginx
        state: restarted


ansible-playbook -C  playbook.yml
ansible-playbook   playbook.yml



galaxy.ansible

找几个例子看看

https://galaxy.ansible.com/andrewrothstein/java-oracle-jdk


https://galaxy.ansible.com/sansible/golang


基本语法

3.4.4 条件语句

#如果是debian类,关机
playbooks cat when.yml 
---
- name: shtudown 
  hosts: pi3
  tasks:
    - name: shutdown if debian 
      command: /sbin/shutdown -t now 
      when: ansible_os_family =="Debian"


#更加好的例子
---
- name: Install vim
  hosts: all
  tasks:
    - name:Install VIM via yum
      yum: 
        name: vim-enhanced 
        state: installed
      when: ansible_os_family =="RedHat"
      
    - name:Install VIM via apt
      apt: 
        name: vim 
        state: installed
      when: ansible_os_family =="Debian"
      
    - name: Unexpected OS family
      debug: msg="OS Family {{ ansible_os_family }} is not supported" fail=yes
      when: not ansible_os_family =="RedHat" or ansible_os_family =="Debian"


Ansible14:Playbook条件语句

3.4.5循环控制

一般书上都是用with_item 
cat loop-user2.yml 
---
- name: create user
  hosts: pi3
  tasks:
    - name: create user
      user:
        name: "{{ item }}"
        state: present
      with_items:
      - user04
      - user05
      - user06

    - name: set password
      shell: echo 'e12345678' | passwd --stdin "{{ item }}"
      with_items:
      - user04
      - user05
      - user06



➜  ansible cat loop-user.yml 
---
- name: create user
  hosts: pi3
  tasks:
    - name: create user
      user:
        name: "{{ item }}"
        state: present
      loop:
      - user01
      - user02
      - user03

    - name: set password
      shell: echo 'e12345678' | passwd --stdin "{{ item }}"
      loop:
      - user01
      - user02
      - user03


ansible-playbook -C  loop-user.yml
ansible-playbook   loop-user.yml


执行后查看结果 

root@mypi3b:~# cat /etc/passwd | grep user
root@mypi3b:~# 
root@mypi3b:~# cat /etc/passwd | grep user
user01:x:1003:1004::/home/user01:/bin/sh
user02:x:1004:1005::/home/user02:/bin/sh
user03:x:1005:1006::/home/user03:/bin/sh
root@mypi3b:~# su  - user01
$ hostname
mypi3b
$ id 
uid=1003(user01) gid=1004(user01) groups=1004(user01)

refer

https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_roles.html#dynamic-versus-static-includes

Ansible之Playbook详解、案例

通过ansible安装中间件(jdk,nginx,mysql,etcd集群)

(playbook ins zbx还不错) ansible自动化运维详细教程及playbook详解


How to Install Nginx using Ansible Playbook


Ansible Loop循环控制

官网参考文档:

loops: https://docs.ansible.com/ansible/latest/user_guide/playbooks_loops.html

filters: https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html