ansible模块示例

# blockinfile
- name: Config HOSTS FILE
  blockinfile:
    path: /etc/hosts
    block: "{% for item in groups.tt %}
    {{item}}  {{ hostvars[item]['hostname'] }}

    {% endfor %}"


# yum
- name: Install packages
  yum:
    name: ['gcc*', 'make', 'perl', 'pam', 'pam-devel']
    state: present

# shell
- name: Cp Openssl
  shell: |
	mv /usr/bin/openssl /usr/bin/openssl.OFF


#with_items
- name: Install | Build an install openssh.
  shell: "{{ item }}" 
  with_items:
    - "./config --prefix=/usr/local/openssh"
    - make
    - make install
  args:
    chdir: "/opt/openssh-9.0"


#unarchive
- name: Unarchive Openssh
  unarchive:
    src: "/opt/packages/openssh-9.0.tar.gz"
    dest: "/opt"

# lineinfile&with_items
- name: Setting Passwd
  lineinfile:
    dest: /etc/login.defs
    regexp: "{{ item.regexp }}"
    line: "{{ item.line }}"
    state: present
  with_items:
    - { regexp: "^PASS_MAX_DAYS", line: "PASS_MAX_DAYS   90" }
    - { regexp: "^PASS_MIN_DAYS", line: "PASS_MIN_DAYS   24" }
    - { regexp: "^PASS_MIN_LEN", line: "PASS_MIN_LEN    8" }
  tags:
   - sshd


# copy 
- name: Copy openresty
  template:
    src: zabbix_agentd.conf.j2
    dest: /etc/zabbix/zabbix_agentd.conf
    mode: 0644

#service
- block:
  - name: Start Zabbix-agent
    service:
      name=zabbix-agent
      state=restarted
      enabled=yes
  when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "6"

#systemd
- name: Start Zabbix-agent
  systemd: 
    name=zabbix-agent
    state=restarted
  when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"






































- block:
  - name: "复制zabbix_agent到主机"
    copy:
      src=zabbix-agent-3.0.5-1.el6.x86_64.rpm
      dest=/usr/local/src/
  - name: "安装zabbix_agent"
    yum:
      name: /usr/local/src/zabbix-agent-3.0.5-1.el6.x86_64.rpm
      state: present
  - name: "复制zabbix_agent配置文件"
    template:
      src=zabbix_agentd_centos6.conf.j2
      dest=/etc/zabbix/zabbix_agentd.conf
  - name: "启动zabbix,设置开机启动"
    service:
      name=zabbix-agent
      state=started
      enabled=yes
  when: (ansible_distribution == "CentOS" or ansible_distribution == "RedHat") and ansible_distribution_major_version == "6"
- block:
  - name: "复制zabbix_agent到主机"
    copy:
      src=zabbix-agent-3.0.5-1.el7.x86_64.rpm
      dest=/usr/local/src/
  - name: "安装zabbix_agent"
    yum:
      name: /usr/local/src/zabbix-agent-3.0.5-1.el7.x86_64.rpm
      state: present
  - name: "复制zabbix_agent配置文件"
    template:
      src=zabbix_agentd_centos7.conf.j2
      dest=/etc/zabbix/zabbix_agentd.conf
  - name: "启动zabbix,设置开机启动"
    service:
      name=zabbix-agent
      state=started
      enabled=yes
  when: ansible_distribution == "CentOS" and ansible_distribution_major_version == "7"

你可能感兴趣的:(运维,ansible,自动化,linux)