3

编写ansible playbook的一些注意事项

 3 years ago
source link: https://www.lujun9972.win/blog/2017/05/21/%E7%BC%96%E5%86%99ansible-playbook%E7%9A%84%E4%B8%80%E4%BA%9B%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

编写ansible playbook的一些注意事项

刚开始学ansible,在写playbook的时候遇到了一些坑,在此记录一下

  1. playbook顶层必须是三个横杠
  2. 列表中的成员以 "- " 开头,注意后面要 带个空格

    你应该写成

    - hosts:ALL
    
    -hosts:ALL
    
  3. 若单个列表项中有多个key-value对,则每个key也要对齐

    所以应该写成

    - hosts:ALL
      tasks:
        # 任务说明
    
    - hosts:ALL
    tasks:
      # 任务说明
    
  4. 每个task以 "- name:"开头,且后面的操作 只能有一个步骤

    所以只能这样写:

    ---
    - hosts:ALL
      tasks:
        - name: test
          command: ls
    

    而不能写成

    ---
    - hosts:ALL
      tasks:
        - name: test
          command: ls
          command: df
    
  5. 测试时先在小范围服务器上测试,这样你就能很快的得到测试结果。基本上若在一台服务器上测试成功,则在其他服务器上一般也会成功。
  6. 在运行前,最好先用 check-mode 检查一次,以保证所有的命令都能运行
  7. 尽可能频繁地测试,不用怕会破坏现场,因为ansible是暮等的,它描述的就是想要达到的状态。
  8. 请保证在 /etc/ansible/hosts 中定义的所有主机名都能被解析成IP
  9. 由于所有的操作都要通过SSH连接到远程主机来执行,因此请预先跟远程主机做好密钥交换,并且先保证已经将远程主机的SSH key纳入主管机器的 ~/.ssh/known_hosts 中。
  10. 最好根据不同的linux发行版编写不同的playbook

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK