a=10
echo ‘$a‘ 结果$a
echo "$a" 结果10
ymal规范(必须严格遵守):
字典:
key: value(冒号后边必须有空格)
列表:
- (以短横杠开头)
如:
- attila: shuai
olsen: mei
movie:
- name: 复联
salary: 10000
结果:
[ { attila: ‘shuai‘,
olsen: ‘mei‘,
movie: [ { name: ‘复联‘, salary: 10000 } ] } ]
Usage: ansible-playbook [options] playbook.yml [playbook2 ...]
-C, --check #白跑,执行但是不会有结果
--list-hosts #列出符合的主机
-f FORKS, --forks=FORKS #做并发
--syntax-check #检查语法
-k, --ask-pass #输入密码
!!!必须遵守ymal规范
- hosts: web # 指定运行命令的主机
remote_user: root #可以不写,默认是root
tasks: # 任务列表
- name: createuser # 任务1,必须是name
user: name=olsen1 home=/opt/olsen1 uid=666 # 指定模块
- name: copyfile # 任务2
copy: src=/opt/xxx.txt dest=/opt/xxx1.txt
# 执行任务的顺序是,当web组里的所有服务器执行完第一个任务之后在执行第二个
ansible-playbook --syntax-check playbook1.yml # 检查语法
ansible-playbook playbook1.yml # 真正的执行
# 多个playbook,就是在一个.yml文件中有复制了多个像单个playbook那样的,了解即可
# playbook3.yml文件
- hosts: web
tasks:
- name: create{{user}}
user: name={{user}}
ansible-playbook -e user=curry1 playbook3.yml
# 修改host文件 vim /etc/ansible/hosts
[web]
192.168.111.129 user=curry2
192.168.111.130 user=kobe2
ansible-playbook playbook3.yml # 129有了curry2,130有了kobe2
# 修改host文件 vim /etc/ansible/hosts
[web]
192.168.111.129
192.168.111.130
[web:vars]
user=curry3
ansible-playbook playbook3.yml # 129和130都有了curry3
# playbook4.yml文件
- hosts: web
vars:
- user: curry4
tasks:
- name: create{{user}}
user: name={{user}}
ansible-playbook playbook4.yml # 129和130都有了curry4
查看收集信息的命令:
ansible 192.168.111.129 -m setup|more
ansible_all_ipv4_addresses #所有的ipv4地址
ansible_all_ipv6_addresses #所有的ipv6地址
ansible_architecture #系统的架构
ansible_date_time #系统时间
ansible_default_ipv4 #默认的ipv4地址
address ip地址
alias 网卡名称
broadcast 广播地址
gateway 网关
netmask 子网掩码
network 网段
ansible_default_ipv6 #默认的ipv6地址
ansible_device_links #系统的磁盘信息
ansible_distribution #系统名称
ansible_distribution_file_variety #系统的基于公司
ansible_distribution_major_version #系统的主版本
ansible_distribution_version #系统的全部版本
ansible_dns #系统的dns 默认udp 端口53
ansible_domain #系统的域 ldap
ipv4 #ipv4地址
ansible_env #系统的环境
ansible_fqdn #系统的完整主机名
ansible_hostname #系统的简写主机名
ansible_kernel #系统的内核版本
ansible_machine #系统的架构
ansible_memtotal_mb #系统的内存
ansible_memory_mb #系统的内存使用情况
ansible_mounts #系统的挂载信息
ansible_os_family #系统家族
ansible_pkg_mgr #系统的包管理工具
ansible_processor #系统的cpu
ansible_processor_cores #每颗cpu的核数
ansible_processor_count #cpu的颗数
ansible_processor_vcpus #cpu的个数=cpu的颗数*每颗cpu的核数
ansible_python #系统python信息
ansible_python_version #系统python的版本
ansible_system #系统名字
# playbook6.yml文件
- hosts: web
tasks:
- name: install
yum: name=redis
- name: copyfile
copy: dest=/etc/redis.conf src=/etc/redis.conf
tags: copyfile
- name: start
service: name=redis state=started
ansible-playbook playbook6.yml # 全都执行一遍
ansible-playbook -t copyfile playbook6.yml
# 只执行copy那个命令(把bind的ip修改之后执行这步就可以)
# playbook7.yml文件
- hosts: web
tasks:
- name: install
yum: name=redis
- name: copyfile
copy: dest=/etc/redis.conf src=/etc/redis.conf
tags: copyfile
notify: restart # value必须是要触发的模块的value
- name: start
service: name=redis state=started
handlers:
- name: restart
service: name=redis state=restarted
ansible-playbook -t copyfile playbook7.yml # 不仅执行copy还重启
vim /etc/redis/conf
bind {{ansible_default_ipv4.address}] # 上边setup中的参数
- hosts: web
tasks:
- name: install
yum: name=redis
- name: copyfile
template: dest=/etc/redis.conf src=/etc/redis.conf
tags: copyfile
notify: restart
- name: start
service: name=redis state=started
handlers:
- name: restart
service: name=redis state=restarted
ansible-playbook -t copyfile playbook8.yml
mkdir templates
cp /etc/redis/conf templates/
mv redis.conf{,.j2} = mv redis.conf redis.conf.j2 # 改名为.j2格式
- hosts: web
tasks:
- name: install
yum: name=redis
- name: copyfile
# 创建名为templates文件夹(和playbook9.yml在同一文件夹下)直接写文件名就能找到
template: dest=/etc/redis.conf src=redis.conf.j2
tags: copy
notify: restart
- name: start
service: name=redis state=started
handlers:
- name: restart
service: name=redis state=restarted
ansible-playbook -t copyfile playbook9.yml
# playbook10.yml文件
- hosts: web
tasks:
- name: copyfile
copy: content="十步杀一人" dest=/tmp/a.txt
when: ansible_distribution_major_version=="7" # setup的参数
- name: copyfile
copy: content="千里不流行" dest=/tmp/a.txt
when: ansible_distribution_major_version=="6"
ansible-playbook -t copyfile playbook10.yml
# playbook11.yml文件
- hosts: web
tasks:
- name: createuser
user: name={{item}} # 必须是item
with_items:
- attila50
- curry50
- kobe50
ansible-playbook -t copyfile playbook11.yml # 创建了上述三个用户
# playbook12.yml文件
- hosts: web
tasks:
- name: createuser
user: name={{item}}
with_items:
- attila60
- curry60
- kobe60
- name: creategroup
group: name={{item}} # cat /etc/group 能查看组信息
with_items:
- attila60
- curry60
- kobe60
ansible-playbook -t copyfile playbook12.yml # 创建了上述三个用户和三个组
# playbook12.yml文件
- hosts: web
tasks:
- name: crateuser
user: name={{item.name}} group={{item.group}}
with_items:
- {"name":attila80,"group":attila80}
- {"name":curry80,"group":curry80}
- {"name":kobe80,"group":kobe80}
ansible-playbook -t copyfile playbook12.yml
原文:https://www.cnblogs.com/attila/p/10828120.html