如何在Ansible中发挥作用

时间:2018-12-23 05:36:07

标签: ansible

今天,我正在尝试为詹金(Jenkin)和使用基于角色的方法编写一本有趣的剧本。 但是我在这里面临一些问题,以下是我用于创建Jenkins ansible的语法。

Approch1-site.yml

---
- 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

方法2-site.yml

---
- name: Install Jenkins
  hosts: localhost
  gather_facts: false
  become: true
     - include_role: 
        name: jenkins

这种方法会产生以下错误。

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

有人可以帮助在角色扮演角色中找到正确的方法吗?

预先感谢。

1 个答案:

答案 0 :(得分:2)

我建议您花一些时间在文档上,尤其是YAML docs。如果您以前从未使用过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

包含角色时,您可以传递更多的参数,但这会让您入门。

没有理由不能在同一游戏中同时使用任务和角色,但总的来说,best practice是尽可能多地使用角色。

因此,对于您的特定问题,您需要:

---

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