Ansible Study - Playbooks


  • Inventoryで定義したホストで何をすべきかを定義します.
  • 自動化プロセスのコードファイルコードセットについて説明します.
  • Yaml形式で
  • に書き込む
  • playbookの目標は、ホスト上のグループを定義された解析可能な「技術ロール」(Role)にマッピングすることである.
    1 hosts、デフォルト
    -
        name: 'Execute two commands on localhost'
        hosts: web_node1
        tasks:
            - name: 'Execute a date command'
              command: date
                
            - name: 'Execute a command to display hosts file'
              command: 'cat /etc/hosts'       
    1つのPlay name+グループのhosts
    -
        name: 'Execute two commands on web_node1'
        hosts: "web_node1,sql_db1"
        tasks:
            -
                name: 'Execute a date command'
                command: date
            -
                name: 'Execute a command to display hosts file'
                command: 'cat /etc/hosts'
    
    2個Play name+2個hosts
    -
        name: 'Execute command to display date on web_node1'
        hosts: web_node1
        tasks:
            -
                name: 'Execute a date command'
                command: date
                 
    -
        name: 'Execute a command to display hosts file contents on web_node2'
        hosts: web_node2
        tasks:
            -
                name: 'Execute a command to display hosts file'
                command: 'cat /etc/hosts'  
    練習プレイブック-連続のTask
    -
        name: 'Stop the web services on web server nodes'
        hosts: web_nodes
        tasks:
            -
                name: 'Stop the web services on web server nodes'
                command: 'service httpd stop'
    
    -
        hosts: db_nodes
        tasks:
            -
                name: 'Shutdown the database services on db server nodes'
                command: 'service mysql stop'
                
    -
        hosts: all_nodes
        tasks:
            -
                name: 'Restart all servers (web and db) at once'
                command: '/sbin/shutdown -r'            
                
    -
        hosts: db_nodes
        tasks:
            -
                name: 'Start the database services on db server nodes'
                command: 'service mysql start'      
                
    -
        hosts: web_nodes
        tasks:
            -
                name: 'Start the web services on web server nodes'
                command: 'service httpd start'