Ansible - 是否可以跳过角色的执行?

Ansible - 是否可以跳过角色的执行?

这是我的主要剧本,它启动了下面列出的角色

---
 - hosts: slaves
   roles:
      - ntp
      - nmap
      - tcpdump
      - unattended-upgrades
      - traceroute
      - apache
      - mysql

我想让用户决定是否要安装apachemysql,方法是在执行剧本时弹出的提示框中输入是或否。我尝试过不同的方法,例如vars_promptexcept和,when但都没有成功。有什么办法可以实现我的目标吗?谢谢!

答案1

确实,临时用户交互并不是 ansible 的目的。但你可以在运行剧本之前简单地散列几行:

---
 - 主人:奴隶
   角色:
      -NTP
      - 网络地图
      - tcpdump
      - 无人值守升级
      - 跟踪路由
# 今天留下这些:
#-阿帕奇
#-mysql

答案2

如果您可以使用,include_role那么roles您可以像这样运行游戏。

只需在提示符下提供一个以空格分隔的列表。

---
- hosts: localhost
  gather_facts: no
  vars_prompt:
  - name: run_roles
    prompt: Which roles do you want to run
  tasks:
  - include_role:
      name: "{{ role.role }}"
    loop_control:
      loop_var: role
    when: role.role in run_roles
    loop:
    - role: ntp
    - role: nmap
    - role: tcpdump
    - role: unattended-upgrades
    - role: traceroute
    - role: apache
    - role: mysql

答案3

我终于找到了一些有用的东西这是角色文件夹“apache”中包含的剧本,用于安装此服务

---
    - name: Installation apache sur Centos et RedHat
      yum:
        name: httpd
        state: present
      when: (ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat') and reponse_apache == 'oui'

我声明了一个变量“reponse_apache”。如果用户说“oui”,则安装开始

我的主要策略:

---
 - hosts: slaves
   roles:
#      - ntp
#      - nmap
#      - tcpdump
#      - unattended-upgrades
#      - traceroute
      - apache
#      - mysql


   vars_prompt:
     - name: "reponse_apache"
       prompt: "Voulez vous installer apache ? Une exception pare-feu pour le service http sera créée sur les systèmes CentOS 7 (oui/non) "
       private: no

     - name: "reponse_mysql"
       prompt: "Voulez vous installer mysql ? (oui/non) "
       private: no

vars_prompt我创建了一个包含变量的部分reponse_apache。我对 mysql 做了同样的事情,变量为reponse_mysql

---
   - name: Installation python sur les systèmes Debian et Ubuntu
     apt:
       name: python3
       state: present
     when: (ansible_distribution == 'Debian' or ansible_distribution) == 'Ubuntu' and reponse_mysql == 'oui'

   - name: Installation wget sur les systèmes CentOS et RedHat
     yum:
       name: wget
       state: present
     when: (ansible_distribution == 'CentOS' or ansible_distribution) == 'RedHat' and reponse_mysql == 'oui'

   - file:
      path: /etc/repo_mysql
      state: directory
     when: reponse_mysql == 'oui'

   - name: Téléchargement dépôt mysql sur les systèmes CentOS et RedHat
     get_url:
       url: http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
       dest: /etc/repo_mysql/mysql-community-release-el7-5.noarch.rpm
     when: (ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat') and reponse_mysql == 'oui'

   - name: Installation dépôt mysql sur les systèmes CentOS et RedHat
     yum:
       name: /etc/repo_mysql/mysql-community-release-el7-5.noarch.rpm
       state: present
     when: (ansible_distribution == 'CentOS' or ansible_distribution == 'RedHat') and reponse_mysql == 'oui'

   - name: Installation mysql sur tous les systèmes
     yum:
       name: mysql-server
       state: present
     when: reponse_mysql == 'oui'

   - name: Creation BDD "vierge" sur tous les systèmes
     mysql_db:
       name: vierge
       state: present
     when: reponse_mysql == 'oui'

   - name: Demarrage mysql sur les systèmes Debian et Ubuntu
     service:
       name: mysql
       state: started
     when: (ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu') and reponse_mysql == 'oui'

   - name: Demarrage mysql sur les systèmes CentOS et RedHat
     service:
       name: mysqld
       state: started

相关内容