如何在Ansible中执行角色

如何在Ansible中执行角色,ansible,Ansible,今天,我正试图用基于角色的方法为詹金写一本ansible剧本。 但我在这里面临一些问题,下面是我用来创建Jenkins ansible的语法 方法1-site.yml --- - name: Install Jenkins hosts: localhost gather_facts: false become: true include_role: name: jenkins --- - name: Install Jenkins hosts: lo

今天,我正试图用基于角色的方法为詹金写一本ansible剧本。 但我在这里面临一些问题,下面是我用来创建Jenkins ansible的语法

方法1-site.yml

---
- name: Install Jenkins
  hosts: localhost
  gather_facts: false
  become: true
      include_role:
        name: jenkins
---
- name: Install Jenkins
  hosts: localhost
  gather_facts: false
  become: true
     - include_role: 
        name: jenkins
它给了我以下的错误

ERROR! 'include_role' is not a valid attribute for a Play

The error appears to have been in '/home/ubuntu/ansible/jenkins/site.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: Install Jenkins
  ^ here
ERROR! Syntax Error while loading YAML.


The error appears to have been in '/home/ubuntu/ansible/jenkins/site.yml': line 7, column 20, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  become: true
     - include_role:
                   ^ here
方法2-site.yml

---
- name: Install Jenkins
  hosts: localhost
  gather_facts: false
  become: true
      include_role:
        name: jenkins
---
- name: Install Jenkins
  hosts: localhost
  gather_facts: false
  become: true
     - include_role: 
        name: jenkins
这种方法会产生以下错误

ERROR! 'include_role' is not a valid attribute for a Play

The error appears to have been in '/home/ubuntu/ansible/jenkins/site.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: Install Jenkins
  ^ here
ERROR! Syntax Error while loading YAML.


The error appears to have been in '/home/ubuntu/ansible/jenkins/site.yml': line 7, column 20, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  become: true
     - include_role:
                   ^ here
方法3:site.yml

---
- name: Install Jenkins
  hosts: localhost
  gather_facts: false
  become: true
  tasks: 
     - include_role: 
它给出了以下错误:

ERROR! no action detected in task

The error appears to have been in '/home/ubuntu/ansible/jenkins/site.yml': line 8, column 8, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

  tasks:
     - include_role:
       ^ here
        name: jenkins
方法4-site.yml:

---
- name: Install Jenkins
  hosts: localhost
  gather_facts: false
  become: true
  tasks: 
      include_role: 
      name: jenkins
它给了我以下的错误

ERROR! A malformed block was encountered.

The error appears to have been in '/home/ubuntu/ansible/jenkins/site.yml': line 3, column 3, but may
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---
- name: Install Jenkins
  ^ here
有没有人能帮助我们在剧本中加入角色的正确方式


提前谢谢

我建议多花点时间看医生,尤其是医生。如果您以前没有使用过YAM/Ansible,它可能需要一段时间才能单击,直到单击为止,您可能很难理解为什么在给定的点上需要一个空格,-或:

剧本只是一个剧本的“列表”,因此在YAML中,您可以从以下内容开始列表:

---

- 
然后,每个剧本由一个参数“字典”组成:

---

- hosts: localhost
  gather_facts: no     # Make sure you have at least one play early on that does gather facts
  connection: local    # For localhost only. This stops Ansible opening an SSH connection to its own host
  become: yes          # If you don't specify 'become_user:' defaults to root
有更多的参数,但它们是常见的。你需要做的最后一件事,是告诉剧本该做什么,你可以使用两个参数。两者都以“列表”作为值。第一个是单个任务的列表:

 tasks:
   - name: Your first task
     some_ansible_module:
       module_param_1: some_value
       module_param_2: some_value
   - name: Your second task
     some__other_ansible_module:
       module_param_1: some_value
       module_param_2: some_value
第二个是要包括的角色列表:

 - roles:
     - role_name_1
     - role_name_2
     - role_name_2
在包含角色时,可以传递更多参数,但这会让您开始

没有理由不能在同一个剧本中同时使用任务和角色,但一般来说,应该尽可能多地使用角色

因此,对于您的具体问题,您需要:

---

- hosts: localhost
  connection: local
  gather_facts: false
  become: true
  roles:
    - jenkins

我建议花更多的时间在文档上,尤其是在文档上。如果您以前没有使用过YAM/Ansible,它可能需要一段时间才能单击,直到单击为止,您可能很难理解为什么在给定的点上需要一个空格,-或:

剧本只是一个剧本的“列表”,因此在YAML中,您可以从以下内容开始列表:

---

- 
然后,每个剧本由一个参数“字典”组成:

---

- hosts: localhost
  gather_facts: no     # Make sure you have at least one play early on that does gather facts
  connection: local    # For localhost only. This stops Ansible opening an SSH connection to its own host
  become: yes          # If you don't specify 'become_user:' defaults to root
有更多的参数,但它们是常见的。你需要做的最后一件事,是告诉剧本该做什么,你可以使用两个参数。两者都以“列表”作为值。第一个是单个任务的列表:

 tasks:
   - name: Your first task
     some_ansible_module:
       module_param_1: some_value
       module_param_2: some_value
   - name: Your second task
     some__other_ansible_module:
       module_param_1: some_value
       module_param_2: some_value
第二个是要包括的角色列表:

 - roles:
     - role_name_1
     - role_name_2
     - role_name_2
在包含角色时,可以传递更多参数,但这会让您开始

没有理由不能在同一个剧本中同时使用任务和角色,但一般来说,应该尽可能多地使用角色

因此,对于您的具体问题,您需要:

---

- hosts: localhost
  connection: local
  gather_facts: false
  become: true
  roles:
    - jenkins