編輯角色 apache 樣板

  1. 在樣板目錄 templates 新增樣板檔案 index.html.j2,其中變數 ansible_fqdn
    [deyu1@kvm19 ansible]$ vim roles/apache/templates/index.html.j2
    [deyu1@kvm19 ansible]$ cat roles/apache/templates/index.html.j2
    Welcome to {{ ansible_fqdn }} on {{ ansible_default_ipv4.address }}
    
  2. 其中主機名稱及 IP 可用 ansible -m setup 查詢,過濾字串 ansible_fqdn
    [deyu1@kvm19 ansible]$ ansible kvm14 -m setup | grep fqdn
            "ansible_fqdn": "kvm14.deyu.wang",
    [deyu1@kvm19 ansible]$ ansible kvm14 -m setup -a "filter=ansible_fqdn"
    kvm14 | SUCCESS => {
        "ansible_facts": {
            "ansible_fqdn": "kvm14.deyu.wang",
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false
    }
    
  3. 其中主機名稱及 IP 可用 ansible -m setup 查詢,過濾字串 default_ipv4
    [deyu1@kvm19 ansible]$ ansible kvm14 -m setup | grep ipv4
            "ansible_all_ipv4_addresses": [
            "ansible_default_ipv4": {
                    "tx_checksum_ipv4": "on",
                "ipv4": {
                    "tx_checksum_ipv4": "off [fixed]",
                "ipv4": {
    [deyu1@kvm19 ansible]$ ansible kvm14 -m setup -a "filter=ansible_default_ipv4"
    kvm14 | SUCCESS => {
        "ansible_facts": {
            "ansible_default_ipv4": {
                "address": "192.168.122.14",
                "alias": "ens3",
                "broadcast": "192.168.122.255",
                "gateway": "192.168.122.1",
                "interface": "ens3",
                "macaddress": "52:54:00:59:28:59",
                "mtu": 1500,
                "netmask": "255.255.255.0",
                "network": "192.168.122.0",
                "type": "ether"
            },
            "discovered_interpreter_python": "/usr/libexec/platform-python"
        },
        "changed": false
    }
    
  4. 在任務主檔 main.yml 新增任務,將樣板 index.html.j2 複製到 /var/www/html/index.html。
    [deyu1@kvm19 ansible]$ vim roles/apache/tasks/main.yml
    [deyu1@kvm19 ansible]$ cat roles/apache/tasks/main.yml
    ---
    - name: install the latest version of Apache
      yum:
        name: httpd
        state: latest
    - name: Start service httpd
      service:
        name: httpd
        state: started
        enabled: yes
    
    - name: Enable service firewalld
      service:
        name: firewalld
        enabled: yes
        state: started
    
    - name: firewalld add http service
      firewalld:
        service: http
        permanent: yes
        state: enabled
        immediate: yes
    
    - name: Template a file
      template:
        src: index.html.j2
        dest: /var/www/html/index.html
    
  5. 再查看 roles/apache 目錄,樣板目錄 templates 下有 index.html.j2:
    [deyu1@kvm19 ansible]$ tree roles/apache/
    roles/apache/
    ├── defaults
    │   └── main.yml
    ├── files
    ├── handlers
    │   └── main.yml
    ├── main.yml
    ├── meta
    │   └── main.yml
    ├── README.md
    ├── tasks
    │   └── main.yml
    ├── templates
    │   └── index.html.j2
    ├── tests
    │   ├── inventory
    │   └── test.yml
    └── vars
        └── main.yml
    
    8 directories, 10 files