简单几句话了解Ansible-playbook
四、Ansible 变量注册含义及使用场景
- hosts: lingchen
tasks:
- name: Install Nginx Server
yum:
name: nginx
state: present
- name: Service Nginx Server
service:
name: nginx
state: started
- name: Check Nginx Server
shell: ps aux | grep nginx
register: check_nginx
- name: OutPut variables
debug:
msg: {{check_nginx.stout_lines}}
五、Ansible facts 变量
通过facts变量提取被控端总内存大小
]$ ansible 10.8.4.23 -m setup -a "filter=ansible_memtotal_mb" -i hosts
10.8.4.23 | SUCCESS => {
"ansible_facts": {
"ansible_memtotal_mb": 4096,
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
daemon
]$ cat lingchen_5.yaml
---
- hosts: lingchen
tasks:
- name: Install Nginx Server
yum:
name: nginx
state: present
- name: Configure Nginx Conf
template:
src: ./nginx.conf.j2
dest: /etc/nginx/nginx.conf
- name: Service Nginx Server
service:
name: Nginx
state: started
enabled: yes
- name: Check Nginx Server
shell: ps aux| grep nginx
register: check_mem
- name: Debug Nginx Variables
debug:
msg: "{{ check_mem.stdout_lines }}"
六、Ansible Playbook tasks任务控制
role | action | describe |
---|---|---|
1.循环语句 | with_items | 多值场景,例如拷贝多文件至不同路径时 item列表循环、facts字典循环等 |
2.触发器 | handlers | 通常与任务tasks中的notify并行,例如修改完配置文件需要重启等操作场景 |
3.包含 | include | 可用于include 某tasks,import_palybook 等 |
4.判断语句 | when | 根据不同类别的主机或系统及其他来进行不同的任务 |
5.标签 | tags | 一般用于调试模式使用 -t 指定 -- skip- tags 跳过 |
6.错误忽略 | ignore_errors | Yes 则表示忽略错误,继续执行出错后续tasks |
7.错误处理 | Fource_handlers\change_when\ | fource_handlers: yes 强制调用handlers\change_when: false 抑制changed状态\change_when: (check_nginx.stdout.find('ok') |
==Demo:==
1. 根据不同的操作系统安装相同的软件包
]$ cat lingchen.yaml
- hosts: lingchen
tasks:
- name: Install {{ansible_distribution}} Nginx Server
yum:
name: nginx
state: present
when: ( ansible_distribution == "CentOS")
- name: Install {{ansible_distribution}} Nginx2 Server
yum:
name: nginx2
state: present
when: ( ansible_distribution == "Ubuntu")
2. 为所有web主机添加nginx仓库,其余的skip
]$ cat lingchen.yaml
- hosts: all
tasks:
- name: Create yum repo
yum_repository:
name: ansible_nginx
description: ansible_test
baseurl: https://mirrors.oldboy.com
gpgcheck: no
enabled: no
when: ( ansible_fqdn is match ("web*")) or
( ansible_fqdn is match ("slb*"))
3. 根据命令结果进行判断
]$ cat lingchen.yaml
- hosts: lingchen
tasks:
- name: Check Nginx Server
shell: systemctl is-active nginx
ignore_errors: yes
register: check_nginx
- name: Nginx Restart # check_nginx变量中的rc结果等于0,则执行重启nginx,否则跳过
service:
name: nginx
state: restarted
when: check_nginx.rc = 0
4. 循环语句 with_items # 变量方式循环启动服务
]$ cat lingchen.yaml
- hosts: lingchen
tasks:
- name: Service Nginx Server
service:
name: {{ item }}
state: restarted
with_items:
- nginx
- php-fpm
]$ cat lingchen.yaml # 定义变量方式循环安装软件包
- hosts: lingchen
tasks:
- name: Installed Nginx Mariadb Package
yum:
name: {{ pack }}
state: latest
vars:
pack:
- nginx
- mariadb-server
]$ cat lingchen.yaml # 定义变量字典方式循环创建用户
- hosts: lingchen
tasks:
- name: Create User
user:
name: {{ item.name }}
groups: {{ item.groups }}
state: present
with_items:
- { name: 'lingchen1', groups: 'lingchen' }
- { name: 'lingchen2', groups: 'root' }
]$ cat lingchen.yaml # 定义变量字典方式循环拷贝文件
- hosts: lingchen
tasks:
- name: Configure Rsyncd Server
copy:
src: {{ item.src }}
dest: {{ item.dest }}
mode: {{ item.mode }}
with_items:
- { src: './rsyncd.conf.j2', dest: '/tmp/rsyncd.conf', mode: '0644' }
- { src: './rsync.pass.j2', dest: '/tmp/rsync.pass', mode: '0600' }
# 变量套变量写法
- name: Configure PHP-FPM {{ php_fpm_conf }}
template: src={{ item.src }} dest={{ item.dest }}
with_items:
- { src: './local/php_www.conf.j2', dest: '{{ php_fpm_conf }}' }
- { src: './local/php.ini.j2', dest: '{{ php_ini_conf }}' }
5. handlers 安装nginx demo
]$ cat lingchen.yaml
- hosts: lingchen
vars: #定义变量,在配置文件中调用
nginx_port: 8018
tasks:
- name: Install Nginx Server
yum:
name: nginx
state: present
- name: Configure Nginx Server
template: #使用template模板,引用上面vars定义的变量至配置文件中
src: ./nginx.conf
dest: /etc/nginx/nginx.conf
notify: #调用名称为Restart Nginx Server的handlers(可以写多个)
- Restart Nginx Server
- name: Start Nginx Server
service:
name: nginx
state: started
enabled: yes
handlers:
- name: Restart Nginx Server
service:
name: nginx
staet: restarted
###########################################################################################
1.无论多少个task通知了相同的handlers,handlers仅会在所有tasks结束后运行一次。
2.只有task发生改变了才会通知handlers,没有改变则不会触发handlers
3.不能使用handlers替代tasks、因为handlers是一个特殊的tasks。
###########################################################################################
6. tags 标签 demo
]$ cat lingchen.yml
- hosts: lingchen
tasks:
- name: Install Nfs Server
yum:
name: nfs-utils
state; present
tags: install_nfs
- name: Service Nfs Server
service:
name: nfs-server
state: started
enabled: yes
tags: start_nfs-server
指定执行某个tags标签
]$ ansible-playbook -i hosts lingchen.yml -t "install_nfs"
忽略执行某个tags标签
]$ ansible-playbook -i hosts lingchen.yml --skip-tags "install_nfs"
7. inclued demo
#编写restart_httpd.yml文件
]$ cat restart_nginx.yml #注意这是一个tasks所有没有play的任何信息
- name: Restart Nginx Server
service: name=nginx state=restarted
#A Project的playbook如下
]$ cat a_project.yml
- hosts: lingchen
tasks:
- name: A Project command
command: echo "A"
- name: Restart nginx
include: restart_nginx.yml
#B Project的playbook如下
]$ cat b_project.yml
- hosts: webserver
tasks:
- name: B Project command
command: echo "B"
- name: Restart nginx
include_tasks: restart_nginx.yml
8. 导入一个完整的playbook文件
]$ cat tasks_total.yml
- import_playbook: ./tasks_1.yml
- import_playbook: ./tasks_2.yml
9. 错误忽略ignore_errors
]$ cat test_9.yml
- hosts: lingchen
tasks:
- name: Command
command: /bin/false
ignore_errors: yes
- name: Create File
file:
path: /tmp/lingchen.txt
state: touch
9. 强制调用handlers(不常用)
]$ cat lingchen.yml
- hosts: lingchen
force_handlers: yes #强制调用handlers
tasks:
- name: Touch File
file:
path: /tmp/bgx_handlers
state: touch
notify: Restart Httpd Server
- name: Installed Packages
yum:
name: tree
state: latest
handlers:
- name: Restart Httpd Server
service:
name: httpd
state: restarted
10. 关闭changed的状态(确定该tasks不会对被控端做任何的修改和变更.)
]$ cat test_11.yml
- hosts: lingchen
tasks:
- name: Installed Httpd Server
yum: name=httpd state=present #传统写法
- name: Service Httpd Server
service: name=httpd state=started
- name: Check Httpd Server
shell: ps aux|grep httpd
register: check_httpd
changed_when: false
- name: OutPut Variables
debug:
msg: "{{ check_httpd.stdout_lines }}"
11. 使用changed_when检查tasks任务返回的结果
]$ cat test_12.yml
- hosts: lingchen
tasks:
- name: Installed Nginx Server
yum: name=nginx state=present
- name: Configure Nginx Server
copy: src=./nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify: Restart Nginx Server
- name: Check Nginx Configure Status
command: /usr/sbin/nginx -t
register: check_nginx
changed_when:
- ( check_nginx.stdout.find('successful'))
- false
- name: Service Nginx Server
service: name=nginx state=started
handlers:
- name: Restart Nginx Server
service: name=nginx state=restarted