您好,欢迎来到宝玛科技网。
搜索
您的当前位置:首页Ansible学习笔记--Ansible Playbook实验记录

Ansible学习笔记--Ansible Playbook实验记录

来源:宝玛科技网

Ansible学习笔记

5、Ansible Playbook实验记录

1.1、tasks示例

cat > tasks.yml << EOF
---
- hosts: all
  remote_user: root
  tasks:
    - name: install nginx
      yum: name=nginx
    - name: copy conf
      copy: src=/etc/nginx/nginx.conf dest=/etc/nginx
    - name: copy index
      copy: src=/usr/share/nginx/html/index.html dest=/usr/share/nginx/html
    - name: start service
      service: name=nginx state=started enabled=yes
    - name: stop firewalld
      command: systemctl stop firewalld
EOF

执行tasks.yml

ansible-playbook tasks.yml

以上tasks为顺序执行,某个task执行失败,则不再继续往下执行,可通过ignore_errors忽略错误,方法如下:

---
- hosts: all
  remote_user: root
  tasks:
    - name: somecommand
      command: somecommand
      ignore_errors: True

1.2、handlers示例

cat > handlers.yml << EOF
---
- hosts: all
  remote_user: root
  tasks:
    - name: install nginx
      yum: name=nginx
    - name: copy conf
      copy: src=/etc/nginx/nginx.conf dest=/etc/nginx
      notify: restart service
    - name: copy index
      copy: src=/usr/share/nginx/html/index.html dest=/usr/share/nginx/html
    - name: start service
      service: name=nginx state=started enabled=yes
    - name: stop firewalld
      command: systemctl stop firewalld
  handlers:
    - name: restart service
      service: name=nginx state=restarted
EOF

先执行handlers.yml,修改/etc/nginx/nginx.conf文件内容,再次执行handlers.yml

ansible-playbook handlers.yml

1.3、tags示例

cat > tags.yml << EOF
---
- hosts: all
  remote_user: root
  tasks:
    - name: install nginx
      yum: name=nginx
    - name: copy conf
      copy: src=/etc/nginx/nginx.conf dest=/etc/nginx
    - name: copy index
      copy: src=/usr/share/nginx/html/index.html dest=/usr/share/nginx/html
    - name: start service
      service: name=nginx state=started enabled=yes
      tags: start_service
    - name: stop firewalld
      command: systemctl stop firewalld
EOF

查看tags.yml中的标签

ansible-playbook tags.yml --list-tags

只执行tags.yml中带标签tasks

ansible-playbook -t start_service tags.yml

1.4、vars示例

变量名由字母、数字和下划线组成,只能以字母开头,变量来源如下:

1、远程主机系统变量

2、/etc/ansible/hosts中定义变量,主机组单独定义变量优先级高于公共变量

3、命令行指定变量,优先级最高,ansible-playbook -e varname=value

4、Playbook中定义变量

5、Role中定义变量

1.4.1、在命令行中定义变量
cat > vars1.yml << EOF
---
- hosts: all
  remote_user: root
  tasks:
    - name: install packages
      yum: name={{ package_name }}
    - name: start service
      service: name={{ package_name }} state=started enabled=yes
EOF

执行vars1.yml

ansible-playbook -e 'package_name=nginx' vars1.yml
1.4.2、在Playbook中定义变量
cat > vars2.yml << EOF
---
- hosts: all
  remote_user: root
  vars:
    - package_name: nginx
  tasks:
    - name: install packages
      yum: name={{ package_name }}
    - name: start service
      service: name={{ package_name }} state=started enabled=yes
EOF

执行vars2.yml

ansible-playbook vars2.yml
1.4.3、在hosts中定义公共变量
echo [client:vars] >> /etc/ansible/hosts
echo package_name=nginx >> /etc/ansible/hosts
cat > vars3.yml << EOF
---
- hosts: all
  remote_user: root
  tasks:
    - name: install packages
      yum: name={{ package_name }}
    - name: start service
      service: name={{ package_name }} state=started enabled=yes
EOF

执行vars3.yml

ansible-playbook vars3.yml
1.4.4、在hosts中定义主机变量

修改/etc/ansible/hosts

[client]
192.168.0.11 package_name=nginx
192.168.0.12 package_name=vsftpd
cat > vars4.yml << EOF
---
- hosts: all
  remote_user: root
  tasks:
    - name: install packages
      yum: name={{ package_name }}
    - name: start service
      service: name={{ package_name }} state=started enabled=yes
EOF

执行vars4.yml

ansible-playbook vars4.yml
1.4.5、在变量文件中定义变量
cat > /etc/ansible/vars.yml << EOF
package_name: nginx
EOF

cat > vars5.yml << EOF
---
- hosts: all
  remote_user: root
  vars_files:
    - /etc/ansible/vars.yml
  tasks:
    - name: install packages
      yum: name={{ package_name }}
    - name: start service
      service: name={{ package_name }} state=started enabled=yes
EOF

执行vars5.yml

ansible-playbook vars5.yml
1.4.6、使用系统变量
cat > vars6.yml << EOF
---
- hosts: all
  remote_user: root
  tasks:
    - name: create logfile
      file: name=/tmp/{{ ansible_hostname }}.log state=touch mode=60
EOF

执行vars6.yml

ansible-playbook vars6.yml

1.5、templates示例

templates文本文件,嵌套脚本,使用模板编程语言Jinja2编写,支持以下形式:

1、字符串:使用单引号或双引号

2、数字:整数、浮点数

3、列表:[item1,item2,…]

4、元组:(item1,item2,…)

5、字典:(key1:value1,key2:value2,…)

6、布尔型:True/False

7、算术运算:+、-、*、/、//、%、**

8、比较操作:==、!=、>、>=、<、<=

9、逻辑运算:and、or、not

10、流表达式:when、if、for

1.5.1、无变量模板
cp /etc/nginx/nginx.conf /etc/ansible/nginx.conf.j2
cat > template1.yml << EOF
---
- hosts: all
  remote_user: root
  tasks:
    - name: install nginx
      yum: name=nginx
    - name: copy conf
      template: src=/etc/ansible/nginx.conf.j2 dest=/etc/nginx/nginx.conf
    - name: copy index
      copy: src=/usr/share/nginx/html/index.html dest=/usr/share/nginx/html
    - name: start service
      service: name=nginx state=started enabled=yes
    - name: stop firewalld
      command: systemctl stop firewalld
EOF

执行template1.yml

ansible-playbook template1.yml
1.5.2、有变量模板
cp /etc/nginx/nginx.conf /etc/ansible/nginx.conf.j2

修改/etc/ansible/nginx.conf.j2,添加变量

worker_processes {{ ansible_processor_vcpus*2 }};
......
        listen       {{ http_port }} default_server;
        listen       [::]:{{ http_port }} default_server;
cat > template2.yml << EOF
---
- hosts: all
  remote_user: root
  tasks:
    - name: install nginx
      yum: name=nginx
    - name: copy conf
      template: src=/etc/ansible/nginx.conf.j2 dest=/etc/nginx/nginx.conf
    - name: copy index
      copy: src=/usr/share/nginx/html/index.html dest=/usr/share/nginx/html
    - name: start service
      service: name=nginx state=started enabled=yes
    - name: stop firewalld
      command: systemctl stop firewalld
EOF

执行template2.yml

ansible-playbook -e 'http_port=8080' template2.yml

1.6、with_items示例

执行重复性任务时,可使用迭代机制引用迭代项,固定变量名为item,在tasks中使用with_items迭代元素列表,列表格式支持字符串和字典。

1.6.1、列表迭代
cat > with_items1.yml << EOF
---
- hosts: all
  remote_user: root
  tasks:
    - name: create user
      user: name={{ item }} state=present
      with_items:
        - user1
        - user2
        - user3
EOF

执行with_items1.yml

ansible-playbook with_items1.yml
1.6.2、字典迭代
cat > with_items2.yml << EOF
---
- hosts: all
  remote_user: root
  tasks:
    - name: create group
      group: name={{ item }} state=present
      with_items:
        - group1
        - group2
        - group3
    - name: create user
      user: name={{ item.name }} group={{ item.group }} state=present
      with_items:
        - { name: 'user1',group: 'group1' }
        - { name: 'user2',group: 'group2' }
        - { name: 'user3',group: 'group3' }
EOF

执行with_items2.yml

ansible-playbook with_items2.yml

1.7、if示例

cp /etc/nginx/nginx.conf /etc/ansible/nginx.conf.j2

修改/etc/ansible/nginx.conf.j2,添加if语句

{% for http_port in ports %}
    server {
{% if http_port.port is defined %}
        listen       {{ http_port.port }} default_server;
        listen       [::]:{{ http_port.port }} default_server;
{% endif %}
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
{% endfor %}
cat > if.yml << EOF
---
- hosts: all
  remote_user: root
  vars:
    ports:
      - web1:
      - web2:
        port: 8080
  tasks:
    - name: copy conf
      template: src=/etc/ansible/nginx.conf.j2 dest=/etc/nginx/nginx.conf
    - name: copy index
      copy: src=/usr/share/nginx/html/index.html dest=/usr/share/nginx/html
    - name: start service
      service: name=nginx state=started enabled=yes
    - name: stop firewalld
      command: systemctl stop firewalld
EOF

执行if.yml

ansible-playbook if.yml

1.8、for示例

1.8.1、列表循环
cp /etc/nginx/nginx.conf /etc/ansible/nginx.conf.j2

修改/etc/ansible/nginx.conf.j2,添加for语句

{% for http_port in ports %}
    server {
        listen       {{ http_port }} default_server;
        listen       [::]:{{ http_port }} default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
{% endfor %}
cat > for1.yml << EOF
---
- hosts: all
  remote_user: root
  vars:
    ports:
      - 80
      - 8080
  tasks:
    - name: copy conf
      template: src=/etc/ansible/nginx.conf.j2 dest=/etc/nginx/nginx.conf
    - name: copy index
      copy: src=/usr/share/nginx/html/index.html dest=/usr/share/nginx/html
    - name: start service
      service: name=nginx state=started enabled=yes
    - name: stop firewalld
      command: systemctl stop firewalld
EOF

执行for1.yml

ansible-playbook for1.yml
1.8.2、字典循环
cp /etc/nginx/nginx.conf /etc/ansible/nginx.conf.j2

修改/etc/ansible/nginx.conf.j2,添加for语句

{% for http_port in ports %}
    server {
        listen       {{ http_port.port }} default_server;
        listen       [::]:{{ http_port.port }} default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
{% endfor %}
cat > for2.yml << EOF
---
- hosts: all
  remote_user: root
  vars:
    ports:
      - port: 80
      - port: 8080
  tasks:
    - name: copy conf
      template: src=/etc/ansible/nginx.conf.j2 dest=/etc/nginx/nginx.conf
    - name: copy index
      copy: src=/usr/share/nginx/html/index.html dest=/usr/share/nginx/html
    - name: start service
      service: name=nginx state=started enabled=yes
    - name: stop firewalld
      command: systemctl stop firewalld
EOF

执行for2.yml

ansible-playbook for2.yml

1.9、when示例

cat > when.yml << EOF
---
- hosts: all
  remote_user: root
  tasks:
    - name: install nginx
      yum: name=nginx
    - name: copy conf
      copy: src=/etc/nginx/nginx.conf dest=/etc/nginx
      when: ansible_distribution_major_version == '7'
    - name: copy index
      copy: src=/usr/share/nginx/html/index.html dest=/usr/share/nginx/html
      when: ansible_distribution_major_version == '7'
    - name: start service
      service: name=nginx state=started enabled=yes
    - name: stop firewalld
      command: systemctl stop firewalld
EOF

执行when.yml

ansible-playbook when.yml

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baomayou.com 版权所有 赣ICP备2024042794号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务