I have some code checking for the existence of something. If it has 2 lines it means the post exists. Can I register the variable from the check into a boolean immediately in the first task, rather than needing to cast it in the second? My current solution:

- name: Check if home page has been created
  sudo_user: www-data
  shell: wp post list --post_type=page --post_title=Home --post_status=publish
  register: is_homepage_created

- name: Booleanize homepage check
    is_homepage_created={{is_homepage_created.stdout_lines|length >= 2}}

3 Answers 3


I don't think so, since you need to use set_fact to set it to anything other than its actual output, and I don't think shell can return a boolean directly.

I believe the usual way of doing this is to replicate the conditional you have in the "booleanize" task everywhere you use the fact, which is something you understandably want to avoid. Unfortunately, the register functionality is rather simple.

You could probably use a combination of failed_when and ignore_errors: yes to implement such functionality, but doing so would make a failure to run the shell command map to one boolean or the other, so I wouldn't recommend it.


After some playing around with wp, I could not get it to actually filter output on post title. It always displayed a list of every page. This may not be relevant to you, but it might.

Given this apparent bug, I'd rewrite the play as follows:

First, have wp output in CSV format, which will be easier to work with. Then check whether the desired output appears within it. In the CSV format, if a page named Home exists, then the string ,Home, will be in the output, and should not match anything else, so that is what we will look for.

- name: Get list of WordPress pages
  sudo_user: www-data
  command: wp post list --post_type=page --post_title=Home --post_status=publish --format=csv
  register: wordpress_pages

- name: Create the homepage if it doesn't exist
  sudo_user: www-data
  command: wp post create --post_type=page --post_title=Home --porcelain
  when: "',Home,' not in wordpress_pages.stdout"

Finally, it's best practice to use command instead of shell unless you really absolutely need to pass the command through a shell.


You can use a lookup for this.

ansible-playbook -c local test_lines_lookup.yml



- hosts: localhost
  gather_facts: false

    - set_fact:
        test: "{{ lookup('lines', 'echo "This is stdout"') }}"

    - debug:
        msg: "{{ test }}"


PLAY [localhost] ************************************************************************

TASK [set_fact] *************************************************************************
ok: [localhost]

TASK [debug] ****************************************************************************
ok: [localhost] => {
    "msg": "This is stdout"

PLAY RECAP ******************************************************************************    
localhost                  : ok=2    changed=0    unreachable=0    failed=0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.