RHCE8.0实训:管理变量

来自CloudWiki
跳转至: 导航搜索

实训目的

  • 在playbook中定义变量。
  • 创建使用定义的变量的任务。

实训步骤

配置文件

在当前目录(/root)下新建目录0528

mkdir 0528

cd 0528

ansible.cfg

vi ansible.cfg

[defaults]
inventory = /root/0528/hosts
host_key_checking = False

inventory文件

vi hosts

[master]
localhost ansible_connection=local ansible_ssh_user=root
10.0.0.30 ansible_ssh_user=root
[slave]
10.0.0.32 ansible_ssh_user=root


验证:列出配置过的主机列表

ansible all --list-host

 hosts (3):
    localhost
    10.0.0.30
    10.0.0.32

编写play

[root@localhost ansible]# mkdir data-variables

[root@localhost ansible]# cd data-variables

vi playbook.yml

---
- name: Deploy and start Apache HTTPD service
  hosts: slave
  vars:
      web_pkg: httpd
      firewall_pkg: firewalld
      web_service: httpd
      firewall_service: firewalld
      python_pkg: python3-PyMySQL
      rule: http
  tasks:
      - name: Required pachages are installed and up to date
        yum:
          name:
            - "{{ web_pkg }}"
            - "{{ firewall_pkg }}"
          state: latest
      - name: The {{ firewall_service }} service is started and enabled
        service:
          name: "{{ firewall_service }}"
          enabled: true
          state: started
      - name: The {{ web_service }} service is started and enabled
        service:
          name: "{{ web_service }}"
          enabled: true
          state: started
      - name: Web content is in place
        copy:
          content: "Example web content"
          dest: /var/www/html/index.html
      - name: The firewall port for {{ rule }} is open
        firewalld:
          service: "{{ rule }}"
          permanent: true
          immediate: true
          state: enabled
- name: Verify the Apache service
  hosts: localhost
  become: false
  tasks:
      - name: Ensure the webserver is reachable
        uri:
          url: http://localhost
          status_code: 200


语法检查

[root@localhost data-variables]# ansible-playbook --syntax-check playbook.yml

[DEPRECATION WARNING]: The firewalld module has been moved to the ansible.posix
 collection. This feature will be removed from community.general in version
2.0.0. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.

playbook: playbook.yml


执行play

[root@localhost data-variables]# ansible-playbook playbook.yml

[DEPRECATION WARNING]: The firewalld module has been moved to the ansible.posix collection. This feature will be
removed from community.general in version 2.0.0. Deprecation warnings can be disabled by setting
deprecation_warnings=False in ansible.cfg.

PLAY [Deploy and start Apache HTTPD service] ***********************************************************************

TASK [Gathering Facts] *********************************************************************************************
ok: [localhost]
ok: [10.0.0.30]

TASK [Required pachages are installed and up to date] **************************************************************
ok: [localhost]
ok: [10.0.0.30]

TASK [The firewalld service is started and enabled] ****************************************************************
ok: [localhost]
ok: [10.0.0.30]