适用场景:有大批量机器需要配置,即便使用高并发线程依旧要花费很多时间
通常在配置大批量机器的场景下使用,灵活性稍有欠缺,但效率几乎可以无限提升,对运维人员的技术水平和前瞻性规划有较高要求
JSON是什么
JSON 特性
JSON 语法规则
JSON 数据的书写格式是:名称/值对
YAML是什么
YAML基础语法
注意:
YAML的键值表示方法
YAML 数组表示方法
-
Jinja2是什么
为什么要学习Jinja2模版
Jinja2模版基本语法
"{{ }}"
内的"{% %}"
内的"{# #}"
内,支持块注释{{varname}}
{{2+3}}
{{1 in [1,2,3]}}
Jinja2模版控制语句
{% if name == '诗仙' %}
李白
{% elif name == '诗圣' %}
杜甫
{% elif name == '诗魔' %}
白居易
{% else %}
李贺
{% endif %}
{% if name == ... ... %}
... ...
{% elif name == '于谦' %}
{% for method in [抽烟, 喝酒, 烫头] %}
{{do method}}
{% endfor %}
... ...
{% endif %}
Jinja2过滤器
例如
playbook语法格式
": "
分隔表示playbook构成
playbook执行结果
[root@ansible ~]# vim ping.yml
--- # 第一行,表示开始
- hosts: all
remote_user: root
tasks:
- ping:
[root@ansible ~]# ansible-playbook ping.yml -f 5
tasks
hosts
playbook执行命令
[root@ansible ~]# vim webuseradd.yml
---
- hosts: web1,web2
remote_user: root
tasks:
- name: create user z3 #task下的name都表示注释
user:
name=z3
- name: change password #task下的name都表示注释
shell:
echo 123 | passwd --stdin z3
[root@ansible ~]# ansible-playbook webuseradd.yml
使用ansible-doc 模块名
查看使用帮助
[root@ansible ~]# echo "hello world" > index.html
[root@ansible ~]# vim installHttpd.yml
---
- hosts: web
tasks:
- name: install the latest version of Apache
yum:
name: httpd
state: latest
- name: change listen port
lineinfile:
path: /etc/httpd/conf/httpd.conf
regexp: '^Listen '
insertafter: '^#Listen '
line: 'Listen 8080'
- name: copy index.html
copy:
src: /root/index.html
dest: /var/www/html/
owner: apache
group: apache
mode: 0644
- name: start and enable apache
service:
name: httpd
state: restarted
enabled: yes
[root@ansible ~]# ansible-playbook installHttpd.yml
变量
[root@ansible ~]# vim dbuseradd.yml
---
- hosts: db
remote_user: root
vars:
username: l4
tasks:
- name: create user "{{username}}"
user:
name: "{{username}}"
- name: change password
shell:
echo 456 | passwd --stdin "{{username}}"
[root@ansible ~]# ansible-playbook dbuseradd.yml
设密码
解决方案
{{ 'urpassword' | password_hash('sha512')}}
变量过滤器
[root@ansible ~]# vim user01.yml
---
- hosts: db
remote_user: root
vars:
username: w5
tasks:
- name: create user "{{username}}"
user:
name: "{{username}}"
password: "{{'123'|password_hash('sha512')}}"
[root@ansible ~]# ansible-playbook user01.yml
变量参数
---
username:
nb
– -e @args.yml
定义yml配置文件,默认创建用户是w5,默认创建密码是123,默认的组是db组
[root@ansible ~]# vim user01.yml
---
- hosts: '{{host}}'
remote_user: root
vars:
username: w5
pwd: '123'
host: db
tasks:
- name: create user "{{username}}"
user:
name: "{{username}}"
password: "{{ pwd |password_hash('sha512')}}"
(1)//传入json格式的参数,创建www用户的密码是www,如果不传入参数,则以默认参数创建
[root@ansible ~]# ansible-playbook user01.yml -e '{"username": "www","pwd": "www"}'
//给web组主机创建ccc用户,密码是ccc
[root@ansible ~]# ansible-playbook user01.yml -e '{"username": "ccc","pwd": "ccc","host": "web"}'
(2)创建yml参数文件
[root@ansible ~]# vim aa.yml
---
username: plj
pwd: dddd
host: cache
(3)调用yml参数文件创建用户
[root@ansible ~]# ansible-playbook user01.yml -e @aa.yml
---
- hosts: cache
remote_user: root
tasks:
- shell: mkdir /tmp/cache
- name: ReStart service httpd
service:
name: httpd
state: restarted
错误处理方法
name: run some command
shell: /usr/bin/somecommand
ignore_errors: True
name: NAME
module: arguments
tags: TAG_ID
-t TAGS, --tags=TAGS
--skip-tags=SKIP_TAGS
--start-at-task=START_AT
[root@ansible ~]# vim installHttpd.yml
... ...
- name: copy index.html
copy:
src: /root/index.html
dest: /var/www/html/
owner: apache
group: apache
mode: 0644
tags: defindex #给copy模块加个标签
- name: start and enable apache
... ...
只执行copy模块
[root@ansible ~]# ansible-playbook installHttpd.yml -t defindex
创建本地httpd.conf文件,修改web监听端口为808,批量修改web主机并重启服务
[root@ansible ~]# vim httpd.conf
... ...
Listen 808
... ...
创建yml文件
[root@ansible ~]# vim web1.yml
---
- hosts: web
remote_user: root
tasks:
- copy:
src: /root/httpd.conf
dest: /etc/httpd/conf/httpd.conf
owner: root
group: root
mode: 0644
tags: httpconf
notify:
- restart httpd
handlers:
- name: restart httpd
service:
name: httpd
state: restarted
执行配置
[root@ansible ~]# ansible-playbook web1.yml -t httpconf
注:在远程批量copy本地文件的时候,只有本地的文件跟远程的文件内容有区别,notify后面的动作才会执行
注意事项:
有些时候需要在满足特定的条件后再触发某一项操作,或在特定的条件下终止某个行为,这个时候需要进行条件判断,when正是解决这个问题的最佳选择,远程中的系统变量facts作为when的条件,可以通过setup模块查看
when 的样例
tasks:
name: somecommand
command: somecommand
when: expr
---
- 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"
- command: test command
register: result
- command: run command
when: result
监控web主机,当某台机器的一分钟的负载大于0.7时,就关闭该主机的apache服务
[root@ansible ~]# vim uptime.yml
---
- hosts: web
remote_user: root
tasks:
- shell: uptime | awk '{printf("%.2f",$(NF-2))}'
register: result
- service:
name: httpd
state: stopped
when: result.stdout|float > 0.7
[root@ansible ~]# ansible-playbook uptime.yml
例如创建多个用户
[root@ansible ~]# vim user02.yml
---
- hosts: '{{host}}'
remote_user: root
vars:
host: cache
tasks:
- name: create user "{{item}}"
user:
name: "{{item.name}}"
group: "{{item.group}}"
password: "{{ item.pwd |password_hash('sha512')}}"
with_items:
-
name: nb
group: ftp
pwd: dachui
-
name: wk
group: man
pwd: xiangj
-
name: yy
group: lp
pwd: hh
-
name: tt
group: games
pwd: uj
[root@ansible ~]# ansible-playbook user02.yml
注: 在新的ansible软件中,将可能废弃include的使用,请替换成import_tasks
,用法不变
tasks:
- include: tasks/setup.yml
- include: tasks/users.yml user=plj #users.yml 中可以通过
{{ user }}来使用这些变量
handlers:
- include: handlers/handlers.yml
roles像是加强版的include,它可以引入一个项目的文件和目录
一般所需的目录层级有
vars
:变量层tasks
:任务层handlers
:触发条件files
:文件template
:模板default
:默认,优先级最低假如有一个play包含了一个叫"x"的role,则
---
- hosts: host_group
roles:
-x
根据参数文件,创建组,创建用户名,添加密码
(1)创建工作目录
[root@ansible ~]# mkdir ansibles
[root@ansible ~]# cd ansibles/
[root@ansible ansibles]# mkdir vars tasks handlers files template default
(2)创建主文件,引入工作目录
[root@ansible ~]# vim user02.yml
---
- hosts: '{{host}}'
remote_user: root
roles:
- /root/ansibles
(3)编辑tasks文件
[root@ansible ~]# vim ansibles/tasks/main.yml
- import_tasks: /root/ansibles/tasks/addGroup.yml
- import_tasks: /root/ansibles/tasks/addUser.yml
(4)创建添加组的任务文件
[root@ansible ~]# vim ansibles/tasks/addGroup.yml
- name: create group
group:
name: "{{item.group}}"
state: present
with_items: "{{userlists}}"
(5) 创建添加用户的任务文件
[root@ansible ~]# vim ansibles/tasks/addUser.yml
- name: create user "{{item.name}}"
user:
name: "{{item.name}}"
group: "{{item.group}}"
password: "{{ item.pwd |password_hash('sha512')}}"
with_items: "{{userlists}}"
(6)创建变量文件
[root@ansible ~]# vim ansibles/vars/main.yml
vars:
host: cache
userlists:
-
name: nb
group: ftp
pwd: dachui
(7)创建参数文件
[root@ansible ~]# vim useradd.json
{"host":"cache",
"userlists":[
{"name":"123",
"group":"sgs",
"pwd":"fbnafnas"
},
{"name":"789",
"group":"vnmsaij",
"pwd":"qrjqwoi"
},
{"name":"vf",
"group":"fhfhhf",
"pwd":"qrwjqwp"
},
{"name":"ttg",
"group":"gjpkw",
"pwd":"terypo"
},
{"name":"yui",
"group":"gmmsvs",
"pwd":"lllll"
}
]
}
(8)测试
[root@ansible ~]# ansible-playbook user02.yml -e @useradd.json
ansible-playbook --syntax-check playbook.yaml
ansible-playbook -C playbook.yaml
--list-hosts
--list-tasks
--list-tags
[root@ansible ~]# ansible-playbook user02.yml --list-hosts
[root@ansible ~]# ansible-playbook user02.yml --list-tasks
[root@ansible ~]# ansible-playbook user02.yml --list-tags
debug模块可以在运行时输出更为详细的信息,帮助我们排错
debug使用样例
---
- hosts: web
remote_user: root
tasks:
- shell: uptime |awk '{printf("%.2f\n",$(NF-2))}'
register: result
- shell: touch /tmp/isreboot
when: result.stdout|float > 0.7
- name: Show debug info
debug: var=result
输出的result是个Json格式的数据