Ansible playbook练习
示例1:创建用户
--- - name: create user hosts: openstack gather_facts: false tasks: - name: create {{ item }} user:
name: "{{ item }}"
groups: wheel
password: "{{ 123456 | password_hash('sha512', 'mysecretsalt') }}" loop: - alex - jack
示例2:复制文件
--- - name: copy file hosts: servera tasks: - name: copy ansible.cfg file copy: src: /root/ansible.cfg dest: /opt/ansible.cfg mode: '0644'
示例3:配置防火墙
--- - name: add firewall rule hosts: servera tasks: - name: add http service firewalld: service: http state: enabled permanent: true immediate: true - name: add tcp 8080 firewalld: port: 8080/tcp state: enabled permanent: true immediate: true
示例4: 安装服务
--- - name: install nginx hosts: servera tasks: - name: install nginx package: name: nginx state: present - name: enable service service: name: nginx enabled: true state: started