bdjhx 发表于 2018-1-2 19:48:02

ansible系列3-pyYAML

  想要表示列表项,使用一个短横杠加一个空格。多个项使用同样的缩进级别作为同一个列表的一部分
  例子:
  

### 剧本的开头,可以不写  
- hosts: all         <- 处理所有服务器,找到所有服务器;-(空格)hosts:(空格)all
  
tasks:             <- 剧本所要干的事情;                (空格)(空格)task:
  
- command: echo hello oldboy linux.
  (空格)(空格)空格)(空格)-(空格)模块名称:(空格)模块中对应的功能
  ansible all -m command -a "echo hello oldboy linux"
  

编写后检查方法:
  01:ansible-playbook--syntax-check 01.yml
  --- 进行剧本配置信息语法检查
  02:ansible-playbook-C01.yml
  --- 模拟剧本执行(彩排)
  说明:ansible执行时,加1上-vvvv显示ansible详细执行过程,也可以定位异常原因!

剧本编写内容扩展:剧本任务编写多个任务
  

- hosts: all  tasks:
  - name: restart-network
  cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'
  - name: sync time
  cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1"
  


剧本编写内容扩展:剧本任务编写多个主机

https://common.cnblogs.com/images/copycode.gif  

- hosts: 172.16.1.7  tasks:
  - name: restart-network
  cron: name='restart network' minute=00 hour=00 job='/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1'
  - name: sync time
  cron: name='sync time' minute=*/5 job="/usr/sbin/ntpdate pool.ntp.com >/dev/null 2>&1"
  
- hosts: 172.16.1.31
  tasks:
  - name: show ip addr to file
  shell: echo $(hostname -i) >> /tmp/ip.txt
  

  例子:

#cat test1.sh
#!/bin/bash
if [ -z $1 ] || [ -z $2 ];then
   echo "Wrong,Please input two args"
   echo "Usage `basename $0` arguments arguments"
   exit 6
fi
mkdir -pv /usr/local/src/$1/$2
#cat createdir.yml
---
- hosts: "{{ host }}"
user: "{{ user }}"
gather_facts: True
tasks:
    - name: Create Dir in client server
      script: /etc/ansible/test1.sh data log
执行:
#ansible-playbook createdir.yml -e "host=web user=root" # 给{{ host }} {{ user }} 传值
页: [1]
查看完整版本: ansible系列3-pyYAML